如何在 VirtualBox 中设置具有静态 IP 地址的 NAT 和 Host-Only 网络

如何在 VirtualBox 中设置具有静态 IP 地址的 NAT 和 Host-Only 网络

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

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

  1. 关闭此设置中使用的 Host-Only 网络的 DHCP 服务器功能
  2. 将分配给主机的 IP 地址更改为 192.168.56.254
  3. 创建具有 2 个网络接口卡 (NIC) 的客户机
  4. 配置第一个网卡使用 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
  1. 保存文件
  2. 重启

当客户机再次启动时,仅主机网络工作正常。主机/客户机和客户机/客户机对可以互相 ping 通,但是互联网无法正常工作apt-get

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

我究竟做错了什么?

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

主机是 Windows 8.1,客户机是 Ubuntu Server 13.10。我在 Mac OS X 上尝试过同样的操作,结果类似。

我将非常感激任何能帮助我的。

更新:

我在下面包含了以下命令的输出,以帮助进行诊断:

  • 猫/等/网络/接口
  • 配置文件
  • 路线-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 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

那么我如何在启动时确保这个配置?

答案1

如果您发布尝试 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

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

答案2

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

在虚拟机中,启用 NAT 和仅主机模式网络。(顺便说一句:它可以在我的 win7 主机上运行。我不知道如何在 linux 主机上做到这一点)。

在主机上 -找到“仅主机”接口的 IP

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

在嘉宾中,编辑 /etc/network/interfaces. 诀窍是颠倒顺序 eth1 (仅主机) 位于 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.

重启。

答案3

首先,我启用了两个网络的 DHCP NAT 和仅主机网络static ip add 192.168.40.41

然后,我按照这个命令:

nano 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.41.4
netmask 255.255.255.0

重新启动之前,我输入了以下命令:

sudo service networking restart

然后重新启动。

192.168.40.41在我加载到 VM VirtualBox 后,两个网络都可以正常工作。我可以连接到互联网,也可以通过 Web 浏览器进行连接。

我希望这对编辑有帮助nano

sudo apt-get update
sudo apt-get install nano

然后只需输入nano终端即可启动它。

答案4

我尝试通过配置来设置它/etc/network/interfaces,但没有成功,然后我阅读了有关 netplan 的信息,然后在其中设置了我的 ip 地址 /etc/netplan/00-installer-config.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s10:
      dhcp4: true
    enp0s3:
      dhcp4: true
    enp0s8:
      dhcp4: true
    enp0s9:
      addresses:
      - 192.168.57.3/24
      nameservers: {}
  version: 2

它起作用了

相关内容