如何在 VirtualBox 中使用静态 IP 地址设置 NAT 和 Host-Only 网络

Uma*_*aja 23 server virtualbox lubuntu networking

我正在尝试在 VirtualBox 中设置一组来宾,以便他们每个人都可以访问 Internet,并且彼此和主机都可以看到。我还希望客人拥有静态 IP 地址。

这是我迄今为止遵循的程序:

  1. 关闭要在此设置中使用的 Host-Only 网络的 DHCP 服务器功能
  2. 将分配给主机的 IP 地址更改为 192.168.56.254
  3. 创建带有 2 个网络接口卡 (NIC) 的访客计算机
  4. 配置第一个 NIC 以使用 NAT
  5. 将第二个 NIC 配置为使用仅热网络
  6. 在每个服务器上安装操作系统(Ubuntu Server 13.10)
  7. 更新操作系统
  8. 通过编辑 /etc/network/interfaces 配置网络如下
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
gateway 192.168.56.254
dns-search cloudspace.local
dns-nameservers 8.8.8.8 8.8.4.4
Run Code Online (Sandbox Code Playgroud)
  1. 保存文件
  2. 重启

当来宾再次启动时,Host-Only 网络工作正常。主机/来宾和来宾/来宾对机器可以互相 ping 通,但是 Internet 无法正常工作apt-get

如果我随后发出service networking restart命令,网络就会开始正常工作。

我究竟做错了什么?

我尝试切换 NIC 在/etc/network/interfaces文件中出现的顺序。我还在 2 个 NIC 之间交换了 NAT/Host-Only 网络。没有任何效果。

主机是 Windows 8.1,来宾是 Ubuntu Server 13.10。我在 Mac OS X 上尝试过相同的结果。

我将不胜感激这方面的任何帮助。

更新:

我已经包含了以下命令的输出,以帮助诊断:

  • cat /etc/network/interfaces
  • ifconfig -a
  • 路由 -n
$ cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
gateway 192.168.56.254

$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:00:27:75:47:64
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe75:4764/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1180 (1.1 KB)  TX bytes:1332 (1.3 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:93:98:d8
          inet addr:192.168.56.1  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe93:98d8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:140 errors:0 dropped:0 overruns:0 frame:0
          TX packets:225 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14418 (14.4 KB)  TX bytes:27378 (27.3 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1184 (1.1 KB)  TX bytes:1184 (1.1 KB)

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.56.254  0.0.0.0         UG    0      0        0 eth1
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
Run Code Online (Sandbox Code Playgroud)

更新 2:

执行后sudo service networking restart,输出route -n变为:

$ 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    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
Run Code Online (Sandbox Code Playgroud)

那么如何在启动时确保此配置?

jkt*_*123 17

如果您发布了尝试 ping 外部地址(例如您使用的 Google DNS 服务器)时获得的结果、当前网络设置和路由表,那么解决问题会更容易。

/sbin/ifconfig -a
/sbin/route -n
Run Code Online (Sandbox Code Playgroud)

在不知道更多的情况下,这有点摸不着头脑,但我的猜测是 a) 您没有在 eth0 上获得 DHCP 地址,或者 b) 您的 eth1 网关设置与分配的默认路由 DHCP 混淆。

如果您没有获得 eth0 的 DHCP 地址,则可能是 VirtualBox 中的配置错误(例如让您的适配器倒退)。

无论哪种方式,您都不需要专门为 eth1 分配的网关或 DNS 设置,因为这将由 DHCP 在 eth0 上分配,因此我将从您的配置中删除网关、dns-search 和 dns-nameservers 行。如果您的虚拟机在同一网络上并且 VirtualBox 设置正确,则它们仍然可以在没有网关设置的情况下进行通信。

编辑:为了确保重启后 eth1 上没有网关,请从 eth1 块中删除行,使其看起来像这样:

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0
Run Code Online (Sandbox Code Playgroud)

完成后,您的接口文件中将没有任何网关行。