Open In App

How to check the routing table in Linux | route Command

Improve
Improve
Like Article
Like
Save
Share
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.

How to Check the routing Table in Linux – FAQs

How do I check the routing table in Linux using the route command?

To view the routing table in Linux, you can use the `route` command. Simply open a terminal and type `route` or `route -n` to display the routing table. This command provides information about the network routes configured on your system.

Can I filter the output of the route command to display specific information?

Yes, the `route` command allows you to filter the output. For example, you can use `route -n` to display numerical IP addresses instead of hostnames. Additionally, you can use grep or awk commands to further filter and format the output as needed.

How can I add a new route to the routing table in Linux?

To add a new route, you can use the `route add` command followed by the destination network and gateway. For example, `sudo route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1` adds a route to the 192.168.1.0 network via the gateway 192.168.0.1.

What is the difference between the route and ip route commands in Linux?

Both `route` and `ip route` commands can be used to display and manipulate the routing table. However, `ip route` is considered more modern and flexible. It provides additional features and is recommended for users working with newer Linux systems.

How do I make a route persistent across reboots in Linux?

To make a route persistent, you need to add the route configuration to the appropriate network configuration file. The file location may vary depending on the Linux distribution. For example, on systems using systemd, you can add the route to a .network file, while on older systems, you might modify the /etc/network/interfaces file or use custom scripts in /etc/network/if-up.d/.

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.



Last Updated : 30 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads