How to Configure a Static IP Address on CentOS 8 or RHEL 8

This post will guide you how to configure a static IP address for a network interface using Network Scripts on CentOS Linux 8 or RHEL Linux 8. How do I set a static IP address with Nmtui or Nmcli Tool on CentOS or Red Hat Enterprise Linux 8 system.

From the RHEL 8 releaes notes, you should know that Network Scripts is deprecated, and you need to use NetworkManager to control all network connections. And Network service is also replaced with NetworkManager service. So you need to restart NetworkManager.service after modified network confgiuration. The below will show you three methods to configure a static IP address on CentOS Linux 8 or RHEL Linux 8.

redhat 8 logo

Configuring Static IP Address Using Network Scripts


You can still use the old way to modify configuration file of network interface under /etc/sysconfig/network-scripts/ifcfg-*, just do the following steps:

#1 List all network interfaces to find which one interface you want to set a static ip address. type:

$ ip a show

Outputs:

[root@rhel8-devops ~]# ip a show
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:16:3e:0b:ec:35 brd ff:ff:ff:ff:ff:ff
inet 192.147.27.97/22 brd 192.147.27.255 scope global dynamic noprefixroute eth0
valid_lft 5432sec preferred_lft 5432sec
inet6 fe80::f39:2ad0:af05:7b10/64 scope link noprefixroute
valid_lft forever preferred_lft forever

From the above outputs, you should know which network interface that you want to use. At this example, we can only choose eth0 network interface.

Note: To list more detailed information about eth0 network interface, you can execute the following command:

$ sudo nmcli device show eth0

Outputs:

[root@rhel8-devops ~]# nmcli device show eth0
GENERAL.DEVICE: eth0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:16:3E:0B:EC:35
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eth0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/3
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.147.27.97/22
IP4.GATEWAY: 192.147.24.1
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 192.147.24.1, mt = 100
IP4.ROUTE[2]: dst = 192.147.24.0/22, nh = 0.0.0.0, mt = 100
IP4.DNS[1]: 192.147.26.60
IP4.DOMAIN[1]: us.oracle.com
IP6.ADDRESS[1]: fe80::f39:2ad0:af05:7b10/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255

#2 You need to edit the configuration file called ifcfg-eth0 under /etc/sysconfig/network-scripts/ directory. If this file does not exist, create it. Using vi/vim text editor to open it, type:

$ vim /etc/sysconfig/network-scripts/ifcfg-eth0

Changing the file content as below:

TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth0"
IPADDR="192.147.27.97"
NETMASK="255.255.255.0"
GATEWY=192.147.27.1
DEVICE="eth0"
ONBOOT="yes"

Save and close the file.

Note: if you want to configure a dynamic IP address using DHCP , you can use the following configuration file for eth0:

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="eth0"
UUID="dd40ea41-cfbe-4ada-917e-10de8880b023"
DEVICE="eth0"
ONBOOT="yes"

#3 restart the Network service on your RHEL or CentOS 8, type:

$ systemctl restart NetworkManager.service

Or

$ nmcli connection reload

Or

$ nmcli con down eth0
$ nmcli con up eth0

#4 you can use ip command to check the current IP address for eth0 interface, type:

$ ip a show eth0

Configuring Static IP Address Using Nmtui


You can also use another tool called Nmtui to configure static IP address for a specific network interface on RHEL 8 or CentOS. This tool is a text user interface for interacting with NetworkManager. You can use it to adding, modifying, or deleting network connections. For example, you wish to configure a static IP address for eth0 using Nmtui, just type:

$ sudo nmtui edit eth0

Then the Edit Connection Text User Interface will appear. Select Manual from the drop down list of the IPv4 CONFIGURATION.

set static ip address cento8 -1

Click Show to display Addresses, Gateway options, Type a static ip address in Addresses text box, and also type a gateway address. Press Ok button.

set static ip address cento8 -2

Configuring Static IP Address Using Nmcli


The default RHEL 8 or CentOS 8 comes with a new version of the ifup and ifdown scripts which call nmcli. And nmcli is a command line interface of NetworkManager. so it also can be uesd to get network information or configuring network interface.

To configure a static IP address for eth0 interface, just type the following command:

$ sudo nmcli con mod eth0 ipv4.address 192.168.1.34/24 
$ sudo nmcli con mod eth0 ipv4.gateway 192.168.1.1
$ sudo nmcli con mod eth0 ipv4.method manual
$ sudo nmcli con mod eth0 ipv4.dns "192.168.2.1"
$ sudo nmcli con up eth0

Those above changes will also be saved in /etc/sysconfig/network-scripts/ifcfg-eth0 file.

Conclusion


You should know that how to configure a static IP address for a network interface on CentOS 8 or Red Hat Enterprise Linux 8. And you also know how to restart network service on CentOS 8 or RHEL 8.

You might also like:

Sidebar



back to top