An IP Address is a unique numerical representation of a computer on a network. When you set up a text-only and GUI-less CentOS server, you may need to configure static IP on it. So, join this article to learn How to Configure Static IP on RHEL/CentOS 8. This guide will teach you three different methods to make you an expert in setting up the static IP address on your CentOS server. The three options you can use to set a static IP address include
nmtui utility, and editing an interface file directly. Let’s cut to the chase and move on with discovering the details.
Prerequisites to Configure Static IP on RHEL/CentOS 8
To let this content help you to achieve your purpose for configuring a Static IP on CentOS, provide all the below considerations and then, go on.
- A system running RHEL/CentOS 8.
- A non-root user with
Tutorial Configure Static IP on CentOS 8 Using 3 CMD
Since the NetworkManager daemon manages the network connections of RHEL 8 / CentOS 8, changing a Network Interface Settings needs to interact with it. Previously, you have got familiar with different types of IP Addresses.
So, stay with us on this article to see how you can set an IP address, gateway, and DNS server for a NIC. In the following, different three methods will be presented and discussed. Before going through this guide, you must find out the name of your ethernet since a static IP address cannot be configured without this name. To list all the interfaces on your system, log into your server and run the command below.
$ ip addr 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 Server IP/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: <broadcast,multicast,up,lower_up> 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 Server IP brd Server IP scope global dynamic noprefixroute enp1s0 valid_lft 3004sec preferred_lft 3004sec inet6 fe80 IP address :1ddd:a8e1/64 scope link noprefixroute valid_lft forever preferred_lft forever </broadcast,multicast,up,lower_up></loopback,up,lower_up>
You will see two interfaces in your output. The first virtual interface is used by the system to talk with itself. The second interface is your current IPv4 address that you want to modify. Let’s review the three methods you can use to configure static IP on RHEL/CentOS 8:
1. How to Setup a Static IP Address by Directly Editing the Configuration File
Firstly, to set up the static IP address on the CentOS 8 server, modify the interface configuration file manually. The network interface configurations on CentOS 8 server are located in the ‘/etc/sysconfig/network-scripts‘ directory by default. The directory contains network configurations that are managed by the NetworkManager service.
To view all available configuration files, go to the ‘/etc/sysconfig/network-scripts‘ directory.
The name of the file is composed by the
ifcfg- prefix plus the name of the interface. If you inspect the file related to your NIC, the actual setup will be shown as below.
$ 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"
You might have set up your CentOS 8 with DHCP which sets the protocol to use at boot to set the IP address of the interface. Since your server does not use a GUI, changing the static IP address requires some applications. Here, the BOOTPROTO option is set to dhcp. The possible options to use are:
- none: No protocol should be used
- bootp: Use the bootp protocol
- dhcp: Use the dhcp protocol
To set a static IPv4 address, the value of BOOTPROTO should be changed to
none, and configure your IP, route prefix, gateway, and DNS server statically. The
DNS server must be reported together with a progressive number (starting from 1) because it can be specified. When the modifications are finished, the file should be something 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= Your static IP PREFIX=24 GATEWAY= Your gateway IP DNS1=Your dns IP
To have effective changes, put down and up again the network interface. But it will also disturb existent ssh connections via said interface:
$ sudo nmcli connection down enp1s0 && sudo nmcli connection up enp1s0
To verify if the IP has changed, you can use the following command.
IP addr|grep enp1s0|grep inet inet Your IP brd Your IP scope global noprefix
2. How to use
nmcli to Configure Static IP on CentOS 8
As we mentioned, there are three methods to Configure Static IP on RHEL/CentOS 8. So far, you reviewed one of these methods. Let’s see how the second one works.
nmcli is a command line tool that you can use to control NetworkManager and change network settings for a specific interface among other things. Here, you must use this command with the connection subcommand with the modify or the edit action. The first lets you change the settings directly, while the second launch the
nmcli shell, where you can operate interactively.
nmcli directly, you can run the command below and change the IP address of the “enp1s0” interface.
$ sudo nmcli connection modify enp1s0 IPv4.address Your IP
Since you have specified the routing prefix together with the IP using the so-called
CIDR notation, you can use a similar syntax to change the gateway and the DNS settings:
$ sudo nmcli connection modify enp1s0 IPv4.gateway Your IP $ sudo nmcli connection modify enp1s0 IPv4.dns Your IP
If you prefer to not use any other boot protocol for the interface, you need to set the method to
manual. In this way, the BOOTPROTO option will be set to none in the interface configuration file.
$ sudo nmcli connection modify enp1s0 IPv4.method manual
You are in the middle of learning how to configure static IP on RHEL/CentOS 8. You can also use an interactive method to perform the same changes. So, use the
edit action and the name of the interface you want to edit to enter the
$ 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>
Then, to get a list of the available ones, type
help and run
describe followed by a setting and one of its properties to get a description of the latter. Look at the example below:
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: Your IP
And to change your interface address:
nmcli> set IPv4.address Your IP
The same principle is used to change the other properties:
nmcli> set IPv4.gateway Your IP nmcli> set IPv4.dns Your IP nmcli> set IPv4.method manual
Finally, use the command below to save the changes you made.
To exit the nmcli shell, type the following command and press Enter.
Again, reload the interface using the command below to make the changes effective.
3. How to use
nmtui to Configure Static IP on CentOS 8
So far, you learned two methods of three one to Configure Static IP on CentOS 8. Also, you reviewed the way you can change the parameter of a network interface by modifying its file manually or by using the nmcli command tool. In this section, you will learn how to control NetworkManager by using a text user interface. So, run the
nmtui in your terminal to see the nmtui text user interface
$ sudo nmtui
Then, you should select
Edit a connection and the name of the interface you desire to modify. Next, changing the values will be done as you see in the following example.
Once you see a desirable result, select
ok and press enter to be taken back to the interface selection menu. To exit, select
back and choose
Use the command below to apply the settings you want to reload the interface connection.
$ sudo nmcli connection down enp1s0 && sudo nmcli connection up enp1s0
While Configure a static IP using NetworkManager, you can use the command below to do this:
nmcli con mod ethX ipv4.method manual nmcli con mod ethX ipv4.dns "126.96.36.199 188.8.131.52"
If you do not want to remove the network manager, use the following command to disable it.
systemctl disable NetworkManager systemctl remove NetworkManager
In this article, you learned How to Configure Static IP on RHEL/CentOS 8. Three different methods were explained with examples to help you see how to change options and what to add to the interface configuration file while you are editing it manually. The same changes could be made using nmcli as a command line tool and nmtui as a text user interface which are used to control the NetworkManager daemon.