There are many cases in which we may want to set a static IP for a network interface. In Red Hat Enterprise Linux 8, the network connections are managed by the NetworkManager daemon, so in this tutorial we see how we can perform such task by editing an interface file directly, by using a command line utility, nmcli, or via a text user interface, nmtui.

In this tutorial you will learn:
  • How to set a static IP address by directly editing an interface file
  • How to set a static IP address using the nmcli utility
  • How to set a static IP address using nmtui
interface-file-edited
The edited network interface file

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Red Hat Enterprise Linux 8
Software All the software mentioned in this tutorial is included in a minimal installation of Rhel8.
Other Permission to run command with root privileges.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Introduction

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 by using different three methods.

Identifying our interface

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:

$ ip addr
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.189/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0
       valid_lft 3004sec preferred_lft 3004sec
    inet6 fe80::9ab6:ba48:1ddd:a8e1/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


In the output above, which is the result of running the command on my rhel8 virtualized machine, we can see two interfaces: lo and enp1s0. 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, and currently its IPv4 address is 192.168.122.189: we want to change it.

Method 1 - 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:

$ cat /etc/sysconfig/network-scrIPts/ifcfg-enp1s0
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
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"
UUID="d5f41bf4-de0a-43b3-b633-7e2ec6212e58"
DEVICE="enp1s0"
ONBOOT="yes"

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:

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"
UUID="d5f41bf4-de0a-43b3-b633-7e2ec6212e58"
DEVICE="enp1s0"
ONBOOT="yes"
IPADDR=192.168.122.66
PREFIX=24
GATEWAY=192.168.122.1
DNS1=192.168.122.1


We set our static IP to 192.168.122.66 and set both our gateway and dns server to 192.168.122.1. 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 enp1s0 && sudo nmcli connection up enp1s0

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

IP addr|grep enp1s0|grep inet
inet 192.168.122.66/24 brd 192.168.122.255 scope global noprefix

Method 2 - using nmcli

Nmcli is a command line tool used to control NetworkManager. By using it we can, among other thins, change network settings for a specific interface. In our case we must use nmcli with the connection subcommand with the modify or the edit action. The first lets us change the settings directly, while the second launches the nmcli shell, where we can operate interactively.

Using nmcli directly

To change the IP address of the "enp1s0" interface with a direct nmcli command, we run:

$ sudo nmcli connection modify enp1s0 IPv4.address 192.168.122.66/24

Notice that we specified the routing prefix together with the IP using the so called CIDR notation (Classes Inter-Domain Routing). We use a similar syntax to change the gateway and the dns settings:

$ sudo nmcli connection modify enp1s0 IPv4.gateway 192.168.122.1
$ sudo nmcli connection modify enp1s0 IPv4.dns 192.168.122.1

Finally, we set the method to manual to avoid using any other boot protocol for the interface. This command sets the BOOTPROTO option to none in the interface configuration file:



$ sudo nmcli connection modify enp1s0 IPv4.method manual

Using nmcli shell

As we mentioned above, we can also use an interactive method to perform the same changes. To enter the nmcli shell we use the edit action, followed by name of the interface we want to edit:

$ sudo nmcli connection edit enp1s0
===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'enp1s0'

Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [.]' for detailed property descrIPtion.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, IPv4, IPv6, tc, proxy
nmcli>

The blinking cursor tells us the shell is accepting commands: we can type help to get a list of the available ones, print to see all the connection properties or describe followed by a setting and one of its properties (represented with a dot notation), to get a description of the latter. For example:

nmcli>
nmcli> describe IPv4.address

=== [addresses] ===
[NM property descrIPtion]
Array of IP addresses.

[nmcli specific descrIPtion]
Enter a list of IPv4 addresses formatted as:
  IP[/prefix], IP[/prefix],...
Missing prefix is regarded as prefix of 32.

Example: 192.168.1.5/24, 10.0.0.11/24

To change the address of our interface:

nmcli> set IPv4.address 192.168.122.66/24

The same principle is used to change the other properties:

nmcli> set IPv4.gateway 192.168.122.1
nmcli> set IPv4.dns 192.168.122.1
nmcli> set IPv4.method manual

To save the modifications we made, we need to issue one last command:

nmcli> save
Connection 'enp1s0' (d5f41bf4-de0a-43b3-b633-7e2ec6212e58) successfully updated.

To exit the nmcli shell just type quit and press enter:

nmcli> quit

Again, to make the changes effective, we should reload the interface with the same command we used before.

Method 3 - using 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:


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


$ sudo nmtui
nmtui-interface
The nmtui text user interface

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:

nmtui-change-settings
Editing connection settings with nmtui

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 enp1s0 && sudo nmcli connection up enp1s0

Conclusions

In this tutorial we saw three methods we can use to set a static IP address on Red Hat Enterprise Linux 8. We see what options we should change and what we should add in the interface configuration file when editing it manually, and how to perform the same changes using nmcli and nmtui, which are respectively a command line utility and a text user interface: both are used to control the NetworkManager daemon.

ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions