Linux (CentOS 7) configures static ip and ping fails to connect to the external network

foreword

In daily learning, if MySQL, nacos, redis and other middleware are installed in Linux. We may connect to MySQL through navicat/dataGrip, access nacos through ip:port/nacos, and connect to redis through ip:port. It can be seen that these are all related to the ip address of Linux, so once the Linux ip address changes after booting and restarting, we will need to modify the ip address one by one, which is extremely inconvenient.
We can configure the Linux ip address as a static ip to avoid the above problems.

Second, edit the network card configuration file

1. Configuration file address

It should be noted that the network card configuration file may be ifcfg-ens32 or ifcfg-ens33, whichever one you have can be edited directly.
Please add a picture description

vim /etc/sysconfig/network-scripts/ifcfg-ens32

Modify BOOTPROTO to static and ONBOOT to yes; add IPADDR, NETMASK, GATEWAY, DNS1, DNS2, DNS3
Please add a picture description
gateway and ip address, etc., which can be viewed through "Edit –> Virtual Network Editor –> NAT Settings" in VMware, as shown in the following figure:
insert image description here

BOOTPROTO=static            // 静态网址
ONBOOT=yes                  // 开机自启
IPADDR=192.168.160.188      // ip地址
NETMASK=255.255.255.0       // 子网掩码
GATEWAY=192.168.160.2       // 网关,vmware NAT模式在编辑--虚拟网络编辑器中选中NAT模式里点击NAT配置查看
DNS1=8.8.8.8
DNS2=114.114.114.114
DNS3=8.8.4.4

2. Restart the network service

systemctl restart network

2. Potential problems (the network cannot ping the host/external network)

Written at the top, the solution refers to: CentOS virtual machine linux cannot ping the external network solution after setting a static IP

  1. The gateway should correspond to
    the subnet ip and subnet mask in the virtual machine->edit->virtual network editor, so set the gateway GATEWAY to be consistent with the subnet ip except the last digit is 2, that is, 192.168.44.2. The subnet mask NETMASK is set to 255.255.255.0.

  2. Do not conflict with ip addresses
    Sometimes the ip settings will conflict, just modify the last bit, just try a few more.
    If you carefully check the picture & configuration above, you will find that the ip address in "Edit->Virtual Network Editor->NAT Settings" in VMware is 192.168.160.0, but I set it to 192.168.160.188, which solves the problem that neither the host nor the external network can be pinged.

  3. The domain name must be added
    After the above modification, I can already ping the host, but I still can’t ping the external network (www.baidu.com), so I try to add DNS2, and it works, so it is recommended to add both DNS1 and DNS2.

Summarize

Through this article, we know how to configure Linux static ip, and how to solve the problem of pinging the host and external network after configuration.

Guess you like

Origin blog.csdn.net/qq_37196265/article/details/129398570