Summary of pitfalls in virtualbox centos7 nat+host-only networking

Summary of pitfalls in virtualbox centos7 nat+host-only networking

1. Problem Background

According to my usual settings, I use bridge mode to surf the Internet, but now I am facing the following demand scenario:

The centos virtual machine is required to be able to yum install and docker pull Internet images, so it needs to access the Internet. CentOS needs to access the local host's VPN. The local xshell can access the cenots virtual machine through ssh.

Therefore, in order to achieve my goal, I used the NAT+host-only method to set up the virtual machine. It took some time to set up, so this article was written to summarize the experience.

2. What are the connection methods for virtual machine network cards?

There are 4 network connection methods in VirtualBox:

  • NAT
  • Bridged Adapter
  • Internal
  • Host-only Adapter

Let's take a look at a picture first. It's easy to see the difference between these four methods through this picture:

image.png

2.1 NAT

NAT: Network Address Translation
NAT mode is the simplest way to enable virtual machines to access the Internet. You can understand it this way:

All data accessed by the Guest on the network is provided by the host. The Guest does not actually exist in the network, and the host and any machine on the network cannot view or access the existence of the Guest.
The Guest can access all networks that the host can access, but the Guest is invisible to the host and other machines on the host network, and even the host cannot access the Guest.

**The relationship between the virtual machine and the host: **Only one-way access is allowed. The virtual machine can access the host through the network, but the host cannot access the virtual machine through the network.
**The relationship between the virtual machine and other hosts in the network: **Only one-way access is possible. The virtual machine can access other hosts in the network, but other hosts cannot access the virtual machine through the network.
**The relationship between virtual machines: **Virtual machines cannot access each other. Virtual machines are completely independent of each other and cannot access each other through the network.​

2.2. Bridged Adapter (Bridge Mode)

Bridge mode, you can understand it like this:
It builds a bridge through the host network card and connects directly to the network. Therefore, it enables the virtual machine to be assigned a separate IP in the network, and all network functions are exactly the same as the real machine in the network.
For a virtual machine in bridge mode, you just need to think of it as a real computer.
**The relationship between the virtual machine and the host: **They can access each other because the virtual machine has an independent IP in the real network segment. The host and the virtual machine are in the same network segment and can access each other through their respective IPs.
**The relationship between the virtual machine and other hosts in the network: **They can access each other. Also, because the virtual machine has an independent IP in the real network segment, the virtual machine and all other hosts in the network are in the same network segment and can access each other through their respective IPs.
**The relationship between virtual machines: **They can access each other for the same reason as above.

2.3. Internal (intranet mode)

Intranet mode, as the name implies, is the internal network mode:
The virtual machine is completely disconnected from the external network, and only the internal network mode between virtual machines is implemented.
**The relationship between the virtual machine and the host: **They cannot access each other, they do not belong to the same network, and cannot access each other.
**The relationship between virtual machines and other hosts in the network: **They cannot access each other for the same reason as above.
**Relationship between virtual machines: **They can access each other, provided that the same network name is set for the two virtual machines when setting up the network. As shown in the configuration diagram above, the name is intnet.

2.4. Host-only Adapter (Host Mode)

Host mode is a relatively complex mode that requires solid basic network knowledge to use. It can be said that the functions implemented in the previous modes can be realized in this mode through the settings of the virtual machine and the network card.
We can understand that the Guest simulates a network card in the host that is dedicated to the virtual machine. All virtual machines are connected to this network card. We can achieve Internet access and many other functions by setting up this network card, such as (network card sharing, network card bridging, etc.).
**The relationship between the virtual machine and the host: **By default, they cannot access each other. Both parties do not belong to the same IP segment. The default IP segment of the host-only network card is 192.168.56.X and the subnet mask is 255.255.255.0. Subsequent virtual machines are also assigned to this network segment. Through network card sharing, network card bridging, etc., virtual machines and hosts can access each other.
**The relationship between virtual machines and network hosts: **By default, they cannot access each other for the same reason as above. Through settings, they can achieve mutual access.
**The relationship between virtual machines: **By default, they can access each other and are in the same network segment.​

