Four ways to set the network connection of the VirtualBox virtual machine

Here I will give you a general introduction to the network configuration and application of VBox.

VirtualBox provides four network access modes, they are:
1. NAT network address translation mode (NAT, Network Address Translation)
2. Bridged Adapter bridge mode
3, Internal internal network mode
4, Host-only Adapter host mode
below We analyze and explain these four network modes respectively:

The first NAT mode
Explanation:
NAT mode is the easiest way to realize the Internet access of virtual machines. You can understand it like this: all data that Vhost accesses the network is provided by the host. The vhost does not really exist in the network, and neither the host nor any machine in the network can view and access the existence of the vhost.
The relationship between the virtual machine and the host:
only one-way access, the virtual machine can access the host through the network, and the host cannot access the virtual machine through the network.
The relationship between the virtual machine and other hosts on the network:
only one-way access, the virtual machine can access other hosts on the network, and other hosts cannot access the virtual machine through the network.
The relationship between virtual machines and virtual machines:
they cannot access each other, virtual machines and virtual machines are completely independent, and they cannot access each other through the network.
IP: 10.0.2.15
Gateway: 10.0.2.2
DNS: 10.0.2.3
Multiple network cards of a virtual machine can be configured to use NAT, the first network card is connected to the private network 10.0.2.0, the second network card is connected to the private network 10.0.3.0, and so on. The default client ip (IP Address) is 10.0.2.15, the gateway (Gateway) is 10.0.2.2, and the domain name server (DNS) is 10.0.2.3, you can manually refer to this to modify.
Advantages and disadvantages of the NAT scheme:
When the laptop is plugged into the network cable: The virtual machine can access the host, and the virtual machine can access the Internet. After port mapping is done (described at the end), the host can access services (such as databases) on the virtual machine.
When the laptop is not plugged into the network cable: The "local connection" of the host has a red cross. The virtual machine can access the host, but the virtual machine cannot access the Internet. After port mapping, the host can access the services (such as databases) on the virtual machine.

The second Bridged Adapter mode
Explanation: The
bridge mode is my favorite mode, and at the same time, the analog degree is quite perfect. You can understand it this way, it is directly connected to the network by setting up a bridge through the host network card. Thus, it enables virtual machines to be assigned to separate IPs on a network, with all network functions exactly as real machines on the network.
Relationship between virtual machine and host:
They can access each other because the virtual machine has an independent IP in the real network segment. The host and 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:
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.
Virtual machine-to-virtual machine relationship:
can access each other for the same reasons as above.
IP: Generally assigned by DHCP, it is on the same network segment as the "local connection" IP of the host. The virtual machine can communicate with the host.
When the laptop is plugged into the network cable: (if there is a DHCP server on the network) the host and the virtual machine will obtain an IP through DHCP respectively, and the two IPs are in the same network segment. The host and the virtual machine can be pinged, and the virtual machine can access the Internet.
When the laptop is not plugged into the network cable: The host and the virtual machine cannot communicate. If the "local connection" of the host has a red cross, you cannot manually specify the IP. The virtual machine also cannot obtain an IP address through DHCP. After manually specifying the IP, it cannot communicate with the host because the host has no IP.
At this time, the VirtualBox Host-Only Network card of the host has an ip, 192.168.56.1. Even if the virtual machine manually specifies the IP 192.168.56.*, it cannot ping the host.

The third Internal mode
Explanation:
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 the virtual machine and the virtual machine is realized.
The relationship between virtual machines and hosts:
they cannot access each other, they do not belong to the same network, and cannot access each other.
The relationship between the virtual machine and other hosts in the network:
cannot access each other for the same reasons as above.
Relationship between virtual machines and virtual machines:
They can access each other, provided that the two virtual machines have the same network name when setting the network. As shown in the configuration diagram above, the name is intnet.
IP: VirtualBox's DHCP server will assign an IP to it. Generally, it gets 192.168.56.101, because it starts from 101, or you can manually specify 192.168.56.*.
When the laptop is plugged into the network cable: the virtual machine can communicate with the host's VirtualBox Host-Only Network network card
. This solution is not affected by whether the host's local connection (network card) has a red cross.

