介绍一款用于管理虚拟机的命令行使用软件Vagrant

一、vagrant介绍
是一款用于管理虚拟机的命令行使用软件,用ruby语言开发而成。换言说,可以省去你使用虚拟机创建操作系统的所有操作,比如创建虚拟机,挂在镜像文件,一步步点击安装等等,使用vagrant,这些都不需要做了,简简单单两行命令,快速创建属于你个人的系统。
在开发方面,vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用。我们可以通过 vagrant 封装一个 linux 的开发环境,分发给团队成员。成员可以在自己喜欢的桌面系统(mac/windows/linux)上开发程序,代码却能统一在封装好的环境里运行,非常霸气。
基于 spring boot + mybatis plus + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
二、安装用到的软件
1、安装virtualbox (此处不再讲解安装)
虽然 vagrant 也支持 vmware,不过 vmware 是收费的,对应的 vagrant 版本也是收费的
2、安装vagrant
(1)进入官网https://www.vagrantup.com/downloads,选择版本下载
(2)傻瓜式安装
(3)检测是否安装成功
命令行输入vagrant,如下图显示,说明安装成功。
基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
三、下载系统镜像。
在vagrant官网中,有一个findbox的按钮,这个是vagrant的镜像库,里边列出了都要哪些镜像可以用,并且提供了操作文档。
但是这里默认下载往往会比较慢,所以下面我会介绍如何在其它地方下载到基础镜像,然后按照自己的需要重置。如果网速较好,下载顺利的朋友可以选择性地跳过部分内容。
下面我给出最常用的两个 linux 操作系统镜像的下载地址:
centos
在其中选择自己想要下载的版本,列表中有一个 vagrant 目录,里面是专门为 vagrant 构建的镜像。选择其中的 .box 后缀的文件下载即可。这里可以使用下载工具,以较快的速度下载下来。
ubuntu
同样先选择想要的版本,然后选择针对 vagrant 的 .box 文件即可。
如果这里官网的速度较慢,还可以从 清华大学的镜像站 下载。
例如:
四、查询、添加、删除box
接下来我们需要将下载后的 .box 文件添加到 vagrant 中。vagrant 没有 gui,只能从命令行访问。
(1)查询vagrant 已经管理的 box 有哪些:
vagrant box list  
(2)添加box命令
vagrant box add box的文件路径及文件名 --name centos8  
vagrant box add 将 box 添加到vagrant 中,命令后面跟着的是box文件路径
--name centos8 可以为这个 box 指定一个名字
安装好以后如下图,可以在用查询命令查询一下,可看到安装好的box。
(3)删除box命令
vagrant box remove name      #根据名字删除指定的box  
五、vagrant基本操作
(1)新建虚拟机
我们在创建虚拟机的时候,会生产一些文件,所以我们为每个虚拟机最好都创建一个独立的文件夹。然后进入文件夹中。在文件夹路径下初始化
vagrant init [boxname]  #加上boxname 表示使用哪个box 创建虚拟机  
初始化后,会在文件夹下生成一个配置文件
(2) 启动虚拟机
所有的 vagrant 命令都需要在 vagrantfile 所在的目录下执行:
vagrant up  
如果没有报错,说明启动成功。(注意box的名字,写错了会报错)
(3)查看虚拟机状态
vagrant status  
如果是running 就说明我们的虚拟机,启动成功了
(4)链接虚拟机
如果启动没问题,此时在vbox的列表中自动生成一个虚拟机,这个虚拟机以文件夹名字命名,可以看到自动创建的虚机:
执行 vagrant ssh 就能以 vagrant 用户直接登入虚机中。
root 用户没有默认密码,也不能直接登录。需要 root 权限的命令可以通过在命令前添加 sudo 来执行,也可以执行 sudo -i 直接切换到 root 用户。
也可以在 virtualbox 的终端上登录系统,默认的登录用户名和密码都是 vagrant
(5)停止虚拟机
vagrant halt  
(6)暂停虚拟机
vagrant suspend  
(7)恢复虚拟机
vagrant resume  
注意:不管虚机是关闭还是暂停状态,甚至是 error 状态,都可以执行 vagrant up 来让虚机恢复运行
(8)删除虚拟机
vagrant destroy  
六、vagrantfilefile源文件
# -*- mode: ruby -*-# vi: set ft=ruby : # all vagrant configuration is done below. the 2 in vagrant.configure# configures the configuration version (we support older styles for# backwards compatibility). please don't change it unless you know what# you're doing.vagrant.configure(2) do |config|  # the most common configuration options are documented and commented below.  # for a complete reference, please see the online documentation at  # https://docs.vagrantup.com.   # every vagrant development environment requires a box. you can search for  # boxes at https://vagrantcloud.com/search.  config.vm.box = centos8   # disable automatic box update checking. if you disable this, then  # boxes will only be checked for updates when the user runs  # `vagrant box outdated`. this is not recommended.  # config.vm.box_check_update = false   # create a forwarded port mapping which allows access to a specific port  # within the machine from a port on the host machine. in the example below,  # accessing localhost:8080 will access port 80 on the guest machine.  # note: this will enable public access to the opened port  # config.vm.network forwarded_port, guest: 80, host: 8080   # create a forwarded port mapping which allows access to a specific port  # within the machine from a port on the host machine and only allow access  # via 127.0.0.1 to disable public access  # config.vm.network forwarded_port, guest: 80, host: 8080, host_ip: 127.0.0.1   # create a private network, which allows host-only access to the machine  # using a specific ip.  # config.vm.network private_network, ip: 192.168.33.10   # create a public network, which generally matched to bridged network.  # bridged networks make the machine appear as another physical device on  # your network.  # config.vm.network public_network   # share an additional folder to the guest vm. the first argument is  # the path on the host to the actual folder. the second argument is  # the path on the guest to mount the folder. and the optional third  # argument is a set of non-required options.  # config.vm.synced_folder ../data, /vagrant_data   # provider-specific configuration so you can fine-tune various  # backing providers for vagrant. these expose provider-specific options.  # example for virtualbox:  #  # config.vm.provider virtualbox do |vb|  #   # display the virtualbox gui when booting the machine  #   vb.gui = true  #  #   # customize the amount of memory on the vm:  #   vb.memory = 1024  # end  #  # view the documentation for the provider you are using for more  # information on available options.   # enable provisioning with a shell script. additional provisioners such as  # ansible, chef, docker, puppet and salt are also available. please see the  # documentation for more information about their specific syntax and use.  # config.vm.provision shell, inline: < 2222 (host) (adapter 1) 就是把虚机的 ssh 服务端口(22)映射到宿主机的 2222 端口,这样直接在宿主机通过 ssh 客户端问 127.0.0.1:2222 端口就等价于访问虚拟机的 22 端口。
# create a forwarded port mapping which allows access to a specific port# within the machine from a port on the host machine. in the example below,# accessing localhost:8080 will access port 80 on the guest machine.# note: this will enable public access to the opened port# config.vm.network forwarded_port, guest: 80, host: 8080 # create a forwarded port mapping which allows access to a specific port# within the machine from a port on the host machine and only allow access# via 127.0.0.1 to disable public access# config.vm.network forwarded_port, guest: 80, host: 8080, host_ip: 127.0.0.1  
实际上设置端口转发这个功能并不实用,一个很明显的问题就是如果启动多个虚机,很容易就出现宿主机上端口冲突的问题。即使没有端口冲突,使用起来也不方便,我个人不推荐使用的,可以把这部分配置直接删掉。直接使用下面的私有网络。
这个功能是虚拟机软件提供的,可以在虚机的网卡设置中展开高级选项,找到相关的配置:
还有个地方需要注意,默认的 ssh 端口映射在这里没法直接修改。比如像我这样,2222 端口出现莫名问题,如果想要把 22 端口转发到其它端口如 22222,必须要先强制关闭掉默认的那条规则:
因为不关闭的话,只写第二行,会在原来的基础上新加一个端口转发规则,而不是替代原来的
config.vm.network forwarded_port, guest: 22, host: 2222, id: ssh, disabled: true  config.vm.network forwarded_port, guest: 22, host: 22222  
2、配置私有网络
下面这段配置用来配置私有网络,实际上对应的是 virtualbox 的主机网络,也就是 hostonly 网络
# create a private network, which allows host-only access to the machine# using a specific ip.# config.vm.network private_network, ip: 192.168.33.10  
最下面一行取消注释,就可以为虚机设置指定的私有网络地址:
config.vm.network private_network, ip: 192.168.6.25  
修改完成后,执行 vagrant reload 命令重建虚机,就能看到多出来的网卡了。


UPS智能放电仪主要功能
中国欲与LG合作,将在7月首批LG在广州建造OLED工厂
电动车的电池使用寿命一般是多久?
安科瑞Acrel-2000T/B挂壁式无线测温监控设备温度数据报表实时监控
Orcad批量修改网络标号的方法
介绍一款用于管理虚拟机的命令行使用软件Vagrant
音圈电机在手机触摸屏测试中发挥着重要作用
深入探究血压计方案
智能乘客信息系统让旅客了解最新动态
MMU中的页命中、缺页介绍
2017年网络欺诈事件大盘点 社交平台是集中营
华为前三季度销售收入4566亿元 净利润率增至16%
2020年德国的新能源汽车销售情况
埋入式智能保护PPTC嵌入到PCB技术解析
随着中国品牌的不断崛起发力 长安汽车还将面对越来越大的挑战
微软硬件产品回顾史:从机械鼠标到Kinect
无纺布瑕疵检测仪的检测原理是怎样的
关于BWP的技术原理及优势
金山2010年将推10余款自主研发新网游
为什么需要有效电流这个概念