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

Question: How to configure Static IP address on RHEL 8 / CentOS 8? This guide will show you different ways to configure a static IP address when available on RHEL 8 and CentOS 8.

An IP address is an address that uniquely identifies a device on an IP network. In order for two computers to communicate with each other and share data, they need to set their IP addresses to their network interfaces. The IP address can be assigned manually (static IP address) or it can be assigned via DHCP.

DHCP IP address assignment is commonly used on desktops, laptops and workstations. If the IP is changed, assigning an IP address to the server through DHCP can have a serious impact.

Method 1: Edit the network configuration file

The first method we will consider is to manually change 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 with enp7s0. We will configure the second interface enp7s0 Static IP address. The network interface configuration file will be placed in /etc/sysconfig/network-scripts/ifcfg-enp7s0.

Create this file (if it doesn’t exist), replace enp7s0 With your network interface name.

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

Edit the following to suit your network addressing scheme and paste it 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 : Make sure the interface is up and the system is up
  • NM_CONTROLLED = Yes: Allow the interface to be managed by the network manager
  • BOOTPROTO = None: Use specified network information as an interface
  • IPV6INIT = No: Disable IPv6 initialization for this network interface

After making changes, restart the NetworkManager service

sudo systemctl restart NetworkManager

You should see the new IP address assigned to the 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: Use nmcli to set a static IP address on RHEL 8

Nmcli is a command line tool for controlling NetworkManager and reporting network status. Using this command line tool, you can set RHEL 8 static IP address with several 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 the connection.

# nmcli connection  down  75faabc3-f62c-9770-bfe5-9996ddb77891# nmcli connection  delete 75faabc3-f62c-9770-bfe5-9996ddb77891Connection 'System enp7s0' (75faabc3-f62c-9770-bfe5-9996ddb77891) successfully deleted.

Replace enp7s0 Enter your interface name and IP address information and your actual value in the following code 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 to the interface.

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

of autoconnect yes Equivalent to ONBOOT=yes

Add IP address and network subnet

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

Set the default gateway of the interface

nmcli connection modify ${INT} ipv4.gateway ${GW}

Setting up two DNS servers provided earlier.

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

Check connection

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

NetworkManager will populate the configuration file for you /etc/sysconfig/network-scripts/ table of Contents. My example is 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 It has ncurses GUI, but it is not recommended to set a static IP address on RHEL / CentOS based systems.

in conclusion

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

label:

  • How to configure a static IP address on RHEL 8 / CentOS 8
  • Setting a static IP address on RHEL 8 / CentOS 8
  • DHCP to static IP address on RHEL 8 / CentOS 8

Other RHEL 8 articles:

Sidebar