在Windows上使用VirtualBox+Vagrant安装CentOS 7环境,需要安装VirtualBox和Vagrant工具,以及下载一个CentOS 7的box镜像文件。
一、安装好以上工具,下载好box文件后,使用vagrant命令添加镜像到Vagrant:
vagrant box add 名称 路径
这里成功添加镜像到Vagrant,如下:
二、初始化开发环境
由于是在Windows下,在D盘新建一个文件夹dev,然后进行如下操作:
D:\dev>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: dev_default_1458805394726_355
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
主要是下面几个命令:
$ cd ~/dev # 切换目录
$ vagrant init hahaha # 初始化
$ vagrant up # 启动环境
但是这里有问题,一直提示:default: Warning: Authentication failure. Retrying…
这是Windows下没有配置SSH key的问题,不要着急,使用XShell连接虚拟机,ssh地址127.0.0.1:2222,密码是vagrant.
1.vagrant用户必须存在,所以/home/vagrant目录也必须要存在的
2.导入vagrant公钥
cd /home/vagrant
sudo -u vagrant wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
sudo -u vagrant chmod 700 .ssh
sudo -u vagrant chmod 600 .ssh/authorized_keys
sudo -u vagrant chown -R vagrant:vagrant .ssh
这步搞定,如图(点击看大图):
vagrant_authentication再次回到windows命令行,同样是dev目录,执行vagrant up启动虚拟机:
D:\dev>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: VirtualBox VM is already running.
虚拟机成功启动。