Introduction
Routing is a difficult technique in the networking world. In order to make your computers communicate with other devices on the network, you have to define the route for them.
Thanks to the route command, the network administrators or Linux users can do this work in an easier way.
Route command can be used to show and modify the network routing table in a Linux system.
Below is the guide on how to use the route command in Linux.
Routing table
To show the current routing table, let’s run the following command
$ route -n
Output:
Another way to describe more detail about which network interface of routes is running the command:
$ ip route show
Output:
Add a new route
If your computer has more than one network interface and you want to add a new route, the computer will send traffic through that new gateway. You can use the route add command as below:
$ sudo route add -net <NETWORK-ADDRESS> gw <GATEWAY> <NETWORK-INTERFACE>
For example:
$ sudo route add -net 192.168.1.0/24 gw 192.168.1.1 eth2
To add a new route to a host, run the following command:
$ sudo route add -host <HOST-IP> gw <GATEWAY>
For example:
$ sudo route add -host 172.19.11.75 gw 172.19.11.1
Output:
In the Flags column, there are some values that are difficult to understand. The following section is a quick explanation:
U: up
H: host
G: gateway
!: rejected route
Delete a route
If you no longer use a specific route and you want to delete it, you can simply run the following command:
$ sudo route del -net <NETWORK-ADDRESS> gw <GATEWAY> <NETWORK-INTERFACE>
For example:
$ sudo route del -net 192.168.1.0/24 gw 192.168.1.1 eth2
To reject a route but you want to keep it on the routing table, run the command with the reject option.
$ sudo route add -host 172.19.11.75 reject
Conclusion
You have just read a tutorial about how to use the route command in a Linux system with examples.
Thanks for reading. If you have any concerns, feel free to leave your comment and let me know.