The fourth Host-only Adapter mode
Explanation:
Host mode, which is a more complex mode, requires a relatively solid basic knowledge of the network to play. It can be said that the functions implemented by the previous modes can be implemented through the settings of the virtual machine and the network card in this mode.
We can understand that Vbox simulates a network card for virtual machines in the host, and all virtual machines are connected to this network card. We can set this network card to achieve Internet access and many other functions, such as (network card sharing, NIC bridging, etc.).
The virtual machine and the host
cannot access each other by default. The two 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. The subsequent virtual machines are also assigned to this 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. By setting, mutual access can be achieved.
The relationship between virtual machines and virtual machines
can access each other by default, and they are all in the same network segment.
The virtual machine accesses the host using the IP of the host's VirtualBox Host-Only Network card: 192.168.56.1, regardless of whether the host's "local connection" has a red cross, it will always be connected.
When the host accesses the virtual machine, it uses the IP of the virtual machine's network card 3: 192.168.56.101, regardless of whether the host's "local connection" has a red cross or not, it will always be connected.
The virtual machine uses its own network card 2 to access the Internet. At this time, the host must be able to access the Internet through a "local connection" (wireless network card is not).

Through the understanding of the above network modes, we can use it flexibly to simulate the formation of Any kind of network environment we want.
For example, I want to simulate a host computer to monitor the network environment of a local area network.
First, I opened two virtual machines, vhost1 and vhost2. Of course, if the hardware allows, I can also add vhost3, vhost4...
I set all vhosts to internat intranet mode, the network name is intnal, and the gateway is 192.168.56.100, meaning It is through the 192.168.56.100 network card to access the Internet. One of them is vhost1 which I set as dual network card, one is in intranet mode (192.168.56.100), and the other is in bridge mode (192.168.1.101). Two network cards are set to share the Internet with dual network cards
. There is a local area network between the virtual machines. One of the virtual machines vhost1 is connected to the external network, and all the virtual machines in the local area network are connected to the external network through vhost1. In this way, vhost1 can monitor the Internet access of the entire virtual machine LAN.

NAT setting port mapping
http://huzhangsheng.blog.163.com/blog/static/34787784200802801435931/
You can set up a virtual machine service (such as WEB service) by using the command line tool VboxManage proxy. You need to know which port the virtual machine's service uses, and then decide which port to use on the host (usually, but not always, you want to have the virtual machine and the host use the same port). Providing a service on the host requires the use of a port, you can use any port on the host that is not ready to serve. An example of how to set up a new NAT, connecting to an ssh server on a virtual machine, requires the following three commands:
VBoxManage setextradata 'Linux Guest' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol' TCP
VBoxManage setextradata 'Linux Guest' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort' 22
VBoxManage setextradata 'Linux Guest' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort ' 2222
Description: VboxManage is a command line program, please query your VirtualBox installation directory, 'Linux Guest' is the virtual host name. guestssh is a custom name, you can set it arbitrarily, through the above three commands, forward the 22 port of the virtual machine to the 2222 port of the host.
For another example, I installed the apache2 server on the virtual machine debian, using port 80, which is mapped to port 80 of the host. Use the command below.
'C:\Program Files\innotek VirtualBox\VBoxManage.exe' setextradata 'debian' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/Protocol' TCP
'C:\Program Files\innotek VirtualBox\VBoxManage. exe' setextradata 'debian' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/GuestPort' 80
'C:\Program Files\innotek VirtualBox\VBoxManage.exe' setextradata 'debian' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/HostPort' 80
Note: For the setting to take effect, please close VirtualBox and then Running the virtual machine, I installed VirtualBox on winxp, installed debian 4.02r in the virtual machine, the virtual machine name is debian, and installed apache2 php5 mysql-server, browse http://localhost with IE on the host, and successfully forwarded to the apache2 web server on the virtual machine debian

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326520934&siteId=291194637