1、下载
1.1、连接:https://developer.hashicorp.com/vagrant/downloads
1.2 、选择系统、版本、型号,然后下载
2、安装
2.1、双击运行下载的可执行文件,点击Next
2.2、先同意许可,然后点击Next
2.3、点击Change,选择安装目录,然后点击Next
2.4、点击Install,然后等待安装
2.5、点击Finish,然后直接选择重启(Yes)
2.6、Windows+R调出运行,输入cmd回车调出DOS命令窗口,输入vagrant -v,如下即安装成功
3、配置
3.1、配置box的存储位置
3.1.1 简介
通过 Vagrant 创建虚机需要先导入镜像文件,默认存储位置是在用户目录下的 .vagrant.d 目录下,对于 Windows 系统来说,就是 C:\Users\用户名.vagrant.d。如果后续会用到较多镜像,或者C 盘空间比较紧缺,则可通过创建环境变量 VAGRANT_HOME 来设置该目录。
3.1.2、新建系统环境变量
变量名: VAGRANT_HOME
变量值:(根据你的目录写,文件夹.vagrant.d可以是别的名字)
4、基础用法
4.1、下载镜像
4.1.1、官方仓库:https://app.vagrantup.com/boxes/search
4.1.2、第三方仓库:http://www.vagrantbox.es/
4.2、将镜像(Box)添加到本地仓库
命令:vagrant box add osName --name
例子:vagrant box add centos/7 --name centos7
如果box已经下载到本地(推荐,官方仓库下载慢):
命令:vagrant box add filePath --name
例子:vagrant box add C:\Users\LENG\Downloads\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box --name centos7
这时候步骤3.1配置的文件夹下就会增加一个box
4.3、显示已添加到本地的box列表
命令:vagrant box list
4.4、初始化虚拟机系统
4.4.1、命令:vagrant init centos7
4.4.2、当前目录下生成一个文件 Vagrantfile
4.5、编辑Vagrantfile文件
# -*- 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 = "centos7"
# 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", ip: "192.168.56.10"
# 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
config.vm.provider "virtualbox" do |vb|
# 名称
vb.name = "centos7-1"
# CPU大小
vb.cpus = 2
# 内存大小
vb.memory = "2048"
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: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
4.6、启动虚拟机
命令:vagrant up
由于安装的VirtualBox(7.0.2)版本太高,Vagrant(2.2.19)不支持,请查看此解决办法:https://blog.csdn.net/zf14840/article/details/127289925
经过上述方法解决后,执行vagrant up,则可以正常启动
4.7、连接虚拟机系统
命令:vagrant ssh
4.8、开启远程登录
4.8.1、切换到root用户
命令:su root
密码默认是vagrant
4.8.2、修改 /etc/ssh/sshd_config 文件
将 PasswordAuthentication no 改成 yes
4.8.3、然后重启SSH服务
命令:systemctl restart sshd
5、常用命令列表
命令 | 作用 |
---|---|
vagrant -v/version | 查看Vagrant版本 |
vagrant global-status | 查看 Vagrant 当前所有已安装系统 |
vagrant box add filePath --boxName | 将镜像(Box)添加到本地仓库 |
vagrant box list | 查看所有已添加box |
vagrant box remove boxName | 移除已添加box |
vagrant init centos7 | 初始化虚拟机系统 |
vagrant validate | 编辑Vagrantfile之后,不确定编写是否正确,使用该命令进行验证 |
vagrant up | 启动虚拟机系统 |
vagrant ssh | 连接虚拟机系统 |
vagrant reload | 重新启动虚拟机系统 |
vagrant status | 查看虚拟机系统状态 |
exit | 退出ssh连接的虚拟机系统 |
vagrant halt | 关闭虚拟机系统 |
vagrant package | 打包虚拟机系统 |
vagrant destory | 删除虚拟机系统 |