In this tutorial, we will learn how to add permanent static routes in Linux (redhat/centos) machine. Static routing is the process of manually entering the routes to the routing table to reach a particular destination. So, the data from one point to another uses the same path regardless of other considerations. The main advantages of static routes are its control, predictability and improved performance.
Temporarily adding static routes
In order to add a static route temporarily on your Linux machine, you can use route command. The newly added route will be available till next reboot. You can list the current routing table as follows.
# route –n
If you want to add a route to the network 22.214.171.124 through gateway 126.96.36.199, you can execute the following command.
# route add -net 188.8.131.52 netmask 255.255.255.0 gw184.108.40.206dev eth0
Adding Permanent Static routes
In order to add permanent static routes, you need to modify the interface file in /etc/sysconfig/network-scripts. For example, here, we have to add routes on eth0. Hence, the file we need to modify will be '/etc/sysconfig/network-scripts/route-eth0'.
The basic syntax of static route in this file is
Target_address via gateway_address
So, in order to add the above route in route-eth0 file, append the following line.
220.127.116.11/24 via 18.104.22.168
Then, you need to restart the network service.
# service network restart
Finally, verify that the new routes are visible in the routing table using the following command.
# route -n