如何在VirtualBox中使用静态IP地址设置NAT和仅主机网络

我正在尝试在VirtualBox中设置一组guest虚拟机,这样每个guest虚拟机都可以访问Internet以及彼此和主机可见。 我也希望客人拥有静态IP地址。

这是我到目前为止遵循的程序:

  1. 关闭要在此设置中使用的仅主机网络的DHCP服务器function
  2. 将分配给主机的IP地址更改为192.168.56.254
  3. 使用2个网络接口卡(NIC)创建来宾计算机
  4. 配置第一个NIC以使用NAT
  5. 配置第二个NIC以使用Hot-Only Networking
  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 
  1. 保存文件
  2. 重启

当guest虚拟机再次启动时,Host-Only网络工作正常。 主机/客户机和来宾/客户机对可以相互ping通,但是因为apt-get失败而无法正常工作。

如果我然后发出service networking restart命令,则网络开始按预期工作。

我究竟做错了什么?

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

主机是Windows 8.1,guest虚拟机是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 

更新2:

执行sudo service networking restartroute -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 

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

如果您在尝试ping外部地址(例如您正在使用的Google DNS服务器),当前网络设置和路由表时发布了结果,则可以更轻松地解决问题。

 /sbin/ifconfig -a /sbin/route -n 

不知道更多,这在黑暗中是一个镜头,但我的猜测是,a)你没有在eth0上获得DHCP地址,或b)你的eth1的网关设置正在弄乱DHCP分配的默认路由。

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

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

编辑:要确保重新启动后eth1上没有网关,请从eth1块中删除行,使其如下所示:

 auto eth1 iface eth1 inet static address 192.168.56.1 netmask 255.255.255.0 

完成后,interfaces文件中不会有任何网关线。

我提出了这个解决方案,这与@jkt123的答案相同,而且@Umar的问题,但更短。 我很感激对它的反馈!

在virtualbox中, 启用NAT和仅主机网络。 (顺便说一下:它适用于我的win7主机。我不知道如何在linux主机上这样做)。

在主机上 – 找到“仅限主机”接口的ip

 ipconfig /all # for windows host ifconfig -a # for linux host 

在guest中, 编辑/ etc / network / interfaces 。 诀窍是逆转订单 eth1(仅限主机)BEFORE eth0(internet / dhcp)。 我不知道为什么。

 auto lo # keep the original loopback settings iface lo ... # yeah, i don't remember, just keep it. # ----> Ok, this is my addition <----- auto eth1 allow-hotplug eth1 # i think hotplug it helps. not sure. iface eth1 inet static address 192.168.56.100 # arbitrary IP address between 2 and 254 auto eth0 # This is the original content iface eth0 inet dhcp # of this file, now at the end. 

重启。