[Linux Series] Detailed explanation of vmware virtual machine network configuration

unoriginal

Original address [1]

First blog address [2]

Series article address [3]


vmware provides us with three network working modes, which are: Bridged (bridge mode), NAT (network address translation mode), and Host-Only (host-only mode).

Open the vmware virtual machine. We can see VMnet0 (bridge mode), VMnet1 (host only mode), and VMnet8 (NAT mode) in the "Virtual Network Editor" under "Edit" in the option bar. So what are these? What's the role? In fact, the VMnet0 we see now represents the virtual switch used in bridge mode; VMnet1 represents the virtual switch used in host-only mode; VMnet8 represents the virtual switch used in NAT mode.

Virtual Network Editor
Virtual Network Editor

At the same time, there are two virtual network cards corresponding to the host: VMware Network Adapter VMnet1 and VMware Network Adapter VMnet8, which operate in host-only mode and NAT mode respectively. We can see these two virtual network cards in "Network Connections". If these two are uninstalled, you can click "Restore Default Settings" in the "Virtual Network Editor" under "Edit" in vmware to reset them. Virtual network card restoration.

Internet connection
Internet connection

When friends see this, they must have questions, why is there no VMware Network Adapter VMnet0 virtual network card on the real machine? So next, let's take a look at why this is.

1. Bridged (bridge mode)

What is bridge mode? The bridge mode is to use the virtual bridge to communicate between the host network card and the virtual network card of the virtual machine. Under the function of bridging, it is similar to virtualizing a physical host into a switch. All virtual machines set up by the bridge are connected to an interface of the switch. The physical host is also plugged into the switch, so all network cards and network cards under the bridge are connected. It is in exchange mode and can access each other without interfering. In bridge mode, the virtual machine IP address needs to be in the same network segment as the host. If networking is required, the gateway and DNS need to be consistent with the host network card. Its network structure is shown in the figure below:

bridge mode network structure
bridge mode network structure

Next, let's do the actual operation of how to set up bridge mode.

First, after installing the system, before starting the system, click "Edit virtual machine settings" to set the network card mode.

Edit virtual machine settings
Edit virtual machine settings

Click on "Network Adapters", select "Bridged Mode", and then "OK"

Network adapter settings
Network adapter settings

Before entering the system, let's confirm the host's ip address, gateway, DNS and other information.

Host network information
Host network information

Then, enter the system to edit the network card configuration file, the command is vi /etc/sysconfig/network-scripts/ifcfg-eth0

Edit network card configuration file
Edit network card configuration file

Add the following content:

Network card configuration content
Network card configuration content

After editing, save and exit, then restart the network card of the virtual machine, and use the ping command to ping the external network ip to test whether it can be connected to the Internet.

Test networking
Test networking

The external network ip can be pinged, which proves that the bridge mode is set successfully.

Is the communication between the host and the virtual machine normal? Let's use remote tools to test it.

Remote connection test
Remote connection test

Communication between the host and the virtual machine is normal.

This is the setting steps of the bridge mode, I believe you should have learned how to set the bridge mode. Bridge mode is simple to configure, but if your network environment is short of IP resources or has strict IP management, then bridge mode is not suitable. If this is the case, how can we solve it? Next, let's get to know another network mode of vmware: NAT mode.

2. NAT (Address Translation Mode)

刚刚我们说到,如果你的网络 ip 资源紧缺,但是你又希望你的虚拟机能够联网,这时候 NAT 模式是最好的选择。NAT 模式借助虚拟 NAT 设备和虚拟 DHCP 服务器,使得虚拟机可以联网。其网络结构如下图所示:

NAT mode network structure
NAT模式网络结构

在 NAT 模式中,主机网卡直接与虚拟 NAT 设备相连,然后虚拟 NAT 设备与虚拟 DHCP 服务器一起连接在虚拟交换机 VMnet8 上,这样就实现了虚拟机联网。那么我们会觉得很奇怪,为什么需要虚拟网卡 VMware Network Adapter VMnet8 呢?原来我们的 VMware Network Adapter VMnet8 虚拟网卡主要是为了实现主机与虚拟机之间的通信。在之后的设置步骤中,我们可以加以验证。

