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.
It displays the routing table entries.
2. 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
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.
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
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.
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
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
This will print the details of the local table.
9. To get output related to IPv4.
$ip -4 route
This will only display the entries with ipv4.
10. To get output related to IPv6.
$ip -6 route
This will only display the entries with ipv6.