
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 30, 2024
In Linux, managing network routes is an essential part of network administration. Specifically, they define the path that network packets take to reach their destination. Therefore, it sometimes becomes necessary to delete or modify these pathways. Furthermore, effective route management can enhance network performance and security.
In this tutorial, we’ll discuss the ip, route, and nmcli commands to delete an IP route in Linux.
The ip command is a part of the iproute2 package and is the modern preferred tool for network configuration. In particular, its core purpose is to show or manipulate routing, network devices, interfaces, and tunnels.
To begin with, let’s take a look at the syntax of the ip command for deleting a network interface route:
$ sudo ip route del <destination> [<options>]
In this syntax, the <destination> specifies an IP route, and <options> can include parameters like gateway or network interface.
Initially, we should check the available network IP routes in the routing table. To achieve this, we can execute ip route command in the shell:
$ ip route
default via 192.168.119.2 dev ens33 proto static metric 100
9.0.2.0/24 via 192.168.119.2 dev ens33
10.0.2.0/24 via 192.168.119.2 dev ens33
169.254.0.0/16 dev ens33 scope link metric 1000
192.168.119.0/24 dev ens33 proto kernel scope link src 192.168.119.32 metric 100
We can observe that multiple IP routes such as default, 9.0.2.0/24, and 10.0.2.0/24 are configured through the gateway 192.168.119.2 via the interface ens33.
Now, let’s proceed to remove the network IP route 10.0.2.0/24:
$ sudo ip route del 10.0.2.0/24 via 192.168.119.2 dev ens33
Subsequently, to confirm the removal of the IP route, let’s again run the ip route command:
$ ip route
default via 192.168.119.2 dev ens33 proto static metric 100
9.0.2.0/24 via 192.168.119.2 dev ens33
169.254.0.0/16 dev ens33 scope link metric 1000
192.168.119.0/24 dev ens33 proto kernel scope link src 192.168.119.32 metric 100
As expected, the IP route 10.0.2.0/24 no longer exists.
The route command may be considered outdated, but it’s still useful for managing network routes on many Linux systems. Specifically, the standalone route command displays the current content of the routing table. Furthermore, when the add or del option is used, the route command modifies the routing table.
To start, let’s examine the syntax of the route command to delete a network route:
$ route del -net <destination> netmask <netmask>
In this syntax, we can replace the <destination> and <netmask> with a specific IP route and subnet mask to delete.
To begin, let’s display the kernel IP routing table using the standalone route command in the shell first:
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 ens33
9.0.2.0 root 255.255.255.0 UG 0 0 0 ens33
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 ens33
192.168.119.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Upon examining the output, we can see network routes, their associated gateways, netmasks, flags, metrics, and network interfaces used for network traffic.
Let’s say we want to remove the IP route 9.0.2.0 this time. For this purpose, we can execute the del subcommand as shown previously:
$ sudo route del -net 9.0.2.0 netmask 255.255.255.0
Now, to confirm the deletion of the IP route, let’s again run the route command:
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 ens33
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 ens33
192.168.119.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Thus, we can observe that the route 9.0.2.0 has been removed from the list.
In addition to the ip and route commands, we can also delete a network route using the network manager command line tool nmcli. Moreover, the nmcli command provides ways to create, display, edit, delete, activate, and deactivate network connections.
Now, let’s look at the syntax of the nmcli command to remove a network IP route:
$ nmcli connection modify <connection_name> -ipv4.routes <destination>
Here, the <connection_name> refers to the name of the network connection, and <destination> is a network IP route.
To remove a network IP route using nmcli, we can modify the network connection and then bring it up.
For example, to remove the IP route 9.0.2.0/24, we can use this command:
$ nmcli connection modify netplan-ens33 -ipv4.routes 9.0.2.0/24
Afterward, let’s bring up the connection to apply the changes:
$ sudo nmcli connection up "netplan-ens33"
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
Consequently, we’ve successfully removed the IP route 9.0.2.0/24.
In this article, we’ve explored the ip, route, and nmcli commands to delete network IP routes in Linux. These tools provide powerful options to help us maintain and optimize the network routing.