1. Introduction

Often, Linux hosts have multiple default gateways for their Internet connection. This is the case, for example, when a computer has several active network interfaces.

For such devices, it’s essential to be able to remove default gateways. In fact, it can be especially critical for routers and Linux-based IoT devices.

In this tutorial, we’ll look at how the ip command can remove the default gateways on a Linux device.

2. Find Default Gateways

Firstly, let’s find the currently active default gateways in our system:

$ ip route
default via 10.54.4.1 dev enp0s31f6 
default via 10.54.14.1 dev wlx503eaad48d42 proto dhcp metric 600 
10.54.0.0/16 via 10.54.4.1 dev enp0s31f6 proto dhcp metric 100 
...

As we can see, some output lines contain the word default, which means that the route is a default gateway. Therefore, we have two active default gateways.

3. Remove a Single Default Gateway

To remove a single default gateway, we use ip route del default dev <device name>. For example, let’s remove a device named enp0s31f6:

$ ip route del default dev enp0s31f6

To confirm that the default gateway enp0s31f6 has been removed, let’s run the ip route command again:

$ ip route
default via 10.54.14.1 dev wlx503eaad48d42 proto dhcp metric 600 
10.54.0.0/16 via 10.54.4.1 dev enp0s31f6 proto dhcp metric 100
...

In the above, we no longer see a default gateway for enp0s31f6.

4. Remove All Default Gateways

To remove all default gateways, we can again use the ip command:

$ ip route flush 0/0

This won’t provide any output, but will simply delete the default gateways.

Notably, executing this command may disrupt the Internet or SSH connection on our device because default gateways are often the main points for Internet connection access.

Finally, to check that everything worked out correctly, we’ll run the ip route command:

$ ip route
10.54.0.0/16 via 10.54.4.1 dev enp0s31f6 proto dhcp metric 100 
...

As we can see, there are now no default gateways in the output.

5. Conclusion

In this short article, we learned how to remove the default gateways from the Linux host. We used the ip command for both checking the routes and deleting the default gateways.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.