Change IPv4 Addresses using the NMCLI Utility

Change IPv4 addresses using nmcli

Discover the convenient and efficient way to change IPv4 addresses on your Linux machine using the nmcli utility, and take control of your network settings with ease.

Table of Contents

Introduction

Changing IPv4 addresses using the nmcli utility is a useful way to manage network connections on a Linux system. Here’s a step-by-step guide on how to change IPv4 addresses using the nmcli utility, with at least three different ways to accomplish each step.

We’ll be taking a similar approach as our previous article on changing DNS settings, this time delving into the process of changing IPv4 addresses using the NMCLI Utility specifically on RHEL or CentOS operating systems.

Step 1: Network configuration

Before changing the IPv4 address, it’s important to check the current network configuration to ensure you know what you’re changing. Here are three different ways to do this:

Method 1: Use nmcli

The easiest way to check the current network configuration is to use the nmcli command:

				
					$ nmcli connection show
				
			

This command will display a list of all the network connections on your system. Find the name of the connection you want to modify and note it down.

Method 2: Use ip addr

Another way to check the current network configuration is to use the ip addr command:

				
					$ ip addr show
				
			

This command will display the IP addresses and network interfaces on your system. Find the interface you want to modify and note it down.

Method 3: Use ifconfig

A third way to check the current network configuration is to use the ifconfig command:

NOTE:  As of the RHEL 7 release, the ifconfig command is no longer available by default and requires the net-tools package to be installed using either yum or dnf, depending on your operating system.

				
					$ ifconfig
				
			

This command will display the IP addresses and network interfaces on your system. Find the interface you want to modify and note it down.

				
					$ ifconfig
				
			
				
					enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.21  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ad2:4604:a246:e4e7  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:16:04:9c  txqueuelen 1000  (Ethernet)
        RX packets 3243495  bytes 2375068065 (2.2 GiB)
        RX errors 0  dropped 655879  overruns 0  frame 0
        TX packets 150113  bytes 11491248 (10.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 10  bytes 900 (900.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 900 (900.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

				
			
				
					$ ip addr 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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:16:04:9c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.21/24 brd 192.168.1.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::ad2:4604:a246:e4e7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
				
			

Step 2: Modify the IPv4 address

Now that you know the name of the connection or interface you want to modify, you can change the IPv4 address associated with it. Here are three different ways to do this:

Method 1: Use nmcli

The easiest way to modify the IPv4 address is to use the nmcli command:

				
					$ sudo nmcli connection modify <connection_name> ipv4.addresses <new_ip_address>/<subnet_mask> ipv4.gateway <new_gateway_ip>
				
			

Replace the following to match your environment or network settings:

  • <connection_name> with the name of the connection you want to modify.
  • <new_ip_address> with the new IP address you want to assign to the connection.
  • <subnet_mask> with the subnet mask for the new IP address.
  • <new_gateway_ip> with the new gateway IP address you want to use.

Method 2: Use ifconfig

Another way to modify the IPv4 address is to use the ifconfig command:

				
					$ sudo ifconfig <interface_name> <new_ip_address> netmask <subnet_mask>
				
			

Change the following to match your environment or network or settings:

  • <interface_name> with the name of the interface you want to modify.
  • <new_ip_address> with the new IP address you want to assign to the interface.
  • <subnet_mask> with the subnet mask for the new IP address.

Method 3: Use ip addr

A third way to modify the IPv4 address is to use the ip addr command:

				
					$ sudo ip addr add <new_ip_address>/<subnet_mask> dev <interface_name>
				
			

Similarly, the ip addr command performs the same function as the ifconfig command and it’s the new standard for RHEL versions 7, 8 and 9.

Step 3: Apply the changes

After modifying the network configuration, you need to apply the changes by restarting the network service. Here are three different ways to do this:

Method 1: Use systemctl

The easiest way to apply the changes is to use the systemctl command:

				
					$ sudo systemctl restart NetworkManager.service
				
			

This command will restart the NetworkManager service, which will apply the changes to the network configuration.

Method 2: Use service

Another way to apply the changes is to use the service command:

				
					$ sudo service NetworkManager restart
				
			

It performs the same function as the systemctl command and restarts the NetworkManager service, which applies changes to the network configuration.

Method 3: Restart and reload

A third way to apply the changes is to use both systemctl and nmcli:

				
					$ sudo systemctl restart NetworkManager.service
$ sudo nmcli connection reload
				
			

These commands will ensure that network configuration changes remain persistent even after system reboots.

Step 4: Verify the changes

Finally, it’s important to verify that the changes have been applied correctly. Here are three different ways to do this:

Method 1: Use nmcli

The most common way (in the more recent versions of RHEL and CentOS) to verify the changes is to use the nmcli command:

				
					$ sudo nmcli connection show <connection_name>
				
			

Replace <connection_name> with the name of the connection you modified. This command will display the details of the connection, including the new IPv4 address.

Method 2: Use ip addr

Another way to verify changes is to use the ip addr command:

				
					$ sudo ip addr show <interface_name>
				
			

Replace <interface_name> with the name of the interface you modified. This command will display the IP addresses and network interfaces on your system, including the new IPv4 address.

Method 3: Ping a remote host

A third way to verify the changes is to ping a remote host:

				
					$ ping <remote_host>
				
			

Replace <remote_host> with the IP address or hostname of a remote host. If the ping is successful, the changes have been applied correctly and the network connection is working. If the ping fails, there may be an issue with the network configuration.

Conclusion

In summary, changing IPv4 addresses using the nmcli utility involves several steps:

nmcli connection show

Identify the connection you want to modify using the nmcli connection show command.

 nmcli connection modify

Modify the IPv4 address using the nmcli connection modify command.

nmcli connection up, sudo service network-manager restart, or sudo systemctl restart NetworkManager.service && sudo nmcli connection reload.

Apply the changes using one of three methods: nmcli connection up, sudo service network-manager restart, or sudo systemctl restart NetworkManager.service && sudo nmcli connection reload.

nmcli connection show, ip addr show, or ping.

Verify the changes using one of three methods: nmcli connection show, ip addr show, or ping.

We also provided three different ways to accomplish each step to give users more flexibility and options depending on their preferences and familiarity with different commands.

Was this article helpful to you? If so, leave us a comment below and share!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *