route command in Linux with Examples

route command in Linux is used when you want to work with the IP/kernel routing table. It is mainly used to set up static routes to specific hosts or networks via an interface. It is used for showing or update the IP/kernel routing table.

Installing route Command

Many Linux distributions do not have route command 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 yum install net-tools

In case of Fedora OS



$sudo dnf install net-tools

Working with route command

1. To display the IP/kernel routing table.

$route

To-display-the-IP-kernel-routing-table
It displays the routing table entries.

2. To display routing table in full numeric form.

$route -n

To-display-routing-table-in-full-numeric-form

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

3. To add a default gateway.

$sudo route add default gw 169.254.0.0

To-add-a-default-gateway

This assigns a gateway address on 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.

4. To list kernel’s routing cache information.

$route -Cn

To-list-kernels-routing-cache-information

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.

5. To reject routing to a particular host or network.

$sudo route add -host 192.168.1.51 reject

To-reject-routing-to-a-particular-host-or-network

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

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

$ip route

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

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



7. To delete the default gateway.

$route del default

To-delete-the-default-gateway

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

This will remove the default gateway.

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

$ip route show table local

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

This will print the details of the local table.

9. To get output related to IPv4.

$ip -4 route

To-get-output-related-to-IPv4

This will only display the entries with ipv4.

10. To get output related to IPv6.

$ip -6 route

To-get-output-related-to-IPv6

This will only display the entries with ipv6.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.