- 下载 Vagrant 和 Virtualbox https://www.vagrantup.com/downloads
选择版本 需要Vagrant 和Virtualbox 版本兼容
Vagrant 2.2.19 virtualbox 6.1
- 下载 系统镜像
镜像后缀名
.box
官网
https://app.vagrantup.com/boxes/search
Centos 镜像
http://cloud.centos.org/centos/
https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1910_01.VirtualBox.box
Ubuntu 镜像 http://cloud-images.ubuntu.com/
添加镜像
# 查看已经添加的镜像列表
vagrant box list
#添加镜像 -name box 指定一个名字
vagrant box add box的文件路径及文件名 --name centos7
# 删除box命令
# vagrant box remove NAME #根据名字删除指定的box
# 初始化 虚拟机
# 初始化后,会在文件夹下生成一个配置文件
vagrant init [boxname] #加上boxname 表示使用哪个box 创建虚拟机
# 重要配置 桥接模式
# config.vm.network "public_network"
# 虚拟机状态
vagrant status
# 停止虚拟机
vagrant halt
# 暂停虚拟机
vagrant suspend
#恢复虚拟机 不管虚机是关闭还是暂停状态,甚至是 error 状态,都可以执行 vagrant up 来让虚机恢复运行
vagrant resume
#删除虚拟机
vagrant destroy
访问
执行 vagrant ssh 就能以 vagrant 用户直接登入虚机中。
root 用户没有默认密码,也不能直接登录。需要 root 权限的命令可以通过在命令前添加 sudo 来执行,也可以执行 sudo -i 直接切换到 root 用户。
也可以在 VirtualBox 的终端上登录系统,默认的登录用户名和密码都是 vagrant
# -*- 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 "public_network"
# 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: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
重点
启动虚机时,默认的 22 (guest) => 2222 (host) (adapter 1) 就是把虚机的 SSH 服务端口(22)映射到宿主机的 2222 端口,这样直接在宿主机通过 ssh 客户端问 127.0.0.1:2222 端口就等价于访问虚拟机的 22 端口。
需要注意,默认的 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
vagrant创建虚拟机配置内存、硬盘、网络
vagrant plugin install vagrant-disksize
vagrant init
# -*- 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 = "debian/buster64"
#虚拟机主机名称
config.vm.hostname = "master"
#磁盘大小
config.disksize.size = "40GB"
# 配置成静态IP地址
config.vm.network "private_network", ip: "192.168.200.20"
# VirtaulBox相关配置
config.vm.provider "virtualbox" do |vb|
vb.name = "debian_master" # VirtualBox名称
vb.gui = false # 启动机器时显示 VirtualBox GUI
vb.memory = "10024" # 虚拟机存储大小,mb
vb.cpus = 2 # cpu 大小
end
# 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: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
vagrant 用户名密码登录
sudo -i 切换到root权限
vim /etc/ssh/vim sshd_config
把这个配置开启起来
PasswordAuthentication yes
systemctl restart sshd.service
vagrant global-status 查看到全局的虚拟机状态