How to configure static IP address on RHEL 8 / CentOS 8

Posted on 100 views

Question: How can I configure a static IP address on RHEL 8 / CentOS 8?. This guide will show you different ways of configuring a static IP address on RHEL 8 and CentOS 8 once available.

An IP address is an address used to uniquely identify a device on an IP network. For two computers to communicate with each other and share data, they need to have IP addresses set to their network interface. An IP address can be assigned manually (static IP Address) or via DHCP.

DHCP IP addressing assignment is commonly used for Desktops, Laptops, and Workstations. Assigning an IP address through DHCP to a server can cause drastic effects if the IP changes.

Method 1: Edit Network configuration file

The first method we will consider is manually making changes to the network interface configuration file. My RHEL 8 server has two network interfaces.

# ip link show
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 2: enp1s0:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
     link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff
 3: enp7s0:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
     link/ether 52:54:00:83:0b:b9 brd ff:ff:ff:ff:ff:ff

These are enp1s0 and enp7s0. We will configure the second interface enp7s0 static IP address. This network interface configuration file will be placed under /etc/sysconfig/network-scripts/ifcfg-enp7s0.

Create this file if it doesn’t exist, replacing enp7s0 with your network interface name.

sudo vi /etc/sysconfig/network-scripts/ifcfg-enp7s0

Edit the following contents to fit your network addressing scheme and paste into the file.

DEVICE=enp7s0
TYPE=Ethernet
NM_CONTROLLED=yes
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.121.188
NETMASK=255.255.255.0
GATEWAY=192.168.121.1
DNS1=8.8.8.8
DNS2=4.2.2.2
IPV6INIT=no
USERCTL=no
PEERDNS=yes

Parameters/options used:

  • ONBOOT=yes : Ensure the interface comes up upom system start
  • NM_CONTROLLED=yes: Allow interface to be managed by Network Manager
  • BOOTPROTO=none: Use specified network information for the interface
  • IPV6INIT=no: Disable IPv6 initialization for this network interface

Restart your NetworkManager service after making the change

sudo systemctl restart NetworkManager

You should see the new IP address assigned to your network interface.

# ip addr show enp7s0
 3: enp7s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 52:54:00:83:0b:b9 brd ff:ff:ff:ff:ff:ff
     inet 192.168.121.188/24 brd 192.168.121.255 scope global noprefixroute enp7s0
        valid_lft forever preferred_lft forever
     inet6 fe80::5054:ff:fe83:bb9/64 scope link 
        valid_lft forever preferred_lft forever

Method 2: Set Static IP Address on RHEL 8 using nmcli

Nmcli is a command-line tool used to control NetworkManager and for reporting network status. With this command line tool, you can set RHEL 8 static IP address with few commands.

Check available connections.

# nmcli connection show
 NAME           UUID                                  TYPE      DEVICE 
 enp1s0         498869bb-0d88-4a4c-a83a-c491d1040b0b  ethernet  enp1s0 
 System enp7s0  75faabc3-f62c-9770-bfe5-9996ddb77891  ethernet  enp7s0

You can delete and recreate connection.

# nmcli connection  down  75faabc3-f62c-9770-bfe5-9996ddb77891
# nmcli connection  delete 75faabc3-f62c-9770-bfe5-9996ddb77891

Connection 'System enp7s0' (75faabc3-f62c-9770-bfe5-9996ddb77891) successfully deleted.

Replace enp7s0 with your interface name and IP address information with your actual values in the following snippet.

INT="enp7s0"
IP_SUBNET="192.168.121.188/24"
GW="192.168.121.1"
DNS1="8.8.8.8"
DNS2="4.2.2.2"

Add a new connection for the interface.

nmcli connection add type ethernet autoconnect yes con-name $INT ifname $INT

The autoconnect yes is equivalent to ONBOOT=yes

Add IP address and network subnet

nmcli connection modify $INT ipv4.addresses $IP_SUBNET ipv4.method manual

Set default gateway for the interface

nmcli connection modify $INT ipv4.gateway $GW

Set two DNS servers provides earlier.

nmcli connection modify $INT ipv4.dns $DNS1
nmcli connection modify $INT +ipv4.dns $DNS2

Check connections

# nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
enp1s0  498869bb-0d88-4a4c-a83a-c491d1040b0b  ethernet  enp1s0 
enp7s0  9f9dba41-b532-414c-b531-0c7978e0eb6b  ethernet  enp7s0

NetworkManager will populate configuration file for you to /etc/sysconfig/network-scripts/ directory. My example is as shown below.

# cat  /etc/sysconfig/network-scripts/ifcfg-enp7s0
 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=enp7s0
 UUID=9f9dba41-b532-414c-b531-0c7978e0eb6b
 DEVICE=enp7s0
 ONBOOT=yes
 IPADDR=192.168.121.188
 PREFIX=24
 GATEWAY=192.168.121.1
 DNS1=8.8.8.8
 DNS2=4.2.2.2

There is a tool called Nmtui which has ncurses GUI but it is not a recommended way of setting static IP address on RHEL / CentOS based systems.

Conclusion

You should now have a static IP address configured on your RHEL 8 / CentOS 8 server. Stay connected for more in-depth and hands-on guides for Linux and Unix systems.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.