Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

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.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

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.

2. Using ip Command

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.

2.1. Syntax

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.

2.2. Removing IP Route

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.

3. Using route Command

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.

3.1. Syntax

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.

3.2. Removing IP Route

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.

4. Using nmcli Command

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.

4.1. Syntax

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.

4.2. Removing 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.

5. Conclusion

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.