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. Route command can be used to show and modify the network routing table in a Linux system. Therefore, In order to make your computers communicate with other devices on the network, you have to define the route for them.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux System commands queries.
In this context, we shall look into how to use the route command in Linux.
Many Linux distributions do not have route command pre-installed. To install it use the following commands as per your Linux distribution.
1. In case of Debian/Ubuntu:
$ sudo apt-get install net-tools
2. In case of CentOS/RedHat:
$ sudo yum install net-tools
3. In case of Fedora OS:
$ sudo dnf install net-tools
1. To display the IP/kernel routing table, run the command:
$route
This command displays the routing table entries.
2. To display routing table (the current routing table) in full numeric form, run the command:
$route -n
Another way to describe more detail about which network interface of routes is running the command:
$ ip route show
3. To 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
In the Flags column, there are some values that are difficult to understand. The following section is a quick explanation:
4. How to 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
5. To list kernel's routing cache information, execute the command:
$ 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.
This article covers how to use the route command in a Linux system with examples. Linux provides the ip route command in order to manage the routing table. The routing table is used to set routes for different destination IP addresses. Even there is a default route in most of the cases which are used to sent IP packets to the other networks we may need to add new routes via the command-line interface by using the ip route add command.
Important Linux commands: