vagrant是什么?
vagrant是一个操作虚拟机的工具.
通过命令和配置文件来管理虚拟机,很快就能完成一套开发环境的部署,并可以打包传播,统一了开发环境,也解决了重复配置环境的麻烦.
Vargant的好处
1.统一开发环境。
一次配置打包,统一分发给团队成员,统一团队开发环境,解决诸如“编码问题”,“缺少模块”,“配置文件不同”带来的问题;
2.避免重复搭建开发环境。
新员工加入,不用浪费时间搭建开发环境,快速加入开发,减少时间成本的浪费;
3.多个相互隔离开发环境。
可以在不用box里跑不同的语言,或者编译安装同一语言不同版本,搭建多个相互隔离的开发环境,卸载清除时也很快捷轻松。
win7/win10系统下使用流程
1、安装VirtualBox、vagrant两款软件
直接到官网下载安装即可官网如下:
https://www.virtualbox.org
https://www.vagrantup.com
官网范围速度比较慢,而且这两位软件不同版本可能会造成兼容问题,也就是有可能导致你的vagrant用起来出现一些莫名其妙的问题,所以最好是制定两款的版本安装。
win10版本为:
VirtualBox-5.2.12-122591-Win.exe
vagrant_2.2.7_x86_64.msi
2、下载一个box镜像文件
安装后需要下载一个box镜像文件,也就是linux的镜像(其实也可以在vagrant add安装的时候直接指定官方的镜像名称就会自动下载安装,但鉴于国内下载官方的速度太慢了,所以强烈鉴于先用迅雷之类的工具加速下载下来再本地安装)
下载官网地址如下:
https://app.vagrantup.com/boxes/search
如果是安装centos的话直接点击下面的链接直达:
https://app.vagrantup.com/centos/boxes/7
3、将镜像加载到vagrant容器中
执行如下命令:
```
vagrant box add -name [虚拟机名称] [box放置的位置]
centos/7是虚拟机名称 最后的box放置位置直接写你下载后返的根目录即可,如果没指定的话,默认会自己从官网下载
如:vagrant box add -name 'centos/7' [box放置的位置]
下面是我自己放置的位置习惯:
![upload successful](/resource/images/pasted-1.png)
![upload successful](/resource/images/pasted-0.png)
$ vagrant box add xuni CentOS20200119.box
###### 4、初始化虚拟机
$ vagrant init xuni
vagrant init命令就是初始话命令
xuni 是指box的名称(也就是第三步[虚拟机名称]那个位置)
###### 5、启动虚拟机
初始话之后,就可以开始启动虚拟机,运行如下命令:
$ vagrant up
D:>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'xuni'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: _default_1583637544209_19565
Vagrant is currently configured to create VirtualBox synced folders with
the SharedFoldersEnableSymlinksCreate
option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default: https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default: https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: password
default: Warning: Connection reset. Retrying...
default: Warning: Connection aborted. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => D:/
看到这段内容时说明你的虚拟机已经正常安装启动完毕了!
###### 6、连接虚拟机
如果是使用官方基础镜像的话一开始是连不上的,需要先用ssh命令进入修改配置后才能连接修改配置如下
1、修改sshd_config配置
进入虚拟机中,登录的账号密码均为:vagrant
进入文件夹/etc/ssh,修改配置文件sshd_config
cd /etc/ssh
vi sshd_config
![upload successful](/resource/images/pasted-2.png)
重启sshd.service服务
systemctl restart sshd.service
本地使用Xshell连接虚拟机,运行命令如下:
ssh 127.0.0.1 2200
在弹出的窗口,输入用户名和密码就进入了虚拟机。ssh表示连接的命令,127.0.0.1 2200可以从vagrant up的时候的输出命令中找到。
###### 7、使用xshell工具连接虚拟机
也可以直接用官网的ssh命令直接连接,不过在win用ssh命令连接后还是在cmd窗口,以后开发不太方便,最好是用xshell工具连接。
![upload successful](/resource/images/pasted-3.png)
账号密码默认直接都是:vagrant 即可,(个人改成账号 root登录,密码还是vagrant)
到此就结束了,之后等镜像调整细节完毕后直接打包成自己的镜像,以后在其他电脑用或者本电脑虚拟机出问题的话,可以直接一键安装(相当于还原)
具体打包自己box的方法我博客中也有教程介绍 http://tiance.club/post/299409888.html
作者:xingguang
链接:原文链接:[https://www.tiance.club/post/2370098709.html](https://www.tiance.club/post/2370098709.html)