Routing is the way that computers communicate on a local network or over the internet. To communicate over a network, computers need to know which gateway it should send traffic. A gateway could be a router in your network.
The route command is used in Linux to shows and change the ip routing table.
In this tutorial, I will explain how to use the route command to add static routes via gateway to send traffic.
1) Show the routing table
Before editing the routing table, it is good to see the default table with the existing routes on your Linux Kernel.
Use route command with the -n
option to displays the current kernel IP routing table. This command helps to identify the locally connected networks with its route.
The following command displays the routing table:
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 600 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
You may also use netstat -nr
command to print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
Display the routing table with netstat command:
$ netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
Now we have ip command to show routes
$ ip route show
2) Add a new route
As we say above, to manipulate the routing table, we use the route add
command ( add is the option) to manually indicate a static path that the packets will use through the network. It is used to assign a temporary static route which will change only if the administrator manually modifies the values of the new route.
There are many possibilities for using the route add command.
We should indicate the destination network, subnet mask of the network, and the gateway to use.
route add -net <network_address> gw <gateway> <interface_name>
Alternatively, you can now use ip route
command, syntax as follows:
ip route add <network_address> via <gateway> dev <interface_name>
For example, let's add a new route network 10.0.0.0/8 and via gateway IP address 192.168.1.1:
# route add -net 10.0.0.0/8 gw 192.168.1.1 eno1
Now we can display the routing table using route -n
:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eno1 10.0.0.0 192.168.1.1 255.0.0.0 UG 0 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 600 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
You can add netmask
option if you want to add netmask value. If not provided then route command finds automatically the value of netmask.
# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.100 eth0 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 192.168.1.100 255.255.255.0 UG 0 0 0 eth0 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 10.0.0.0 192.168.1.254 255.0.0.0 UG 0 0 0 eth0 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
To add a route to a specific host use -host
option. We must only indicate the gateway to use.
route add -host <host_IP_address> gw <gateway>
For example, let's add a specific host '10.0.0.10' and then display the routing table
# route add -host 10.0.0.10 gw 192.168.1.1 eno1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eno1 10.0.0.0 192.168.1.1 255.0.0.0 UG 0 0 0 eno1 10.0.0.10 192.168.1.1 255.255.255.255 UGH 0 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 600 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
In the above examples, the flag column has differents values and that can be incomprehensible.
The possible flags and its definition:
U
- route is up
H
- target is a host
G
- use gateway
R
- reinstate route for dynamic routing
D
- dynamically installed by daemon or redirect
M
- modified from routing daemon or redirect
A
- installed by addrconf
C
- cache entry
!
- reject route
3) Reject and Deleting a route
We can manipulate the routing table not only to add statics routes but also to delete or reject a route that we insert early.
To delete a destination network from the routing table use route del
commad:
route del -net <network_address> gw <gateway> <interface_name>
For example to delete the route to our 10.0.0.0/8 network:
# route del -net 10.0.0.0/8 gw 192.168.1.1 eno1
Output
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eno1 10.0.0.10 192.168.1.1 255.255.255.255 UGH 0 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 600 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
To ban an IP address but it is still on the routing table. Note that we still use the add option parameter but at the end of the command, we have to use reject
option. We can conserve our gateway or not but we don't mention the output interface.
route add -host <host_IP_address> reject
In the following command we will reject our host '10.0.0.10':
# route add -host 10.0.0.10 reject
Display routing table after the changes:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 600 0 0 eno1 10.0.0.10 - 255.255.255.255 !H 0 - 0 - 10.0.0.10 192.168.1.1 255.255.255.255 UGH 0 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.16.20.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet8 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-707c8e2f7441 192.168.1.0 0.0.0.0 255.255.255.0 U 600 0 0 eno1 192.168.161.0 0.0.0.0 255.255.255.0 U 0 0 0 vmnet1
The routes added using the above commands is not persistent over a reboot, so we have add static routes to the config file to keep it permanent.
To add persistent static routes for any Linux distribution, you can use the generic file /etc/rc.local.
For Ubuntu and Debian use file named '/etc/network/interfaces' and on RHEL/CentOS use '/etc/sysconfig/network-scripts/route-ethX'.
Conclusion
In this tutorial, we learned how to add static routes in Linux using route add command. We must note that with that method, the route will not change unless you modify it. It is important to delete routes that we don't need.
Related Read: How to Permanently add Static Route in Linux
Thanks for posting, however route command is not available by default .
[root@LINUX ~]# route -n
-bash: route: command not found
You need package net-tools , yum install net-tools needed.
Thanks Ranjeet.
This will include in the deprecated networking commands and replacement for net-tools is iproute2.