Featured image of post Setting a Static IP Address for Network Interface in CentOS

Setting a Static IP Address for Network Interface in CentOS

In CentOS, you can set a network interface to a static IP address by following these steps: cd /etc/sysconfig/net…

In CentOS, you can set a network interface to a static IP address by following these steps:

  1. Open the /etc/sysconfig/network-scripts directory and locate the network interface configuration file you want to modify. For example, for eth0, find the file named ifcfg-eth0.
1
2
cd /etc/sysconfig/network-scripts
ls ifcfg-*

Setting a Static IP Address in CentOS

  1. Backup the file and open it with a text editor. For instance, you can use the vi editor to open the ifcfg-eth0 file:
1
2
cp ifcfg-eth0 ifcfg-eth0.bak
vi ifcfg-eth0
  1. Set the BOOTPROTO attribute to static to indicate that a static IP address will be used, and add the following attributes:
1
2
3
4
5
IPADDR=192.168.0.100        # Set the IP address for the network interface
NETMASK=255.255.255.0       # Set the subnet mask for the network interface
GATEWAY=192.168.0.1         # Set the gateway IP address
DNS1=8.8.8.8                # Set the primary DNS server IP address
DNS2=8.8.4.4                # Set the secondary DNS server IP address (optional)

For example, the modified file content should look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.0.100
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
# For DNS addresses inside China, you can refer to the following article: https://bmzhp.com/knowledge/164

Setting a Static IP Address in CentOS

  1. Save the modified file and restart the network service.
1
systemctl restart network
  1. Use the ip addr command to check if the network interface is using the static IP address.
1
ip addr

These are the complete steps to set a static IP address for a network interface in CentOS. I hope this method helps you successfully configure the static IP address for your network interface.

Licensed under CC BY-NC-SA 4.0