Setting up a Static IP Address on CentOS / RHEL

This guide will show you different ways to set up a static IP address on your CentOS/RHEL Linux server.

Before you start

First of all we need to get the network interface information:

ip addr

We will have a result similar to this:

We could find “eth0” instead of “enp0s3

1. How to Configure Static IP Using Network Scripts Manually

The first way to configure a static IP is by editing the network interface using this command line:

nano /etc/sysconfig/network-scripts/ifcfg-(interface-name)

For example, in our case:

nano /etc/sysconfig/network-scripts/ifcfg-enp0s3

Then edit the file in the following way:

TYPE="Ethernet"
BOOTPROTO="none"
NAME="enp0s3"
IPADDR="<ip-address>"
NETMASK="<netmask>"
GATEWAY="<gateway>"
DEVICE="enp0s3"
ONBOOT="yes"

Be sure to enter your data in ip address, netmask and gateway. For example “ip address: 192.168.20.150, netmask 255.255.255.0, gateway 192.168.20.1” Then restart the NetworkManager with:

sudo systemctl restart NetworkManager

To check the new IP address use the following command:

ip a show enp0s3

The result will be something like the screen above.

2. How to Configure Static IP Using Nmtui Tool

Another way to set a static IP is to use NMTUI Tool, which is more “user friendly” with a text based graphic interface. To open it just type:

nmtui

After launching the program an interface will appear:

We are going to edit our connection:

After selecting the right connection, an edit panel will appear:

Here we can set our configuration. For example we set IP address to 192.168.20.160, and the Gateway to 192.168.20.1

To save the changes scroll down to the end of the page and select OK.

Now we firstly need to deactivate the active connection

And then, to reload the interface with the new settings, choose <Activate> after selecting it:

Then select <Back> to return to the main menu and then select “Quit” to exit.

Quit Network Manager

Now we can easily verify that the new IP address settings have been applied with the following command:

ip a show enp0s3

3. How to Configure Static IP Using Nmcli Tool

NMCLI is a command we can use to obtain information or configure a network interface.

To set a static IP address, here is how to proceed.

Set the IP address for interface “enp0s3”:

sudo nmcli con mod enp0s3 ipv4.addresses < ip-address >

for example: 192.168.20.170/24

Set the gateway on RHEL 8:

sudo nmcli con mod enp0s3 ipv4.gateway < gateway >

for example: 192.168.20.1

Then we need to inform the interface that it is using manual configuration (not dhcp):

sudo nmcli con mod enp0s3 ipv4.method manual

Now it’s time to configure DNS:

sudo nmcli con mod enp0s3 ipv4.dns "8.8.8.8"

At the end we reload the interface configuration:

sudo nmcli con up enp0s3

All the changes will be saved in /etc/sysconfig/network-scripts/ifcfg-<name>

Here is an example:

cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE="Ethernet"
BOOTPROTO="none"
NAME="enp0s3"
IPADDR="192.168.20.170"
NETMASK="255.255.255.0"
GATEWAY="192.168.20.1"
DEVICE="enp0s3"
ONBOOT="yes"
PROXY_METHOD="none"
BROWSER_ONLY="no"
PREFIX="24"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="no"
UUID="3c36b8c2-334b-57c7-91b6-4401f3489c69"
DNS1="8.8.8.8"

Post your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

SELinux
DNS Server Installation Guide on CentOS 7 / RHEL 7 and CentOS 8 / RHEL 8 using dnsmasq