Press "Enter" to skip to content

Windows下使用Vagrant

Vagrant是用来快速构建虚拟机的工具,只需要短短数行命令,就可以帮我们构建一个基于Virtualbox/VMware的虚拟机。在Linux系统下使用非常方便。本文介绍其在Windows下的使用方法(虽然本文介绍的是Windows下的使用方法, 但其在Linux下使用类似, 比如下方的建立文件夹就用mkdir, 编辑Vagrantfile文件就用vim即可)。

1,准备工作

1.1,安装Virtualbox和Vagrant,这里不多说;

1.2,新建一个文件夹,例如”C:\vm1″,并进入到此目录;

1.3,在Powershell中进入此目录,方法如下(以下方法任选其一):
方法1(推荐): 在当前目录下按一下Shift+F10后松开,然后按s;
方法2: 在目录空白处鼠标右键,在右键菜单中选择”在此处打开 Powershell 窗口(s)”;
方法3: 按一下Windows+x后松开,然后按i,此时进入的Powershell不在”C:\vm1″目录,我们可以手动运行”cd C:\vm1″命令进入之

2,在Powershell中初始化虚拟机

Vagrant的原理是从网络下载回来一个virtualbox的最小虚拟机镜像文件(.box文件), 然后直接导入Virtualbox中进行安装. 所以这里有2种虚拟机安装方式. 一种是手动把.box镜像文件下载到本地以后导入Virtualbox中, 这种方式复杂一些, 但能保证高效. 另一种是官方推荐的方式, 即只需指定镜像的名字, 由vagrant程序去网络上的Box仓库中寻找相关镜像并自动下载. 如果你的网络环境比较差, 建议选择前者(国内用户基本都选择前者吧).

2.1 离线方式安装虚拟机(推荐)

这种情况, 我们需要事先把虚拟机的镜像文件(.box)下载回来. 然后导入到Vagrant中. 由于这种方式不是Vagrant官方推荐的方式, Vagrant官方也不会给你镜像文件(.box)的下载链接, 因此只能自己想办法找到它们

2020.02.23更新:
Ubuntu官网提供了基于Vagrant的直接下载地址, 使用下方地址直接下载即可. 如果你打算使用Ubuntu来当虚拟机, 直接使用下方地址下载以后跳到本文第3步就可以了, 如果你使用CentOS来当虚拟机, 请继续往下看.

https://cloud-images.ubuntu.com/releases/xenial/release/ubuntu-16.04-server-cloudimg-amd64-vagrant.box    # Ubuntu 16.04 for Virtualbox
https://cloud-images.ubuntu.com/releases/bionic/release/ubuntu-18.04-server-cloudimg-amd64-vagrant.box    # Ubuntu 18.04 for Virtualbox
https://cloud-images.ubuntu.com/releases/bionic/release/ubuntu-18.04-server-cloudimg-amd64.vmdk    # Ubuntu 18.04 for VMware

在https://app.vagrantup.com找到你想要的虚拟机, 例如:

https://app.vagrantup.com/ubuntu/boxes/bionic64    # Ubuntu 18.04
https://app.vagrantup.com/ubuntu/boxes/xenial64    # Ubuntu 16.04
https://app.vagrantup.com/centos/boxes/7           # CentOS7
https://app.vagrantup.com/debian/boxes/jessie64    # Debian8

打开上面的地址, 你会看到虚拟机的版本号, 例如v20190221.0.0这样的. 点进去, 你会得到类似于这样的链接: https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20190225.0.0, 在链接后面加上/providers/virtualbox.box, 即可得到该虚拟机的镜像文件下载链接, 如下

https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20190225.0.0/providers/virtualbox.box   # Ubuntu 18.04
https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190221.0.0/providers/virtualbox.box    # Ubuntu 16.04
https://app.vagrantup.com/centos/boxes/7/versions/1902.01/providers/virtualbox.box    # CentOS 7
https://app.vagrantup.com/debian/boxes/jessie64/versions/8.11.0/providers/virtualbox.box    # Debian8

下载你想要的.box文件. 例如我们这里下载了Ubuntu 18.04的镜像

$ vagrant box add foo-box ~/Downloads/bionic-server-cloudimg-amd64-vagrant.box    # 将镜像导入到vagrant中并命名为foo-box

$ vagrant box list    # 查看已导入的镜像
foo-box         (virtualbox, 0)

# 会在当前目录下(本文是C:\vm1)生成一个Vagrantfile的文件
# 该文件记录了该虚拟机的大致配置信息
$ vagrant init foo-box    # 初始化一台虚拟机

此时我们可能需要修改虚拟机的相关配置, 请参照本文第3步即可.

2.2 在线安装虚拟机

这种情况, 我们不需要事先把虚拟机的镜像文件(.box)提前下载回来. 在https://app.vagrantup.com找到你想要的虚拟机,
比如https://app.vagrantup.com/ubuntu/boxes/bionic64
页面上有个简单的介绍, 只需要下面几行命令

# 初始化一台虚拟机
# 会在当前目录下(本文是C:\vm1)生成一个Vagrantfile的文件
# 该文件记录该虚拟机的大致配置信息
$ vagrant init ubuntu/bionic64    #Ubuntu 18.04

$ vagrant init debian/stretch64   #Debian 9

$ vagrant init centos/7           #Centos 7

通过这种方式初始化的虚拟机, 在运行vagrant up启动该虚拟机的时候会自动联网查找相关的镜像文件并自动下载.

3,虚拟机相关配置设定

指定虚拟机IP. 这个非常重要,因为使用Vagrantfile安装的虚拟机默认guest与host网络互不相通
使用Notepad++等工具打开这个Vagrantfile文件,取消注释如下行(即将虚拟机的IP指定为192.168.33.10,如果系统中已经有192.168.33.10的虚拟机在运行,请把这里的IP改成其它)

# config.vm.network "private_network", ip: "192.168.33.10"

指定虚拟机的SSH Key. 这个有点重要. vagrang安装好以后的虚拟机, 默认是禁止使用用户名+密码的方式登陆的, 仅能使用key登陆. 如何指定为我们使用的key呢? 添加如下配置即可(请确保真实系统中的~/.ssh/id_rsa.pub文件存在)

config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"

指定虚拟机内存大小. 根据情况取消注释如下内容

# 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

4,开启和运行虚拟机

$ vagrant up     # 开启虚拟机, 并运行之
# 如果你是使用在线安装方式进行init的, 会需要联网下载.box镜像, 速度取决于你的网络速度.

$ vagrant ssh    # 进入到虚拟机的shell环境, 默认用户名为vagrant,有sudo权限

其它注意事项

1,vagrant权限限制较为严格,进入虚拟机以后, 很可能发现多个虚拟机不能互相ssh, 使用以下命令依次排查问题

$ telnet 192.168.33.10 22

$ egrep '(PasswordAuthentication|PermitRootLogin)' /etc/ssh/sshd_config
#PermitRootLogin yes
#PasswordAuthentication yes
PasswordAuthentication yes        #CentOS 7里面可能默认是no, 需要改成yes以后再systemctl restart sshd

如果需要在Xshell等第三方的ssh工具中登陆虚拟机, 由于vagrant默认禁止了密码登陆,此时我们可以使用public_key来登陆。在Xshell默认的用户名是vagrant, 我们需要将.vagrant\machines\default\virtualbox下的private_key导入进来即可登陆。

2,如果需要端口转发功能,即物理机的公网IP某转口转向虚拟机的22(执行vagrant的时候会有提示是哪个端口)。也可以在Vagrantfile文件中添加一行如下内容, 使得外部可以SSH进来

config.ssh.forward_agent = true

3,一些其它的有用命令

$ vagrant status
Current machine states:

default running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
$ vagrant help
Usage: vagrant [options] []

-v, --version Print the version and exit.
-h, --help Print this help.

Common commands:
box manages boxes: installation, removal, etc.
destroy stops and deletes all traces of the vagrant machine
global-status outputs status Vagrant environments for this user
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment by creating a Vagrantfile
login log in to HashiCorp's Vagrant Cloud
package packages a running vagrant environment into a box
plugin manages plugins: install, uninstall, update, etc.
port displays information about guest port mappings
powershell connects to machine via powershell remoting
provision provisions the vagrant machine
push deploys code in this environment to a configured destination
rdp connects to machine via RDP
reload restarts vagrant machine, loads new Vagrantfile configuration
resume resume a suspended vagrant machine
snapshot manages snapshots: saving, restoring, etc.
ssh connects to machine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs status of the vagrant machine
suspend suspends the machine
up starts and provisions the vagrant environment
validate validates the Vagrantfile
version prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

4,如果需要更严格的guest与host访问权限, 可以参考https://www.vagrantup.com/docs/networking/forwarded_ports.html

Leave a Reply

Your email address will not be published. Required fields are marked *