SoFunction
Updated on 2025-03-09

Solution to the virtualbox virtual machine cannot connect to the external network in NAT mode

background

Two network cards are configured for the VirtualBox virtual machine (loaded with Ubuntu 16.04 system), with the network modes being "Network Address Translation (NAT)" and "Host-Only (Host-Only) Adapter". Among them, the enp0s3 network card (NAT) is used for external network access, and the enp0s8 network card (Host-Only) is used for host access to the virtual machine. However, after the virtual machine is started, it cannot access the external network.

position

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 route and found 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

The enp0s8 network card has become the default route, which causes other network segments that cannot match other routes will use the enp0s8 network card. In fact, the virtual network card connected to the external network is enp0s3, so the environment naturally cannot connect to the external network. We can try to manually delete the current default route.

# 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

The routing setting is successful, and the OS can also access the external network. But this only modified the routing settings this time, and it will be invalid after the OS restart, so we need to persist the configuration.

Persistent routing configuration

We set the route persistence in the network configuration file /etc/network/interfaces. After the network card is started, add the corresponding route addition and deletion code, which is similar to the route command, just add the 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] represents the name of the external network card, that is, the above enp0s3, and [gateway-addr] represents the gateway IP address used by the external network card.
So, how to obtain the gateway address of this external network card? The virtualbox is as follows:

In NAT mode, the guest network interface is assigned to the IPv4 range 10..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, the gateway is 10.0.2.2, the name server is 10.0.2.3, and so on.

refer to:Link address

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.