Linux configuration static IP



Whether it is a separate Linux or a Linux on a virtual machine, the default is to use DHCP to dynamically allocate IP:

When the machine is started, DHCP assigns an IP address from the specified IP address range. The IP assigned may be different each time you start.


The IP address of tomcat, nginx, mysql, redis and other servers will change, which is definitely not good. The IP address of the server is dead:

  • Write the IP address of Tomcat node in the load balancer of nginx
  • Write the IP address of nginx, MySQL and redis server in the configuration file of the project

The server IP will change, so it must be changed to static IP.






Configure static IP

(1) Switch to the directory of network management first

cd /etc/sysconfig/network-scripts



Take a look at the network configuration file:

ls -l

Take a look at the file name of the configuration file. The file name is related to the network card model, – followed by the network card model. Everyone’s may have different names, so you need to CD it to the specified directory first, and LS takes a look at the file name.

Also note whether the currently logged in account has the write permission of this file, if not, switch to the user with W permission, or Chmod modifies the file permission.



(2) Edit the file

vim ifcfg-ens160



Bootproto defaults to DHCP, changes to static, and uses static IP.Configure IP, subnet mask, gateway and DNS server at the back.

The preceding values can be quoted or not, and the following IP addresses must not be quoted.Two DNS servers can be configured: DNS 1 and DNS 2. 1 is the first choice and 2 is the spare wheel.



(3) Restart the network card to take effect

Before CentOS 8, any of the following two instructions can be used to restart the network service:

service network restart 


systemctl restart network.service



CentOS 8 discards network.service and replaces it with NetworkManager. If you use the above two instructions, an error will be reported: failed to restart network.service: unit network.service not found

To use the commands for NetworkManager:

nmcli c reload

Nm is the network manager and cli is the client.

This method is only to reload the network card configuration, not to restart the network card. If ifconfig sees that the IP has not been changed (not yet effective), you can use the following method to restart the network card.


There are two ways to operate the hardware directly. They are general and can be used in any version:

Ifconfig ens160 down - uninstall the ens160 network card
Ifconfig ens160 up enable ens160 network card
Ifdown ens160? Uninstall the ens160 network card
Ifup ens160 - enable ens160 network card

The network card model should be changed to its own.


Of course, the easiest and rudest way is to restart Linux.



How to view the network card model?

1. The file we edited just now ifcfg-ens160, followed by the network card model






If you are using Linux in VMware and you are using a bridge network, you need to make some settings:




By default, only host and NAT are enabled. Click “change settings” to add the bridge.

Dynamic IP does not need to be added here, but static IP does.



Select VMnet0








If a machine (mainly a notebook) connects the company’s network, the home network, the school network, the mobile hotspot, this network, and that network,

The connected network has changed. You need to reconfigure the above addresses, or you will not be able to connect to the Internet.


Static IP is suitable for the situation of using a certain network all the time, such as servers, which always use the network of computer rooms.