How to clear routing table from cache in Linux

In this article, I will show how to refresh the routing table in Linux using terminal commands. A routing table in a computer network is a data table stored on a router or network server that lists routes to specific network destinations. Generally, each static route has a metric associated with it. Network devices use this metric to determine which route to choose for packets destined for a particular network. When talking in the context of a Linux system, they also maintain a routing table, mainly if the server has multiple active network interfaces.

At some point, you may want to refresh or update the routing table so that you can access certain networks. The network can be local in your organization or it can traverse the public Internet. Most Linux distributions provide two main tools for this task, namely ip, And routing command line tools.

Refresh routing table with ip command

IP is a Unix / Linux command line tool for displaying and manipulating routes, network devices, interfaces and tunnels. It replaces the most commonly used ifconfig tool. This tool can perform almost any type of Linux network configuration for the interface. Before refreshing any routes, you may first need to check the current routing table using the following 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 the output, my default route is set to 192.168.0.1. This means that all packets destined for networks other than my local subnet 92.168.0.0/24 will be forwarded through 192.168.0.1. That IP 192.168.0.1 is used for my router.

For demonstration purposes, I will start the docker service on my laptop. Docker has its own subnet. We will use it to demonstrate the use of the 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

Rinse a specific route

I now set up another route for the 172.17.0.0/16 subnet via 172.17.0.1. If you want to refresh this route, you will 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 deleted from the routing table.

Flush all routes

To flush all routes on the routing table, use the ip command option route flush table main. The full command to run is:

$ sudo ip route flush table main
$ ip route show

Rechecking the routing table should return empty. Use this command with caution, as it may kick you out of the server if you are not directly connected or have no other access method (such as console, modem, etc.).

Clear route cache

The Linux kernel usually references the route cache before getting new routes from the routing table. You can clear this cache using commands.

$ sudo ip route flush cache

Confirm all available cache routes with:

$ sudo ip route show cache

Display statistics in route cache

If you want to get more information about cache routing. Use the following command:

$ sudo ip -s route show cache

Other information, such as “used” Given a field indicating the number of times this route has been visited in the route cache.

For a permanent static route, you can place the route in any of the following files:

/etc/sysconfig/static-routes or /etc/sysconfig/network-scripts/route-  # Red Hat and its derivatives - CentOS, Fedora e.t.c
/etc/network/interfaces # Debian and its derivatives

An example of persistent routing 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 looks like this:

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.

Also read:

  • How to use IP commands in Linux by example
  • How to use the Linux Route Add command
Sidebar