15. February 2022

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

By H. Cemre Günay

There are many cases in which we may want to set a static IP for a network interface. In Red Hat Enterprise Linux 8, network interfaces are managed by the NetworkManager daemon, so to change a network interface settings we must somehow interact with it. In this tutorial we will see how can we set a static IP address, gateway and dns server for a NIC.

The first thing we want to do is to find the interface we want to modify. To list all the interfaces on our system, we can use the ip addr command:

In the output above, which is the result of running the command on my rhel8 virtualized machine, we can see two interfaces: lo and ens192.

The first is a “virtual” interface, which is used by the system to “talk” with itself. The second one is the one which interests us, which we want to change it.

So let’s start with modifying interface configuration file manually. For each network interface managed by the NetworkManager daemon, a configuration file is created inside the /etc/sysconfig/network-scripts directory.

The name of the file is composed by the ifcfg- prefix plus the name of the interface. If we inspect the file related to our NIC, we can see its actual setup:

The BOOTPROTO option is set to dhcp: the option sets the protocol to use at boot to set the IP address of the interface. The possible options to use are:

  • none – No protocol should be used
  • bootp – Use the bootp protocol
  • dhcp – Use the dhcp protocol

Since we want to set a static IPv4 address, we want to change the value of BOOTPROTO to none and configure our IP, route prefix, gateway and dns server statically. We can accomplish this by using respectively the IPADDR, PREFIX, GATEWAY and DNS options. Since many dns servers can be specified, the DNS option must be reported together with a progressive number, starting from 1. After the needed modifications, our file should look like this:

We set our static IP to 192.168.34.189 and set our gateway and dns servers. Now, to make our changes effective we must put down and up again the network interface. Be aware that this will disrupt existent ssh connections via said interface:

sudo nmcli connection down ens192 && sudo nmcli connection up ens192

By running the ip addr command again we can verify the IP has changed:

IP addr|grep ens192|grep inet

Output:

inet 192.168.34.189/24 brd 192.168.34.255 scope global nopref ixroute ens192

There is a second way to set static IP with nmtui. Other than changing the parameters of a network interface by modifying its file manually or by using the nmcli utility, we can also control NetworkManager by using a text user interface. To launch it we just invoke nmtui in our terminal:

sudo nmtui

We Select Edit a connection and then the name of the interface we want to manipulate; we then proceed in changing the desired values. For example:

After we are satisfied, we select OK and press enter: we will be taken back to the interface selection menu. This time we select back, and then choose quit to exit. To apply the settings we need to reload the interface connection:

sudo nmcli connection down ens192 && sudo nmcli connection up ens192

That’s it, if you have any questions please leave it in the comments. 🙂