Virtual Box版本:5.2.10
Vagrant版本:2.0.4
电脑系统:windows10 64位
虚拟机系统:centos7
连接工具:Xshell
在使用vagrant管理Virtual Box进行创建虚拟机的时候,镜像文件就是各种各样的box。这些box可以在vagrant的官网找到。网址如下所示:
https://app.vagrantup.com/boxes/search
找到的centos7的网址是:
https://app.vagrantup.com/centos/boxes/7
打开centos7的网址,可以知道这个box的名称叫做:centos/7
。不需要我们去寻找下载链接,只需要知道这个名字就足够了。
这里假装你已经安装了vagrant和Virtual box。打开命令行,运行如下命令:
vagrant box add centos/7
vagrant box add
命令表示给vagrant新增一个box centos/7
表示box的名称,是的,我们不需要指定下载链接,只需要给定一个名字,vagrant就会到官网的Vagrant Cloud中寻找并下载。
提示:下载速度很慢,这里提供了一个下载链接:
https://pan.baidu.com/s/1DIis9g8JoyXMJvkkWkiL8A
这种将本地的box添加到Vagrant的命令如下:
vagrant box add -name 'centos/7' [box放置的位置]
找一个文件夹,用于保存Vagrantfile文件,这个文件表示了Vagrant对虚拟机的一些配置文件。我这里在G:\vagrant-workspace
下创建一个文件夹。
cd G:\vagrant-workspace mkdir test cd test
使用如下命令进行初始话
vagrant init centos/7
vagrant init
命令就是初始话命令 centos/7
是指box的名称
运行之后,有如下提示:
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
初始话之后,就可以开始启动虚拟机,运行如下命令:
vagrant up
运行之后,输入如下:
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: test_default_1529231219933_10952 ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2200 (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:2200 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Rsyncing folder: /cygdrive/g/test/ => /vagrant
这时候虚拟机已经启动了。
使用Xshell工具连接虚拟机,这里假设你已经安装了虚拟机。可以使用ssh
命令连接虚拟机。但是此时虚拟机是连接不上的,所以需要进入虚拟机去修改一些配置。
先给root修改一下密码,默认root是没有密码的。
sudo passwd root
修改完了之后,切换到root用户
su root
通过Virtual Box进入虚拟机,默认账号和密码都是:
vagrant
进入文件夹
/etc/ssh
,修改配置文件sshd_config
cd /etc/ssh vi sshd_config
将配置中的
修改为重启sshd.service服务
systemctl restart sshd.service
本地使用Xshell连接虚拟机,运行命令如下:
ssh 127.0.0.1 2200
在弹出的窗口,输入用户名和密码就进入了虚拟机。
ssh
表示连接的命令,127.0.0.1 2200
可以从vagrant up
的时候的输出命令中找到。