Vagrant
是一个基于Ruby
的工具,主要用于建立和部署虚拟化开发环境。它以来于Oracle
的开源VirtualBox
虚拟化系统,经过使用 Chef
建立自动化虚拟环境。Vagrant
主要的功能以下:
Vagrant
依赖于某种虚拟化技术,目前支持常见的 VirtualBox
、 VMWare
等,因此在使用Vagrant
以前咱们须要先安装VirtualBox
或 VMWare
,否则没法使用。推荐安装 VirtualBox
。vagrant
能够快速,方便,全自动的构建虚拟化环境,这也是咱们选择它的缘由,而不是让咱们像之前同样所有本身来部署。docker
这种,有本身的仓库,咱们直接能够经过命令从仓库中拉取虚拟镜像来快速构建VirtualBox
下载地址:https://www.virtualbox.org/wiki/Downloads ,下载好后安装直接下一步操做vagrant
下载地址:https://www.vagrantup.com/downloads.html ,也是直接下一步的操做完成,须要重启电脑安装完。注意:html
- 二者软件最好都下载最新的,省得出现兼容问题,
- 须要安装虚拟机,须要先开启处理器虚拟化技术,VT-x/AMD-V硬件加速。
命令 | 做用 |
---|---|
vagrant box add | 添加box的操做 |
vagrant init | 初始化box的操做,会生成vagrant的配置文件Vagrantfile |
vagrant up | 启动本地环境 |
vagrant ssh | 经过 ssh 登陆本地环境所在虚拟机 |
vagrant halt | 关闭本地环境 |
vagrant suspend | 暂停本地环境 |
vagrant resume | 恢复本地环境 |
vagrant reload | 修改了 Vagrantfile 后,使之生效(至关于先 halt,再 up) |
vagrant destroy | 完全移除本地环境 |
vagrant box list | 显示当前已经添加的box列表 |
vagrant box remove | 删除相应的box |
vagrant package | 打包命令,能够把当前的运行的虚拟机环境进行打包 |
vagrant plugin | 用于安装卸载插件 |
vagrant status | 获取当前虚拟机的状态 |
vagrant global-status | 显示当前用户Vagrant的全部环境状态 |
vagrant
,这个名字随机,就是存放要新建的虚拟机的配置的目录,以后在vagrant
目录中打开 cmd
或Power Shell
窗口,vagrant init centos/7 --box-version 2004.01docker
PS D:\vagrant> vagrant init centos/7 --box-version 2004.01 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.
up
命令,这个过程会去下载咱们须要的镜像,是比较漫长的过程,下载完后会直接启动,vagrant up
命令原本就是启动命令,这是是由于没有因此会先去下载,PS D:\vagrant> vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'centos/7' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: 2004.01 ==> default: Loading metadata for box 'centos/7' default: URL: https://vagrantcloud.com/centos/7 ==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box Download redirected to host: cloud.centos.org Progress: 3% (Rate: 371k/s, Estimated time remaining: 0:18:28)
vagrant box add {name} {url}
的命令进行本地安装,其中,{name}
是咱们要安装的名称, url
是咱们下载到本地的镜像路径PS D:\vagrant> vagrant box add centos/7 E:\迅雷下载\CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'centos/7' (v0) for provider: box: Unpacking necessary files from: file:///E:/%D1%B8%C0%D7%CF%C2%D4%D8/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box box: ==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!
vagrant up
来启动,以下:PS D:\vagrant> vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'centos/7' version '2004.01' is up to date... ==> default: Clearing any previously set forwarded ports... ==> 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: 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/d/vagrant/ => /vagrant ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
vagrant ssh
开启SSH,并登录到 centos7
Windows
电脑的 IP
,其中有个网卡 VirtualBox Host-Only Network
。,这就是虚拟机的网卡,看到其IP
地址段vagrant
的目录下的 Vagrantfile
文件中就行配置修改,这是咱们刚才建立的虚拟机的配置文件 ,配置 config.vm.network "private_network", ip: "192.168.56.10"
,以下所示:192.168.56.1
,那么咱们将这台的配置为 192.168.56.10
,配置好以后须要重启虚拟机,经过 vagrant reload
,进行重启,重启后咱们能够验证其与主机是否能互通Xshell
或 Secure CRT
进行远程链接呢?vagrant ssh
到虚拟机,以后找到 /etc/ssh/sshd_config
文件修改它,经过 sudo vi sshd_config
,修改里面的以下两项内容,修改后直接 wq
保存退出vi
PermitRootLogin yes PasswordAuthentication yes
SSHD
,经过 systemctl restart sshd
,这时候会让你输入root
的密码,root
帐号的密码默认也是 vagrant
,你能够选择直接用 sudo
执行。xshell
下测试是否能登陆本文由AnonyStar 发布,可转载但需声明原文出处。
欢迎关注微信公帐号 :云栖简码 获取更多优质文章
更多文章关注笔者博客 :云栖简码 i-code.onlineshell