Trying to understand the correct way to create a static route in CentOS, please assist

Solution 1:

Create a file in /etc/syconfig/network-scripts/route-eth0

add add the following

192.168.20.0/24 via 192.168.20.253 dev eth0

I have always used this approach. I have found this to be the best approach.

FYI: Check -- https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html

Solution 2:

RH style device dependent routes defined using /etc/sysconfig/network-scripts/route-device files has caused lots of problems.

So real sysadmins use only /etc/sysconfig/static-routes file without device dependency:

any net 10.0.0.0 netmask 255.255.255.0 gw 192.168.0.1

Problems:

  • When physical devices are bonded, you need to remember to chance route-device file too
  • When you reorganize adapters in a virtual machine.

Naturally one should always use bridge devices, so one could avoid route-device file problems.

Also notice the syntax in /etc/sysconfig/static-routes file, sniplet from /etc/init.d/network:

    # Add non interface-specific static-routes.
    if [ -f /etc/sysconfig/static-routes ]; then
       if [ -x /sbin/route ]; then
           grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
               /sbin/route add -$args
           done
       else
           net_log $"Legacy static-route support not available: /sbin/route not found"
       fi
    fi

Solution 3:

There is an easier way to add routes...

This file - /etc/init.d/network - is launched when the PC is booting, and it uses a file /etc/sysconfig/static-routes to add static routes

You have to create it because it doesn't exist.

If you read carefully the file /etc/init.d/network, it reads in this file each line to add routes, those line must begin by "any", and "route add -" is already known.

So in the file you are going to create >> /etc/sysconfig/static-routes, you have to write :

any net 10.0.0.0 netmask 255.255.255.0 gw 192.168.0.1 eth0
  • where 10.0.0.0/24 is the network you want to reach (255.255.255.0 is the mask)
  • where gw 192.168.0.1 is the gateway to reach (certainly the router)
  • where eth0 is the interface where to use.

This is the best way for adding static routes, 1 file for everything (not X files for X interfaces)