VirtualBox 多个 HostOnly NIC 没有互联网

VirtualBox 多个 HostOnly NIC 没有互联网

我使用 Vagrantfile 创建了 CentOS 7 VM:

config.vm.define "endpoint" do |machine|
    machine.vm.box = "centos/7" # for all available boxes go to https://app.vagrantup.com/boxes/search
    # machine.vm.box_url = "http://vagrant.com/some.box"
    machine.vm.network :private_network, ip: "192.168.56.10"
    machine.vm.network :private_network, ip: "10.0.2.10",
                       :netmask => "255.255.255.0"
    machine.vm.hostname = "endpoint"
    machine.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--memory", 512]
      v.cpus = 2
    end
  end

创建机器后,我在 VirtualBox 中有以下内容:

目前一切正常,我可以访问互联网

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 52:54:00:8a:fe:e6  txqueuelen 1000  (Ethernet)
        RX packets 133  bytes 65603 (64.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 117  bytes 10305 (10.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.10  netmask 255.255.255.0  broadcast 192.168.56.255
        ether 08:00:27:36:76:a8  txqueuelen 1000  (Ethernet)
        RX packets 1577  bytes 152692 (149.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1017  bytes 184289 (179.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.10  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 08:00:27:71:b3:d0  txqueuelen 1000  (Ethernet)
        RX packets 28  bytes 2542 (2.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 369  bytes 34798 (33.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


 route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         10.0.2.2        0.0.0.0         UG    104    0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     102    0        0 eth2
10.0.2.0        0.0.0.0         255.255.255.0   U     103    0        0 eth3
10.0.2.0        0.0.0.0         255.255.255.0   U     104    0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     101    0        0 eth1
  • VirtualBox 接口:

图像

  • 连接到虚拟机的接口:

图像

现在,由于一个特定的应用程序,我需要禁用 eth0 - 10.0.2.15,但仍然必须能够通过 eth2 访问互联网,因此我禁用了 eth0。对于 eth2,添加 10.0.2.1 作为默认网关(地址可 ping),它是图中的仅主机以太网适配器 #2

#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=10.0.2.10
NETMASK=255.255.255.0
GATEWAY=10.0.2.1
DEVICE=eth2
PEERDNS=no
#VAGRANT-END

但现在无法访问互联网(ping 8.8.8.8 失败)

之前的 DG(10.0.2.2)不再可用(在我禁用 eth0 之后)

还尝试添加 192.168.56.1(也可以 ping 通)作为默认网关,但仍然无法访问互联网。

答案1

事实证明这相当复杂,所以我创建了指南,以防有人遇到同样的问题

https://geekdudes.wordpress.com/2019/08/06/virtual-box-setting-nat-network-and-change-primary-interface/

相关内容