Open In App

How to check the routing table in Linux | route Command

Last Updated : 30 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The IP/kernel routing table acts as a crucial map, determining how network packets are forwarded between different hosts and networks. By utilizing the route command, Linux administrators and users can establish static routes, enabling precise control over network connectivity and optimizing data transmission. In this comprehensive guide, we will explore the intricacies of the route command in Linux, unravel its functionalities, and delve into detailed examples to gain a profound understanding of its usage.

Installing route Command

Many Linux distributions do not have route commands pre-installed. To install it use the following commands as per your Linux distribution.

In case of Debian/Ubuntu

sudo apt-get install net-tools

In case of CentOS/RedHat

sudo dnf install net-tools

In case of Fedora OS

sudo dnf install net-tools

Working with route command

To display the IP/kernel routing table.

route

route

route

It displays the routing table entries.

To display routing table in full numeric form.

route -n

route -n

route -n

It is even useful when you have to determine why the route to nameserver has even vanished.

To add a default gateway.

sudo route add default gw 169.254.0.0

add default gateway

add default gateway

This assigns a gateway address to which all the packets that do not belong to the network are forwarded.

Note: In this case the, we wish to choose 169.254.0.0 as the default gateway. You may choose as per your need.

To list kernel’s routing cache information.

route -Cn

route -Cn

route -Cn

To route the packets faster, Kernel maintains this routing cache information. The above command will print the cache information. In this case, the cache information is maintained.

To reject routing to a particular host or network.

sudo route add -host 192.168.1.51 reject

reject routing

reject routing

Now if you ping to the above-mentioned IP it will display “Network is unreachable”.

To get details of the kernel/IP routing table using ip command.

ip route

Details of IP routing table

Details of IP routing table

This will give the details of the kernel/IP routing table and in this case, we have used IP command.

To delete the default gateway.

route del default

delete gateway

delete gateway

Caution:

This may lead to some malfunctioning of the internet. Keep a note of your default gateway before proceeding with the command. This will remove the default gateway.

To get the details of the local table with destination addresses assigned to the local host.

ip route show table local

ip route show table local

ip route show table local

This will print the details of the local table.

To get output related to IPv4.

ip -4 route

output related to IPv4

output related to IPv4

This will only display the entries with ipv4.

To get output related to IPv6.

ip -6 route

output related to IPv6

output related to IPv6

This will only display the entries with ipv6.

Conclusion

In this article we have discussed about `route` command in Linux which is used for managing the IP/kernel routing table. It allows displaying, adding, deleting, and modifying routing table entries. The command is useful for tasks such as setting up static routes, adding a default gateway, rejecting routing to specific hosts/networks, and accessing detailed routing information. Caution should be exercised while making changes to avoid disruptions to network connectivity.



Next Article

Similar Reads