1. Overview
In Linux, Ethernet interfaces are important components that enable communication between the system and the network. At times, it may become necessary to clear the IP address assigned to an interface without bringing the interface up or down or rebooting the entire system, i.e., cycling. For example, server administrators often try to avoid downtime, so any maintenance that can prevent that can help boost productivity.
In this tutorial, we’ll understand the process of removing IP addresses from an Ethernet interface without interruptions to it, the network, and system connectivity. Moreover, we’ll explore the commands and code snippets required to accomplish this task and go through the output of each code snippet.
2. superuser Logging
Modifying network settings usually requires superuser access. Before we proceed, let’s ensure that we have administrative privileges on the Linux system. For example, let’s log in as the root user:
$ su -
Password: Last login: Sat Oct 14 05:08:21 PDT 2022 on tty2
# whoami
root
Referring to the above snippet, we utilize the su command followed by the – symbol to switch to the root user. Furthermore, we verify the current user is now root via the whoami command.
In some scenarios, for security reasons, we don’t have access to the root credentials. In other words, we can’t log in as root. As a workaround, in that case, we can log in with our normal user and use the sudo command.
3. Using the ip Command
The ip command is a powerful tool for managing network interfaces in Linux. Further, we can use this command to clear the IP address associated with a specific Ethernet interface.
3.1. List Available Network Interfaces
Before clearing the IP address, let’s identify the Ethernet interface we want to modify.
There are two commands that provide insights into the available network interfaces:
Using the latter, we list all Ethernet interfaces with their addresses:
# ip addr
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.110/24 brd 192.168.56.255 scope global
dynamic noprefixroute enp3s0
valid_lft 456sec preferred_lft 456sec
In the above output, we see that the IP associated with interface eth0 is 192.168.56.110/24. Let’s check out the output of ip link for that interface:
# ip link show eth0
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff
We used the show option followed by the interface name we want to view. As we can see, no IP address is shown in the output when using the above command. Because of this, we’ll stick to ip addr below.
3.2. Clearing IP Addresses
Now we know our main Ethernet interface is eth0. So, we can proceed to clear its IP address using the ip command:
# ip addr flush dev eth0
Let’s analyze the above command in-depth:
- ip addr flush instructs the ip utility to flush or clear all IP addresses
- dev eth0 specifies the network device (eth0) to perform the operation on
Consequently, let’s validate that we cleared the associated IP address of 192.168.56.110:
# ip addr
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff
Checking the above output, we verify the absence of the IP address configuration.
Notably, there’s an alternative method to remove a specific IP address:
# ip addr del 192.168.56.110/24 dev eth0
This method uses the del option, which stands for delete. After it, we specify the IP address and its subnet mask.
The main difference is that ip addr del is used to delete a specific IP address from an interface, while ip addr flush removes all IP addresses associated with that interface.
4. Understanding dhclient
In some cases, the purpose of removing an IP address associated with a certain interface is to replace it with a new one. Furthermore, in most environments, especially large ones, administrators use Dynamic Host Configuration Protocol (DHCP) rather than manual IP address assignment.
Therefore, we’ll discuss how to utilize the dhclient utility to remove and assign a new IP to our specific Ethernet interface.
First, let’s check the current configuration for eth0:
# ip addr show dev eth0
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.1.150/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 84333sec preferred_lft 84333sec
inet6 fe80::aabb:ccdd:eeff:ffff/64 scope link
valid_lft forever preferred_lft forever
Afterward, we’ll use the dhclient command to remove the IP address 192.168.1.150 and assign a new one dynamically:
# dhclient eth0 -r
# ip addr show dev eth0
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.1.151/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 84333sec preferred_lft 84333sec
inet6 aabb::fe46:ccdd:eeff:ffff/64 scope link
valid_lft forever preferred_lft forever
After specifying the interface eth0, we used the -r option. In essence, this option releases the current DHCP lease for the eth0 interface, effectively clearing the IP address.
5. Conclusion
In this article, we explored several modern methods to clear the IP address of an Ethernet interface in Linux without cycling the interface up or down or restarting the system.
We emphasized the use of the ip command, which is the recommended approach for managing network interfaces in modern Linux distributions.
Additionally, we saw how to clear all IP addresses and discussed the dhclient method for obtaining IP addresses dynamically.
Finally, we didn’t use the ipconfig command in this article as it’s recently been deprecated in many Linux distributions.