This time, my CentOS can't ping www.baidu.com again (gateway configuration)

When we ensure that the IP addresses of the host and the virtual machine are on the same network segment, and we use the virtual machine to ping the host, and the host can ping the virtual machine, the virtual machine cannot ping the external network, because It involves cross-network access, so we should focus on the configuration of the gateway!
First of all, let's briefly understand what a gateway is:
everyone knows that when going from one room to another, one must pass through a door. Similarly , sending information from one network to another must pass through a "gateway", which is a gateway. As the name implies, a gateway is a "gateway" that connects one network to another.  According to different classification standards, there are many kinds of gateways. The gateway in the TCP/IP protocol is the most commonly used, and the "gateway" we talk about here refers to the gateway under the TCP/IP protocol.

Since
we are using the NAT mode, there is no need to go through the gateway for the network communication between the host and the virtual machine, because we are in the "same room", but when the virtual machine communicates with the outside, it must go through the host. The address of the gateway.

This is because

When using NAT mode network connection, VMware will establish a separate private network on the host to communicate with each other between the host and the virtual machine. The request data "package" sent by the virtual machine to the external network will be handed over to the NAT network adapter with a "special mark" and forwarded in the name of the host . The response data "package" returned by the external network will also be received by the host first , and then The NAT network adapter is identified according to the "special mark" and forwarded to the corresponding virtual machine. Therefore, the virtual machine does not need to have its own IP address in the external network. From the perspective of the external network, the virtual machine and the host are sharing an IP address. By default, external network terminals cannot access the virtual machine.

but

If the host in network A finds that the destination host of the data packet is not in the local network, it forwards the data packet to its own gateway, and then forwards the data packet to the gateway of network B, and the gateway of network B forwards it to a certain network B host. Therefore, only by setting the IP address of the gateway, the TCP/IP protocol can realize the mutual communication between different networks.

Here's what actually happened to me:

The host pings the virtual machine:
 The virtual machine pings the host:

But the virtual machine can't ping Baidu (external network)

 This shows that there is no problem with the ip addresses of the two machines. The problem lies in the gateway configuration:
when I checked the configuration of the local gateway for the first time, I found that there was nothing in the default gateway, no artificial configuration, and it was empty.

I believe that many people follow the online tutorial to change the configuration file in the virtual machine when encountering the situation of pinging Baidu: 
vim /etc/sysconfig/network-scripts/ifcfg-ens33 , but many of them are unsuccessful. In fact, this The prerequisite is to ensure that the gateway of the virtual machine is consistent with the gateway of the host!

So let's manually configure the default gateway address of the host

And synchronously change the gateway configuration of the virtual machine network card

The two gateways are consistent!
Save and exit, and restart the network card: systemctl restart network
Try to ping Baidu again:

 

 

Guess you like

Origin blog.csdn.net/weixin_57535055/article/details/129159920