Windows安装配置Vagrant

1、下载
1.1、连接:https://developer.hashicorp.com/vagrant/downloads
1.2 、选择系统、版本、型号,然后下载

在这里插入图片描述

2、安装
2.1、双击运行下载的可执行文件,点击Next

在这里插入图片描述

2.2、先同意许可,然后点击Next

在这里插入图片描述

2.3、点击Change,选择安装目录,然后点击Next

在这里插入图片描述

2.4、点击Install,然后等待安装

在这里插入图片描述
在这里插入图片描述

2.5、点击Finish,然后直接选择重启(Yes)

在这里插入图片描述
在这里插入图片描述

2.6、Windows+R调出运行,输入cmd回车调出DOS命令窗口,输入vagrant -v,如下即安装成功

在这里插入图片描述

3、配置
3.1、配置box的存储位置
3.1.1 简介

通过 Vagrant 创建虚机需要先导入镜像文件,默认存储位置是在用户目录下的 .vagrant.d 目录下,对于 Windows 系统来说,就是 C:\Users\用户名.vagrant.d。如果后续会用到较多镜像,或者C 盘空间比较紧缺,则可通过创建环境变量 VAGRANT_HOME 来设置该目录。
在这里插入图片描述

3.1.2、新建系统环境变量

变量名: VAGRANT_HOME
变量值:(根据你的目录写,文件夹.vagrant.d可以是别的名字)
在这里插入图片描述

4、基础用法
4.1、下载镜像
4.1.1、官方仓库:https://app.vagrantup.com/boxes/search
4.1.2、第三方仓库:http://www.vagrantbox.es/
4.2、将镜像(Box)添加到本地仓库

命令:vagrant box add osName --name
例子:vagrant box add centos/7 --name centos7
如果box已经下载到本地(推荐,官方仓库下载慢):
命令:vagrant box add filePath --name
例子:vagrant box add C:\Users\LENG\Downloads\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box --name centos7
在这里插入图片描述
这时候步骤3.1配置的文件夹下就会增加一个box

在这里插入图片描述

4.3、显示已添加到本地的box列表

命令:vagrant box list
在这里插入图片描述

4.4、初始化虚拟机系统
4.4.1、命令:vagrant init centos7

在这里插入图片描述

4.4.2、当前目录下生成一个文件 Vagrantfile

在这里插入图片描述

4.5、编辑Vagrantfile文件
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network", ip: "192.168.56.10"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  
  config.vm.provider "virtualbox" do |vb|
	# 名称
	vb.name = "centos7-1"
	# CPU大小
	vb.cpus = 2
	# 内存大小
	vb.memory = "2048"
  end
  
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
4.6、启动虚拟机

命令:vagrant up

由于安装的VirtualBox(7.0.2)版本太高,Vagrant(2.2.19)不支持,请查看此解决办法:https://blog.csdn.net/zf14840/article/details/127289925

经过上述方法解决后,执行vagrant up,则可以正常启动
在这里插入图片描述

4.7、连接虚拟机系统

命令:vagrant ssh
在这里插入图片描述

4.8、开启远程登录
4.8.1、切换到root用户

命令:su root
密码默认是vagrant
在这里插入图片描述

4.8.2、修改 /etc/ssh/sshd_config 文件

将 PasswordAuthentication no 改成 yes
在这里插入图片描述

4.8.3、然后重启SSH服务

命令:systemctl restart sshd

5、常用命令列表
命令作用
vagrant -v/version查看Vagrant版本
vagrant global-status查看 Vagrant 当前所有已安装系统
vagrant box add filePath --boxName将镜像(Box)添加到本地仓库
vagrant box list查看所有已添加box
vagrant box remove boxName移除已添加box
vagrant init centos7初始化虚拟机系统
vagrant validate编辑Vagrantfile之后,不确定编写是否正确,使用该命令进行验证
vagrant up启动虚拟机系统
vagrant ssh连接虚拟机系统
vagrant reload重新启动虚拟机系统
vagrant status查看虚拟机系统状态
exit退出ssh连接的虚拟机系统
vagrant halt关闭虚拟机系统
vagrant package打包虚拟机系统
vagrant destroy删除虚拟机系统
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vagrant是一个用于构建和管理虚拟开发环境的工具。它可以帮助开发人员快速创建、配置和共享可重复的开发环境。 要安装Vagrant,您可以按照以下步骤进行操作: 1. 首先,您需要下载并安装适用于您操作系统的Vagrant安装程序。您可以从Vagrant官方网站(https://www.vagrantup.com/)上找到适合您操作系统的安装程序。 2. 安装完成后,打开命令行终端(Windows用户可以使用PowerShell或Git Bash),运行以下命令来验证Vagrant是否成功安装: ``` vagrant --version ``` 如果成功安装,您将看到Vagrant的版本信息。 3. 接下来,您需要选择一个虚拟化提供程序来管理虚拟机。Vagrant支持多种虚拟化提供程序,如VirtualBox、VMware等。您可以根据自己的需求选择其中之一,并确保已经安装配置好了该虚拟化提供程序。 4. 在选择好虚拟化提供程序后,您可以创建一个新的Vagrant项目。在命令行终端中,进入您想要创建项目的目录,并运行以下命令: ``` vagrant init <box> ``` 这里的`<box>`是指您想要使用的虚拟机镜像,也称为"box"。您可以在Vagrant官方网站的"Boxes"页面(https://app.vagrantup.com/boxes/search)上找到各种可用的box。 5. 运行上述命令后,Vagrant将在当前目录下创建一个`Vagrantfile`文件,该文件是Vagrant项目的配置文件。您可以根据需要编辑该文件,配置虚拟机的各种设置,如网络、共享文件夹等。 6. 最后,运行以下命令来启动虚拟机: ``` vagrant up ``` Vagrant将根据`Vagrantfile`中的配置信息创建和启动虚拟机。如果是第一次运行该命令,Vagrant可能需要下载并导入虚拟机镜像,这可能需要一些时间。 至此,您已经成功安装配置Vagrant。您可以使用`vagrant ssh`命令登录到虚拟机,并使用其他Vagrant命令来管理和操作虚拟机。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值