Adding persistant static routes in linux

Let’s say that you have a box that lives on subnet /24. Your default route is set in /etc/sysconfig/network as, as shown below:


Now you need to get to network /24 using gateway and network /24 using gateway One option is to add those routes manually with the following commands:

# route add [-net|-host] netmask gw dev X

route add -net netmask gw
route add -net netmask gw

This will work in the short term, and can be doublechecked using the ‘route’ command – sample output shown below:

Destination Gateway Genmask Flags Metric Ref Use Iface * U 0 0 0 eth0 UG 0 0 0 eth0 UG 0 0 0 eth0
default UG 0 0 0 eth0

NOTE: once the machine is rebooted, those statis routes will disappear, as they are stored in memory and are not recreated on startup.

To add a persistent static route in Redhat Enterprise Linux or CentOS, create a file called route-X in the /etc/sysconfig/network-scripts/ directory where is the interface number and X is the interface number. As you would expect, these are specified in separate files for each of the available interfaces.

In this particular case, we will be creating a file called ‘route-eth0’ in /etc/sysconfig/network-scripts in order to make those routes persistent through reboot, and populating it with the information shown below

vi /etc/sysconfig/network-scripts/route-eth0 via via

Once that file has been modified, run the following command to restart the network:

service network restart

After that, run the route command and make sure that your routes are in place.

Leave a Reply

Your email address will not be published. Required fields are marked *