The Wayback Machine - https://web.archive.org/web/20240118180815/https://www.geeksforgeeks.org/route-command-in-linux-with-examples/
Open In App
Related Articles

route command in Linux with Examples

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 31 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads