我正在使用vagrant设置Virtualbox Centos虚拟机,并配置了一个公共网络。
config.vm.network :public_network, ip: "10.135.15.137"
我该如何设置网关并将其与此配合使用?
我正在使用vagrant设置Virtualbox Centos虚拟机,并配置了一个公共网络。
config.vm.network :public_network, ip: "10.135.15.137"
# default router
config.vm.provision "shell",
run: "always",
inline: "route add default gw 192.168.0.1"
Vagrant 2.0.1有一个配置选项可以实现此功能,并在文档中进行了说明:
Vagrant → Networking → Public Networks → Using the DHCP Assigned Default Route
为了您的方便,这里是相关部分 (2018-05-15检索):
Using the DHCP Assigned Default Route
Some cases require the DHCP assigned default route to be untouched. In these cases one may specify the use_dhcp_assigned_default_route option. As an example:
Vagrant.configure("2") do |config| config.vm.network "public_network", use_dhcp_assigned_default_route: true end
添加一个解决方案,用于在Ubuntu上使用netplan将默认网关设置为公共网络,并在重新启动后保持持久。
config.vm.provision "shell", inline: <<-SHELL
cat <<EOF >> /etc/netplan/60-override.yaml
---
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
dhcp4-overrides:
use-routes: false
eth1:
dhcp4: true
EOF
netplan apply
sleep 5
SHELL
sudo route add default gw 10.0.0.1 eth1
。 - Peter Tseng