通过 Vagrant 在同一接口上使用多个静态地址

Multiple static addresses on same interface via Vagrant

因此,在尝试将手动安装说明转换为 Vagrant 设置 VM 时,我对如何使 /etc/network/interfaces 文件具有以下内容感到困惑:

auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0

auto eth1:1
iface eth1:1 inet static
address 192.168.56.102
netmask 255.255.255.0

auto eth1:2
iface eth1:2 inet static
address 192.168.56.103
netmask 255.255.255.0

这些接口都在 VirtualBox 中的 VM 上使用仅主机适配器。

我知道我可以只创建三个单独的适配器并使用同一个仅供主机使用的适配器来执行以下操作:

config.vm.network "private_network", ip: "192.168.56.101"
config.vm.network "private_network", ip: "192.168.56.102"
config.vm.network "private_network", ip: "192.168.56.103"

但这显然与上述文件不符。我宁愿没有一些在 boot-up/shutdown 上重写 /etc/network/interfaces 的 hacky 解决方案,但我似乎无法让 Vagrant 工作并且除了非常简单的教程之外找不到其他任何东西。

我不确定您是否可以仅使用默认网络配置来做到这一点。

我使用一些参数尝试了一些选项,如

config.vm.network "private_network", ip: "192.168.50.101", :device => "eth1", :adapter => 1, :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.50.102", :device => "eth1:1", :adapter => 1, :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.50.103", :device => "eth1:2", :adapter => 1, :netmask => "255.255.255.0"

但这将导致 VM 无法启动或每个接口都有自己的接口。

然后查看 https://docs.vagrantup.com/v2/networking/public_network.html / DISABLE AUTO-CONFIGURATION,您可以执行以下操作

# manual ip
config.vm.network "public_network", auto_config: false
config.vm.provision "shell",
  run: "always",
  inline: "ifconfig eth1 192.168.50.101 netmask 255.255.255.0 up"
config.vm.provision "shell",
  run: "always",
  inline: "ifconfig eth1:1 192.168.50.102 netmask 255.255.255.0 up"
config.vm.provision "shell",
  run: "always",
  inline: "ifconfig eth1:2 192.168.50.103 netmask 255.255.255.0 up"

当 运行 ifconfig -a 在 VM 上你得到

eth0      Link encap:Ethernet  HWaddr 00:0c:29:e5:fe:e1
          inet addr:172.16.42.238  Bcast:172.16.42.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fee5:fee1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:766 errors:0 dropped:0 overruns:0 frame:0
          TX packets:489 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:83256 (83.2 KB)  TX bytes:69904 (69.9 KB)

eth1      Link encap:Ethernet  HWaddr 00:0c:29:e5:fe:eb
          inet addr:192.168.50.101  Bcast:192.168.50.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fee5:feeb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:125 errors:0 dropped:20 overruns:0 frame:0
          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:10180 (10.1 KB)  TX bytes:1478 (1.4 KB)

eth1:1    Link encap:Ethernet  HWaddr 00:0c:29:e5:fe:eb
          inet addr:192.168.50.102  Bcast:192.168.50.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth1:2    Link encap:Ethernet  HWaddr 00:0c:29:e5:fe:eb
          inet addr:192.168.50.103  Bcast:192.168.50.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1