3. Let’s configure it!

Software Environment

  • virtualbox 6.0.22
  • centos linux7

3.1 Setting up NAT network

The purpose of setting up a NAT network is to allow the virtual machine to access the Internet.

Shut down the virtual machine before setting up. I fell into a trap here for a long time.

  1. In the VirtualBox main control interface, click [Manage] – [Global Settings] – [Network] – [Add New NAT Network]
  2. In the pop-up dialog box, set [Network CIDR] to [192.168.100.0/24], [OK]

As shown in the figure below:

image.png

In the management interface, click [Settings] – [Network] – [Network Card 1], select [NAT Network] in [Connection Mode], select [NAT Network] in [Interface Name], and click [OK]. The screenshot is as follows:

image.png

【start up】

After logging into the system, run:

# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

Change [ONBOOT] to [yes]. The specific configuration is as follows:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=3b8d1c63-5d2e-43d2-9508-6c1ecec961346
DEVICE=enp0s3
ONBOOT=yes
HWADDR=08:00:27:AF:90:BF

Restart the network service:

systemctl restart network

At this point, you can connect to the Internet normally. Use curl to access Baidu as shown below:

image.png

However, you cannot log in to the server via SSH at this time.

3.2 Setting up a host-only network

Shut down the virtual machine before setting up. I fell into a trap here for a long time.

In [Management] - [Global Settings] - [Network], you should be able to see the [Host-Only Network] tab. After installing VirtualBox, you can see [VirtualBox Host-Only Network] in the computer's [Network and Sharing Center] - [Change Adapter Settings]:

image.png

Right click [Properties] – [Internet Protocol Version 4 (TCP/IPv4)] and you can see that the IP address is [192.168.56.1]:

image.png

Go back to VirtualBox and select Edit Host-Only Network. You can see:

image.png

image.png

image.png

[Settings] – [Network] – [Network Card 2] – [Enable network connection], select [Host-Only network] in [Connection method]:

image.png

Turn on the computer.

Execute the following command:

Copy a network card configuration

cp ifcfg-enp0s3 ifcfg-enp0s8
vi ifcfg-enp0s8

Delete the [HWADDR] line, delete the [UUID] line, add [IPADDR] and [NETMASK], change [DEVICE] to [enp0s8], and [BOOTPROTO] to [static]:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=3b8d1c63-5d2e-43d2-9508-6c1ecec96146
DEVICE=enp0s8
ONBOOT=yes
IPADDR=192.168.56.42
NETMASK=255.255.255.0

Restart the network service.

systemctl restart network

At this point you can use xshell to connect to the virtual machine and configure the connection as shown below:

image.png

The connection is successful as shown in the figure below:

image.png

knock off!

refer to

https://www.cnblogs.com/dee0912/p/5470700.html
https://www.cnblogs.com/york-hust/archive/2012/03/29/2422911.html

This is the end of this article about the pitfalls of using NAT+host-only networking in VirtualBox CentOS7. For more information about VirtualBox host-only networking, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of VirtualBox virtual machine network environment analysis and construction - NAT, bridging, Host-Only, Internal, port mapping

<<:  vue3 custom directive details

>>:  A brief discussion on whether too many MySQL data queries will cause OOM

Recommend

Docker generates images through containers and submits DockerCommit in detail

Table of contents After creating a container loca...

Examples of using temporary tables in MySQL

I've been a little busy these two days, and t...

Use of JavaScript sleep function

Table of contents 1.sleep function 2. setTimeout ...

About Zabbix forget admin login password reset password

The problem of resetting the password for Zabbix ...

Detailed explanation of MySQL string concatenation function GROUP_CONCAT

In the previous article, I wrote a cross-table up...

How to implement the prototype pattern in JavaScript

Overview The prototype pattern refers to the type...

Understanding Vuex in one article

Table of contents Overview Vuex four major object...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

SQL IDENTITY_INSERT case study

Generally speaking, once a column in a data table...

Detailed explanation of MySQL transactions and MySQL logs

Transactional Characteristics 1. Atomicity: After...