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

  • 2021-01-18 06:54:11
  • OfStack

background

VirtualBox virtual machine (loaded with Ubuntu16.04 system) is configured with two network cards, the network mode is respectively "network address translation (NAT)" and "host only (Host-Only) adapter", in which, enp0s3 network card (NAT) is used for external network access, and enp0s8 network card (Host-Only) is used for host access virtual machine. However, after the virtual machine starts, it cannot access the external network.

positioning

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, only to 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

ES33en0ES34en8 network card becomes the default route, which leads to other routes can not match the network segment will use the ES35en0ES36en8 network card, but we actually configured to connect with the external network virtual network card is ES37en0ES38en3, environment naturally can not connect to the external network. We can try to remove the current default route 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

Route setup successful, OS can also access the external network. However, this only changes the routing Settings for this time, and ES43en fails after restarting, so we need to persist the configuration.

Persistent routing configuration

We set the routing persistence in the network configuration file /etc/network/interfaces. After the network card is started, add the corresponding route add and delete code, similar to route command, only 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] represents the name of the external network card, namely enp0s3 above, and [gateway-addr] represents the gateway ip address used by the external network card.
So, how to get the gateway address of this 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.

]

In short, if the 0th network card is NAT, the third digit of the segment is 0+2=2, which is 10.0.2.0, gateway is 10.0.2.2, name is 10.0.2.3. And so on.

Reference: link address


Related articles: