当前位置: > 虚拟化 Virtualization > VirtualBox >

使用VirtualBox+Vagrant安装CentOS 7环境

时间:2017-04-06 00:01来源:linux.it.net.cn 作者:IT

在Windows上使用VirtualBox+Vagrant安装CentOS 7环境,需要安装VirtualBox和Vagrant工具,以及下载一个CentOS 7的box镜像文件。

一、安装好以上工具,下载好box文件后,使用vagrant命令添加镜像到Vagrant:

 
 
1
vagrant box add 名称 路径

这里成功添加镜像到Vagrant,如下:

vagrant_box_add

 

二、初始化开发环境

由于是在Windows下,在D盘新建一个文件夹dev,然后进行如下操作:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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...

主要是下面几个命令:

 
1
2
3
$ 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公钥

 
 
 
1
2
3
4
5
6
7
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启动虚拟机:

 
 
 
1
2
3
D:\dev>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: VirtualBox VM is already running.

虚拟机成功启动。

 




(责任编辑:IT)
------分隔线----------------------------