Set Static IP in Rocky Linux [6 Different Methods]

Different methods to set Static IP Address - Rocky Linux 8

After a successful installation of Rocky Linux on your environment, there is need to configure network. You can either configure a static or a dynamic IP address. Network connections in Rocky Linux are managed by NetworkManager daemon.

In this guide, we shall discuss how to configure IP addresses on Rocky Linux using the different methods available.

Advertisement

There are many ways to configure IP addresses on Rocky Linux. In this guide, we shall cover the following:

  1. Configuring IP address through manually editing the network interface file.
  2. Configure IP address using ifconfig utility
  3. Using ip utility
  4. Using ifcfg utility
  5. Configuring IP using the NMTUI tool
  6. Configuring IP address using NMCLI tool

 

Method-1: Manually Edit Network Interface Config File

The first method that we shall discuss is where you edit a configuration file for a specific network interface to set the IP address and other options such as the DNS server.

To do this, you first need to identify the available network interfaces. Run the command below to identify the available interfaces.

$ ip addr

You should get an output such as the one below:
Set Static IP in Rocky Linux [6 Different Methods]

In the above output, I have four interfaces:

  1. Loopback interface (lo)
  2. enp0s3
  3. enp0s8
  4. enp0s9

This information is important as you need to know the interface name, and most importantly the MAC address of the interface that you intend to configure.

Advertisement

It is also important to get the status of the NetworkManager service:

[root@rockylinux-lab ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-08-16 22:35:39 EAT; 1min 1s ago
     Docs: man:NetworkManager(8)
 Main PID: 777 (NetworkManager)
    Tasks: 3 (limit: 6001)
   Memory: 8.5M
   CGroup: /system.slice/NetworkManager.service
           └─777 /usr/sbin/NetworkManager --no-daemon

Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.7282] dhcp6 (enp0s9): activation: beginning transaction (timeout in 45 seconds)
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8097] dhcp6 (enp0s9): state changed unknown -> bound
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8225] device (enp0s9): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8380] device (enp0s9): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8404] device (enp0s9): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8485] device (enp0s9): Activation: successful, device activated.
Aug 16 22:35:42 rockylinux-lab NetworkManager[777]: <info>  [1629142542.8514] manager: startup complete
Aug 16 22:35:43 rockylinux-lab NetworkManager[777]: <info>  [1629142543.7429] dhcp4 (enp0s9): state changed expire -> bound, address=192.168.100.123
Aug 16 22:35:46 rockylinux-lab NetworkManager[777]: <info>  [1629142546.0729] dhcp6 (enp0s3): activation: beginning transaction (timeout in 45 seconds)
Aug 16 22:35:46 rockylinux-lab NetworkManager[777]: <info>  [1629142546.1053] dhcp6 (enp0s3): state changed unknown -> bound

The network interface configuration files exist at /etc/sysconfig/network-scripts/. The interface configuration files have the prefix ifcfg-<interfacce>

You can list the contents of the directory to identify the interface configuration files under /etc/sysconfig/network-scripts/ as shown:

[root@rockylinux-lab network-scripts]# ls -l
total 8
-rw-r--r--. 1 root root 387 Jun 11 16:06 ifcfg-enp0s3
-rw-r--r--. 1 root root 371 Aug  9 16:02 ifcfg-enp0s8
-rw-r--r--. 1 root root 371 Aug  9 16:02 ifcfg-enp0s9

Assuming we want to update or add or modify the IP address of the interface enp0s3, we shall edit the file ifcfg-enp0s3.

