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:
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:
For example, in our case:
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:
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 "220.127.116.11"
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:
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="18.104.22.168"