首先,设置虚拟机中 NAT 模式的选项,打开 vmware,点击“编辑”下的“虚拟网络编辑器”,设置 NAT 参数及 DHCP 参数。

Virtual Network Editor Settings
虚拟网络编辑器设置
NAT parameter settings
NAT参数设置
DHCP parameter settings
DHCP参数设置

将虚拟机的网络连接模式修改成 NAT 模式,点击“编辑虚拟机设置”。

Edit virtual machine settings
编辑虚拟机设置

点击“网络适配器”,选择“NAT 模式”

Network adapter settings
网络适配器设置

然后开机启动系统,编辑网卡配置文件,命令为 vi /etc/sysconfig/network-scripts/ifcfg-eth0

Edit network card configuration file
编辑网卡配置文件

具体配置如下:

Network card configuration content
网卡配置内容

编辑完成,保存退出,然后重启虚拟机网卡,动态获取 ip 地址,使用 ping 命令 ping 外网 ip,测试能否联网。

Test networking
测试联网

之前,我们说过 VMware Network Adapter VMnet8 虚拟网卡的作用,那我们现在就来测试一下。

Test communication between virtual machine and host
测试虚拟机与主机通信
Test host and virtual machine communication
测试主机与虚拟机通信

如此看来,虚拟机能联通外网,确实不是通过 VMware Network Adapter VMnet8 虚拟网卡,那么为什么要有这块虚拟网卡呢?

之前我们就说 VMware Network Adapter VMnet8 的作用是主机与虚拟机之间的通信,接下来,我们就用远程连接工具来测试一下。

Test host and virtual machine communication
测试主机与虚拟机通信

然后,将 VMware Network Adapter VMnet8 启用之后,发现远程工具可以连接上虚拟机了。

那么,这就是 NAT 模式,利用虚拟的 NAT 设备以及虚拟 DHCP 服务器来使虚拟机连接外网,而 VMware Network Adapter VMnet8 虚拟网卡是用来与虚拟机通信的。

三、Host-Only(仅主机模式)

Host-Only 模式其实就是 NAT 模式去除了虚拟 NAT 设备,然后使用 VMware Network Adapter VMnet1 虚拟网卡连接 VMnet1 虚拟交换机来与虚拟机通信的,Host-Only 模式将虚拟机与外网隔开,使得虚拟机成为一个独立的系统,只与主机相互通讯。其网络结构如下图所示:

Host-Only mode network structure
Host-Only模式网络结构

通过上图,我们可以发现,如果要使得虚拟机能联网,我们可以将主机网卡共享给 VMware Network Adapter VMnet1 网卡,从而达到虚拟机联网的目的。接下来,我们就来测试一下。

首先设置“虚拟网络编辑器”,可以设置 DHCP 的起始范围。

Virtual Network Editor Settings
虚拟网络编辑器设置

设置虚拟机为 Host-Only 模式。

Virtual machine network adapter settings
虚拟机网络适配器设置

开机启动系统,然后设置网卡文件。

Edit network card configuration file
编辑网卡配置文件

保存退出,然后重启网卡,利用远程工具测试能否与主机通信。

Test host and virtual machine communication
测试主机与虚拟机通信

主机与虚拟机之间可以通信,现在设置虚拟机联通外网。

Virtual Network Editor Settings
虚拟网络编辑器设置

We can see a prompt in the picture above to force the IP of VMware Network Adapter VMnet1 to be set to 192.168.137.1. Then, next, we need to modify the DHCP subnet and starting address of the virtual machine and click "Virtual Network" editor"

Virtual Network Editor Settings
Virtual Network Editor Settings

Reconfigure the network card and use the VMware Network Adapter VMnet1 virtual network card as the route of the virtual machine.

Virtual machine network adapter settings
Virtual machine network adapter settings

Restart the network card, and then use the remote tool to test whether it can connect to the external network and communicate with the host.

Test networking and communications
Test networking and communications

The test results prove that the virtual machine can be connected to the external network.

References

[1]

Original address: https://blog.51cto.com/u_15169172/2710721

[2]

First blog address: https://blog.zysicyj.top/

[3]

Series article address: https://blog.zysicyj.top/categories/technical articles/backend technology/series articles/Linux/

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/njpkhuan/article/details/132632200