How to configure Static IP address in CentOS

Share this article

We know as a system or network administrator we have to deal with different kinds of Operating systems and Linux OSes are majority in them. Setting up networks in these systems is crucial. Therefore, in this article, we will configure static IP addresses in CentOS. Similarly, like How to add a Static IP address in Ubuntu 20.04 server we will show two different strategies here one with CLI and which is not persistent and the other with NetworkManager or network-scripts which is persistent through even reboot.

IP address to assign

In CentOS interface name is typically start with en, in our case, we will assign the following network configuration to the  enp1s0 interface (see the next section to know what all network devices are present in our environment).

  • IP address: 192.168.1.10
  • Network Subnet Mask : 255.255.255.0
  • Network Gateway192.168.1.1

List network interface

Before assigning the above network configuration to our CentOS machine’s network interface we need to make sure that the interface is present there. So first let’s identify the name of our network interface which we want to configure, to check or list all the interfaces in CentOS use the following command.

ip a

ip a command list all the interfaces, output list two interfaces in our environment lo and enp1s0. lo is loopback interface or local interface which have 127.0.0.1 IP address. enp1s0 is the only actual interface that is responsible for network packet transfer from or into the system. Also, enp1s0 has 192.168.1.10 IP address and we will change this IP address to the above-mentioned IP address.

[root@test ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:25:35:ac brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::1cf8:1245:9513:6700/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@test ~]# 

Configuring Static IP through CLI

Configuring Static IP configuration from the command line is very simple, We will use ip addr commands to set the IP and other configurations. See the following code section:

sudo ip addr add 192.168.1.10/24 dev enp1s0
sudo ip route add default via 192.168.1.1 dev enp1s0

Verification

After running the above command we have set up the IP address on the enp1s0 interface, now lets check the interface:

[root@test ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:25:35:ac brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
     inet6 fe80::1cf8:1245:9513:6700/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@test ~]# 

The above output shows that the configuration is assigned properly and to test the connectivity to network ping the other IP addresses.

Configuring Static IP with NetworkManager

As we have already mentioned at the start of our article the IP assignment in the previous section is not persistent and after reboot, we will not see the same configuration at the interface. Therefore, To make the entry persistent use the network through network-scripts.

NetworkManager or network-scripts related Configuration files can be found in the /etc/sysconfig/network-scripts/ directory. Create the file with the interface name inside /etc/sysconfig/network-scripts/.

We will create the file ifcfg-interface_name inside /etc/sysconfig/network-scripts/, in our case, the file name is ifcfg-enp1s0.

Now add the following entry into the file /etc/sysconfig/network-scripts/ifcfg-enp1s0

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp1s0"
DEVICE="enp1s0"
ONBOOT="yes"
IPADDR=192.168.1.10
PREFIX=24
GATEWAY=192.168.1.1
DNS1=192.168.1.1

Some of the options are optional and maybe not usable in normal scenarios.

Once done, save the file and apply the changes by running the following command:

sudo ifup enp1s0

Verification

Use the ipconfig -a or ip a command to list all interfaces.

[root@test ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:25:35:ac brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
     inet6 fe80::1cf8:1245:9513:6700/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@test ~]# 

Summary

Article presents two different ways of network configuration in CentOS. Configuring the network is not limited to these two methods. There are several ways to do that but for sake of simplicity, we only discussed through the command line and with the NetworkManager/network-scripts.

In case you face any issues don’t hesitate to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *