1、Vagrantfile参数说明:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
################# Box配置 #################
config.vm.box = "debian_buster"
config.vm.box_version = "3.4.00"
config.vm.box_url = "https://vagrantcloud.com/debian/buster"
################# 网络配置 #################
# 端口转发配置
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 2004, host: 33214, protocol: "tcp"
config.vm.network "forwarded_port", guest: 2004, host: 33214, protocol: "udp"
config.vm.network "forwarded_port", guest: 2004, host: 8080, auto_correct: true
config.vm.usable_port_rage = 8000..8999
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: 22222
# NAT 网络
# 启动时会默认起 1 个 NAT 网络,保证了虚机可以上互联网
# 主机网络配置
config.vm.network "private_network", type: "dhcp" # DHCP获取IP地址(不推荐)
config.vm.network "private_network", ip: "192.168.56.10" # 保证了宿主机和虚机,以及虚机和虚机之间的通信
config.vm.network "private_network", ip: "fea3:4442:82f1::c3", netmask: "48"
# 桥接网络配置
config.vm.network "public_network"
config.vm.network "public_network", auto_config: false
config.vm.network "public_network", ip: 192.168.56.100
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
config.vm.network "public_network", bridge: [
"en1: Wi-Fi (AirPort)",
"en2: Broadcom NetXtreme Gigabit Ethernet Controller",
]
# config.vm.network "public_network", auto_config: false
# config.vm.provision "shell",
# run: "always",
# inline: "ifconfig eth1 192.168.56.100 network 255.255.255.0 up"
# config.vm.provision "shell",
# run: "always",
# inline: "ifconfig eth1 inet6 f303::100/48"
# config.vm.network "public_network", ip: "192.168.56.100"
# config.vm.provision "shell",
# run: "always",
# inline: "route add default gw 192.168.56.1"
# config.vm.provision "shell",
# run: "always",
# inline: "route -A inet6 add default gw fc00::1 eth1"
# config.vm.provision "shell",
# run: "always",
# inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
################# SSH配置 #################
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.ssh.insert_key = false # 以后创建出来的虚拟机都是用统一的 insecure_private_key 来登录
# 默认每个虚拟机会生成自己的 key
################# 虚拟机主机名称 #################
config.vm.hostname = "deb"
################# 同步目录配置 #################
config.vm.synced_folder ".", "/vagrant",create: true
config.vm.synced_folder "../data", "/vagrant_data",create: true,owner: "root",group: "root"
#第一个参数是主机上共享的目录,第二个参数是vagrant虚拟机的挂载目录,第三个参数在虚拟机里面没有挂载目录则创建挂载目录,第四个参数所属用户,第五个参数所属用户组
################# Virtualbox 配置 #################
config.vm.provider "virtualbox" do |vb|
vb.gui = false # 启动虚拟机时不启用GUI界面,节省宿主机资源
vb.name = "Debian_11" # Virtuablbox主机名称
vb.memory = "1024" # 虚拟机内存
vb.cpus = "2" # 虚拟机CPU
# 磁盘设置需要安装插件
end
################# Provision 配置 #################
# 启动虚拟机运行的脚本
config.vm.provision "shell", path: "DNS.sh"
# 在与Vagrantfile同级的目录下创建一个`bootstrap.sh`文件
config.vm.provision "shell", "echo hello-world!"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
end
2、Vagrantfile配置案例 单个虚拟机配置(这个创建后的虚拟机装默认关闭防火墙、Selinux、SWAP分区以及安装curl软件包)
Vagrant.configure(2) do |config|
config.vm.box = "centos"
config.vm.box = "ling218"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.synced_folder ".", "/vagrant",create: true
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.ssh.insert_key = false
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.name = "CentOS7"
vb.memory = "1024"
vb.cpus = "2"
end
config.vm.provision "shell", inline: <<-SHELL
# 安装curl软件
yum clean all
yum makecache fast
yum install -y curl
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭Selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 关闭swap
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
SHELL
end
多个虚拟机配置(执行 vagrant up
命令后将创建两个虚拟机 web和db)
Vagrant.configure(2) do |config|
config.vm.box = "centos"
config.vm.synced_folder ".", "/vagrant", create: true
config.vm.define "web-server" do |web|
web.vm.hostname = "web"
web.vm.network "private_network", ip: "192.168.56.10"
web.vm.provider "virtualbox" do |vb|
vb.name = "web"
vb.cpus = "2"
vb.memory = "1024"
vb.gui = false
end
config.vm.define "db-server" do |web|
web.vm.hostname = "db"
web.vm.network "private_network", ip: "192.168.56.11"
web.vm.provider "virtualbox" do |vb|
vb.name = "db"
vb.cpus = "2"
vb.memory = "1024"
vb.gui = false
end
标签:vb,network,Vagrantfile,虚拟机,配置,#################,vm,config,自定义
From: https://blog.51cto.com/u_15551039/6047188