virtualbox虚拟机NAT模式下不能连接外网

背景

  给VirtualBox虚拟机(装载了Ubuntu16.04系统)配置了两张网卡,网络模式分别为“网络地址转换(NAT)”和“仅主机(Host-Only)适配器”,其中,enp0s3网卡(NAT)用于外网访问,而enp0s8网卡(Host-Only)用于主机访问虚拟机。然而,虚拟机启动后,却不能访问外网。
  

定位

网络配置文件如下:

# vi /etc/network/interface

...
# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1

  eth0使用dhcp,eth1使用static。eth0的实际网络如下:

# ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::a00:27ff:fe55:2858  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:55:28:58  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 1476 (1.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33  bytes 3108 (3.1 KB)
        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         192.168.137.1   0.0.0.0         UG    0      0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.137.0   0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

  enp0s8网卡成为了默认路由,这就导致其他路由不能匹配到的网段都会走enp0s8这个网卡,而我们实际上配置与外网连接的虚拟网卡是enp0s3,环境自然就连接不了外网了。我们可以尝试手动来删除现在的默认路由。

# route del default
# route add default gw 10.0.2.2 dev enp0s3
# route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.137.0   0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

  路由设置成功,OS也可以访问外网了。但这只是修改了本次的路由设置,OS重启后就失效了,因此我们需要将配置持久化。

持久化路由配置

  我们将路由持久化设置在网络配置文件/etc/network/interfaces中。在网卡启动后添加对应的路由增删的代码,与route命令类似,只是在句首加上up即可。

# vi /etc/network/interfaces
...
auto enp0s3
iface enp0s3 inet dhcp
up route add default gw 10.0.2.2 dev enp0s3

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1
up route del default dev enp0s8

  注意:up route add default gw [gateway-addr] dev [dev-name],该语句中,[dev-name]表示外网网卡的名称,即上面的enp0s3,而[gateway-addr]表示外网网卡使用的网关ip地址。
  那么,如何获取这个外网网卡的网关地址呢?virtualbox如下规定:

In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.

  简单的说,就是如果第0个网卡是NAT网卡,那么其网段的第三个数字就0+2=2就是10.0.2.0,网关为10.0.2.2,name server则是10.0.2.3.以此类推。

参考:https://www.virtualbox.org/manual/ch09.html#changenat

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VirtualBox NAT网络无法连接到外部互联网时,有几种可能的原因和解决方法。 1. 防火墙设置:首先检查主机和虚拟机的防火墙设置。确保主机和虚拟机的防火墙允许“VirtualBox NAT网络”通过,并禁用对该网络的任何阻止或限制。 2. 网络适配器设置:确保虚拟机的网络适配器配置正确。在VirtualBox中,选择虚拟机,然后点击“设置”按钮。在“网络”选项卡中,确保网络适配器选择为“NAT”。 3. DHCP设置:检查虚拟机中的DHCP设置。在虚拟机中,打开网络配置或设置界面,并确保DHCP已启用。这样虚拟机将获得有效的IP地址和网关信息,从而连接到外部网络。 4. DNS设置:尝试手动设置虚拟机的DNS服务器。打开虚拟机中的网络配置,输入可靠的DNS服务器地址,例如“8.8.8.8”(Google的公共DNS服务器)。这样可确保虚拟机能够正确解析外部域名。 5. 虚拟网络编辑器设置:有时,VirtualBox的虚拟网络编辑器可能需要调整。打开VirtualBox管理界面,点击“文件”>“主机网络管理器”,然后检查并更正任何可能的配置错误。 6. 更新和修复:确保运行的是最新版本的VirtualBox。在VirtualBox网站上下载并安装最新版本。此外,还可以尝试重新安装VirtualBox来修复任何潜在的软件问题。 如果以上方法都无法解决问题,可以在VirtualBox的官方网站或论坛上寻求进一步的帮助和支持。这些渠道通常会提供更专业的解决方案和支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值