In this article, I will describe how to flush routing table in Linux using terminal commands. A routing table in computer networking is a data table that's stored on a router or network server which lists the routes to particular network destinations. Usually, each static route has a metric associated with it. This metric is used by network devices to decide which route to be selected for packets destined to a particular network. When talking in the context of Linux Systems, they also maintain a routing table, mostly when the server has more than one active network interface.
At some point, you may want to flush a routing table or update it so that you can access some networks. The network(s) can be local in your organization, or where traversing the public internet is involved. Most Linux distributions provide two major tools used for this task, namely ip, and route command line tools.
Flush routing table using ip command
IP is a Unix/Linux command line tool used to show and manipulate routing, network devices, interfaces, and tunnels. It is a replacement for the mostly used ifconfig tool. This tool can do almost all kinds of Linux network configurations for an interface. Before you flush any routes, you may first need to check your current routing table using the command::
$ ip route default via 192.168.0.1 dev wlp1s0 proto dhcp metric 600 192.168.0.0/24 dev wlp1s0 proto kernel scope link src 192.168.0.16 metric 600
As you can see from my output, my default route is set to 192.168.0.1. This means all packets destined for networks other than my local subnet 188.8.131.52/24 will be forwarded through 192.168.0.1. This IP 192.168.0.1 is for my router.
For demonstration purposes, I'm going to start docker service on my Laptop. Docker has its own subnet. we'll use this to demonstrate the usage of ip command.
$ sudo systemctl start docker $ ip route default via 192.168.0.1 dev wlp1s0 proto dhcp metric 600 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 192.168.0.0/24 dev wlp1s0 proto kernel scope link src 192.168.0.16 metric 600
Flush specific route
I now have another route for 172.17.0.0/16 subnet via 172.17.0.1. If I want to flush this route, I'll use:
$ sudo ip route flush 172.17.0.0/16 $ ip route default via 192.168.0.1 dev wlp1s0 proto dhcp metric 600 192.168.0.0/24 dev wlp1s0 proto kernel scope link src 192.168.0.16 metric 600 $ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 600 0 0 wlp1s0 192.168.0.0 0.0.0.0 255.255.255.0 U 600 0 0 wlp1s0
You can confirm that the route has been removed from the routing table.
Flush all routes
To flush all routes on the routing tables, the ip command options route flush table main are used. The full commands to run are:
$ sudo ip route flush table main $ ip route show
A recheck for the routing table should return empty. This command should be used with caution since it can kick your out of the server if you're not on direct connection or don't have other access methods like a console, modem e.t.c.
Empty a routing cache
The Linux kernel usually refers to the routing cache before fetching a new route from the routing tables. This cache can be cleared using the command.
$ sudo ip route flush cache
The confirm any available cached routes with:
$ sudo ip route show cache
Displaying statistics from the routing cache
If you would like to get more information about the cached routes. use the commands:
$ sudo ip -s route show cache
Additional information like "used" field is given, which indicates the number of times this route has been accessed in the routing cache.
For a persistent static route, you can place routes in any of the below files:
/etc/sysconfig/static-routes or /etc/sysconfig/network-scripts/route-<interface> # Red Hat and its derivatives - CentOS, Fedora e.t.c /etc/network/interfaces # Debian and its derivatives
An example of persistent routes on CentOS 7 is:
# cat /etc/sysconfig/network-scripts/route-eth0 GATEWAY0=192.168.1.254 NETMASK0=255.255.255.0 ADDRESS0=192.168.5.0 GATEWAY1=10.10.10.1 NETMASK1= 255.255.255.240 ADDRESS1=10.164.234.132
A line on Ubuntu/Debian will look like below:
up route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.1.254 down route del -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.1.254
Please let us know your comments on this article.