Virtual box virtual machine NAT mode can not connect to the external network

Keywords: network VirtualBox Ubuntu

background

Two network cards are configured for VirtualBox Virtual Machine (loaded with Ubuntu 16.04 system). The network modes are "Network Address Translation (NAT)" and "Host-Only" adapter, where enp0s3 network card (NAT) is used for external network access and enp0s8 network card (Host-Only) is used for host access to virtual machine. However, after the virtual machine starts, it can not access the external network.
  

Location

The network configuration file is as follows:

# 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 uses dhcp and eth1 uses static. The actual network of eth0 is as follows:

# 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

Open its routing and find the problem.

# 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 network card has become the default route, which leads to other network segments that can not match the route will go to enp0s8 network card, and we actually configure the virtual network card connected to the external network is enp0s3, the environment naturally can not connect the external network. We can try to delete the default routing manually.

# 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

With successful routing settings, OS can also access the external network. But this is only a modification of the routing settings, OS reboot will fail, so we need to persist the configuration.

Persistent routing configuration

We set routing persistence in the network configuration file / etc/network/interfaces. After the network card starts, add the corresponding routing additions and deletions code, similar to the route command, just add up at the beginning of the sentence.

# 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

Note: up route add default gw [gateway-addr] dev [dev-name]. In this statement, [dev-name] denotes the name of the external network card, that is, enp0s3 above, and [gateway-addr] denotes the gateway ip address used by the external network card.
Then, how to get the gateway address of the external network card? virtualbox provides as follows:

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.

Simply put, if the 0th network card is a NAT network card, then the third number of its network segment is 0+2=2, which is 10.0.2.0, gateway is 10.0.2.2, name server is 10.0.2.3, and so on.

Reference resources: https://www.virtualbox.org/manual/ch09.html#changenat

Posted by Iceman18 on Mon, 01 Apr 2019 19:12:29 -0700