[root@rockylinux-lab network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
HWADDR=08:00:27:dd:31:ef
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=enp0s3
UUID=c75724a4-ae29-4ba4-a098-f6d490357d38
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.100.149
PREFIX=24
GATEWAY=192.168.100.1
DNS1=192.168.100.1
DNS2=8.8.8.8
DOMAIN=viclab.lan

In the above configuration file, the following are the most important fields to take note of:

  1. TYPE - The interface type, such as Ethernet
  2. BOOTPROTO - This is the IP configuration method(Static/DHCP). Use "static" for a static IP or "dhcp" for a DHCP configuration.
  3. ONBOOT - allows the interface to come up when the machine reboots
  4. HWADDR - The MAC address of the interface
  5. DEFROUTE - specify if the interface will be used for the default route.
  6. NAME - The name of the interface, such as enp0s3
  7. DEVICE - The physical NIC name (obtained from "ip addr" command)
  8. IPADDR - The IP address that you intend to assign the interface
  9. PREFIX - The Subnet mask prefix, such as 24, 27, etc.
  10. GATEWAY -  the gateway for the IP address assigned
  11. DNS - The DNS IP for the DNS server

 

Manually update these fields in the respective interface configuration file and save the changes. To activate the configuration, run the command below:

# systemctl restart network

 

Method-2: Configure Static IP Address using ifconfig

You can also configure a static IP address on Rocky Linux using the ifconfig tool.

Advertisement

The syntax to configure IP Address using ifconfig tool would be:

ifconfig <interface> <IP Address> netmask <netmask_value>

For example to assign static IP address to enp0s8 interface, execute the following command:

[root@rockylinux-lab ~]# ifconfig enp0s8 172.29.10.10 netmask 255.255.255.0

Next in case this interface is the default interface, you would also need to provide the default gateway. To assign a default gateway to your interface, execute the following command:

[root@rockylinux-lab ~]# sudo route add default gw 172.29.10.1 enp0s8

In the above command, we have set the IP for the interface enp0s8 and also created a default route to pass through the same interface.

NOTE:
The network configuration changes done by ifconfig tool are not persistent and will be overwritten if the network is restarted or the server is rebooted.

 

Method-3: Configure Static IP using ip command

We can also configure IP using the ip command provided by the iproute2 package. To get the general information of your network configuration on Rocky Linux, use the command below:

[root@rockylinux-lab ~]# ip addr show

You can assign a static IP to an interface using following syntax:

ip addr add <IP Address> dev <interface>

For example to assign static IP to ens0p8 interface, we use following command:

[root@rockylinux-lab ~]# ip addr add 172.29.10.10/24 dev ens0p8

Check the interface details after applying the above command:

[root@rockylinux-lab ~]# ip addr show dev enp0s8

You can then bring down and then bring up the interface to activate the changes:

[root@rockylinux-lab ~]# ip link set enp0s8 down
[root@rockylinux-lab ~]# ip link set enp0s8 up

To add the gateway:

[root@rockylinux-lab ~]# ip route add default via 172.29.10.1 dev enp0s8
NOTE:
The network configuration changes done by ip command are not persistent and will be overwritten if the network is restarted or the server is rebooted.

 

Method-4: Configure Static IP using ifcfg Utility

Configure a static IP on Rocky Linux 8 using the ifcfg utility as shown below:

[root@rockylinux-lab ~]# ifcfg enp0s8 add 172.29.10.10/24

The above command adds the IP 172.29.10.10/24 to the interface enp0s8.

You can remove the IP address on the interface by  the command below:

[root@rockylinux-lab ~]# ifcfg enp0s8 del 172.29.10.10/24

To add the gateway:

[root@rockylinux-lab ~]# ip route add default via 172.29.10.1 dev enp0s8

Use the following command to check the default routes:

[root@rockylinux-lab ~]# ip route

 

Method-5: Set Static IP Address using NMTUI

NMTUI is the acronym of Network Manager Terminal User Interface. This means that you can manage the network using an interface presented through the terminal. To use this tool, you need to have some packages installed.

[root@rockylinux-lab ~]# yum install net-tools -y
Last metadata expiration check: 0:00:36 ago on Mon 16 Aug 2021 11:40:50 PM EAT.
Dependencies resolved.
========================================================================================================
 Package               Architecture       Version                              Repository          Size
========================================================================================================
Installing:
 net-tools             x86_64             2.0-0.52.20160912git.el8             baseos             321 k

Transaction Summary
========================================================================================================
Install  1 Package

Total download size: 321 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm                           913 kB/s | 321 kB     00:00    
--------------------------------------------------------------------------------------------------------
Total                                                                   347 kB/s | 321 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                1/1 
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64                                      1/1 
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64                                      1/1 
/sbin/ldconfig: /etc/ld.so.conf.d/kernel-ml-5.13.8-1.el8.elrepo.x86_64.conf:6: hwcap directive ignored

  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64                                      1/1 

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                                             

Complete!

To edit a network connection, run the command below as root user or with sudo privilege:

nmtui

You will see a screen like this below:

Set Static IP in Rocky Linux [6 Different Methods]

 

Select the "Edit a connection" option to edit a network interface. You will then need to choose the interface that you wish to edit in the subsequent screen.

Set Static IP in Rocky Linux [6 Different Methods]

 

Under IPv4 Configuration, hit the Enter key to bring the drop down menu and select Manual. Here, you are required to configure the IP configuration of the interface as desired.

Set Static IP in Rocky Linux [6 Different Methods]

Set the IPv4 configuration to either Automatic or Manual if you want DHCP or Static IP configuration respectively.

Put the IP address at the "Addresses" section, remember to append the subnet mask of the IP. Such as 192.168.100.149/24 where /24 is the subnet mask prefix.

Add the Gateway for the IP and the DNS servers.

To have the interface always connected (after reboot), check the "Automatically connect" option. It is also advisable to check the "Available to all users" option unless you have a reason not to.

Finish the configuration by pressing "OK" at the bottom. Head back to the first screen to activate the connection.

Set Static IP in Rocky Linux [6 Different Methods]

 

Choose the "Activate a connection" option to activate the specific interface that we have configured in the previous step.

Choose the interface then select the "Activate" button on the right.

Set Static IP in Rocky Linux [6 Different Methods]

You can now exit and verify that the interface has come up.

[root@rockylinux-lab ~]# ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.149  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::c11d:a25a:e065:1357  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:dd:31:ef  txqueuelen 1000  (Ethernet)
        RX packets 23547  bytes 29765516 (28.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1369  bytes 219204 (214.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

You can also use nmtui to set the system hostname.

To achieve this, run the nmtui command once more and select the set system hostname option.

Set Static IP in Rocky Linux [6 Different Methods]

 

Set the hostname in the space provided then press "OK"

Set Static IP in Rocky Linux [6 Different Methods]

 

You will receive a prompt that the hostname configuration has been successful.

Set Static IP in Rocky Linux [6 Different Methods]

Verify the hostname by running the command below:

[root@rockylinux-lab ~]# hostnamectl 
   Static hostname: rockylinux-lab
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 9111f432e0bf7140bb8c3641026cf405
           Boot ID: 9c293f77af9542ddaa9506c77fb69bd1
    Virtualization: oracle
  Operating System: Rocky Linux 8.4 (Green Obsidian)
       CPE OS Name: cpe:/o:rocky:rocky:8.4:GA
            Kernel: Linux 5.13.8-1.el8.elrepo.x86_64
      Architecture: x86-64

 

Method-6: Set Static IP Address using NMCLI on Rocky Linux

NMCLI is an acronym for Network Manager Command Line Interface. Just like NMTUI, NMCLI is a command-line NetworkManager configuration tool.

This tool can also be used to configure the network interfaces just like the two methods we have discussed above.

To configure the interfaces, we first of all need to check and see the available configurations.

[root@rockylinux-lab ~]# nmcli
enp0s9: connected to enp0s9
        "Intel 82540EM"
        ethernet (e1000), 08:00:27:8F:41:38, hw, mtu 1500
        ip4 default
        inet4 192.168.100.123/24
        route4 0.0.0.0/0
        route4 192.168.100.0/24
        inet6 fe80::343d:6ce4:74e3:7e3/64
        route6 fe80::/64

...

DNS configuration:
        servers: 192.168.100.1
        interface: enp0s9

        servers: 192.168.100.1 8.8.8.8
        domains: viclab.lan
        interface: enp0s3

You can also use the nmcli device show command to see a more detailed picture of your network configuration.

[root@rockylinux-lab ~]# nmcli device show
GENERAL.DEVICE:                         enp0s9
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:8F:41:38
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     enp0s9
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/5
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.100.123/24
IP4.GATEWAY:                            192.168.100.1
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 192.168.100.1, mt = 102
IP4.ROUTE[2]:                           dst = 192.168.100.0/24, nh = 0.0.0.0, mt = 102
IP4.DNS[1]:                             192.168.100.1
IP6.ADDRESS[1]:                         fe80::343d:6ce4:74e3:7e3/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 102

...

To configure manual network configuration for an interface using NMCLI, follow the interface below:

[root@rockylinux-lab ~]# nmcli connection modify enp0s8 IPv4.address 172.29.10.10/24

The above command sets the IP 172.29.10.10/24 to the interface enp0s8.

You can also add the Gateway and DNS settings as below:

[root@rockylinux-lab ~]# nmcli connection modify enp0s8 IPv4.gateway 172.29.20.1
[root@rockylinux-lab ~]# nmcli connection modify enp0s8 IPv4.dns 8.8.8.8

Finally, set the IP for the interface to manual:

[root@rockylinux-lab ~]# nmcli connection modify enp0s8 IPv4.method manual

You can also use the nmcli shell to edit interface configuration for a specific interface.

[root@rockylinux-lab ~]# nmcli connection edit enp0s8

You will be presented with an interface such as this below:

[root@rockylinux-lab ~]# sudo nmcli connection edit enp0s8

===| nmcli interactive connection editor |===

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

Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [<setting>.<prop>]' for detailed property description.

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

You can run commands such as describe interface and also add the IP configuration from this shell.

nmcli> set IPv4.addresses 172.29.10.10/24
nmcli> set IPv4.gateway 172.29.10.1
nmcli> set IPv4.dns 8.8.8.8
nmcli> set IPv4.method manual
nmcli> 

Save the configuration for the settings to take effect.

nmcli> save
Connection 'enp0s8' (a5a17c19-45d7-31ef-bd8e-8aa698d8a1f9) successfully updated.
nmcli> quit

Verify the IP configuration

[root@rockylinux-lab ~]# nmcli
...
enp0s8: connected to enp0s8
        "Intel 82540EM"
        ethernet (e1000), 08:00:27:9B:ED:44, hw, mtu 1500
        inet4 172.29.10.8/24
        inet4 172.29.10.10/24
        route4 172.29.10.0/24
        route4 172.29.10.0/24
        route4 0.0.0.0/0
        inet6 fe80::31dd:21b8:5737:333d/64
        route6 fe80::/64

...

 

Conclusion

In this tutorial we covered different methods to configure static IP Address in Rocky Linux using different tools and commands. The network configuration done using ip, ifcfg and ifconfig tool are non-persistent which means the changes are temporary and are valid only for the current session. If someone restarts the network service then your changes will be overwritten. Or if someone reboots the server then also the changes will be overwritten with the default configuration.

So if you are looking to set static IP address persistently across reboot then you should choose nmcli, nmtui or manually updating the network configuration file.

 

Further Reading

Rocky Linux Network Configuration

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment