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

Solution to the problem that 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), and the network modes are "Network Address Translation (NAT)" and "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

After opening the route, I 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 becomes the default route, which means that network segments that cannot be matched by other routes will go through the enp0s8 network card. However, the virtual network card we actually configure to connect 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 is set up successfully and OS can access the external network. But this only modifies the routing settings for this time, and it will become invalid after the OS is restarted, so we need to make the configuration persistent.

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, except that up is added 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: in the statement up route add default gw [gateway-addr] dev [dev-name], [dev-name] indicates the name of the external network card, i.e. enp0s3 above, and [gateway-addr] indicates the gateway IP address used by the external network card.
So, how to obtain the gateway address of this external network card? VirtualBox stipulates the following:

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.

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

Reference: Link address

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • CentOS7 network configuration tutorial under VirtualBox (can connect to the external network)
  • Detailed explanation of VirtualBox virtual machine network environment analysis and construction - NAT, bridging, Host-Only, Internal, port mapping
  • VirtualBox configures virtual network card (bridge) to achieve host-virtual machine network interconnection (graphic tutorial)
  • A tutorial on how to use VirtualBox to connect a virtual machine to the network
  • Detailed explanation of centos6.5 network settings in VirtualBox
  • Detailed explanation of how to configure the network connection between the guest and host in VirtualBox
  • VirtualBox configures virtual network card (bridging) to achieve host-virtual machine network interconnection
  • Detailed explanation of VirtualBox + CentOS virtual machine network card configuration
  • Analysis of the principle of virtualbox virtual machine network settings
  • Solution to VirtualBox not specifying the network interface to be bridged
  • VirtualBox 2.2.0 uses the host network to configure Internet access

<<:  Vue implements login type switching

>>:  Detailed explanation of redundant and duplicate indexes in MySQL

Recommend

JS asynchronous execution principle and callback details

1. JS asynchronous execution principle We know th...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

HTML uncommon tags optgroup, sub, sup and bdo example code

Optgroup is used in the select tag to make the dro...

Basic knowledge of website design: newbies please read this

Now many people are joining the ranks of website ...

Some questions about hyperlinks

<br />I am very happy to participate in this...

HTML sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

...

XHTML introductory tutorial: text formatting and special characters

<br />This section introduces how to impleme...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

Common considerations for building a Hadoop 3.2.0 cluster

One port changes In version 3.2.0, the namenode p...