8

I've been trying to understand what's the correct way to add a static route on a CentOS 6.x machine. In some forums they say to create a file named route-dev_name (for example route-eth0) with the relevant route and place it in /etc/sysconfig/network-scripts , in some forums they say the file should be named static-routes , in both cases I'm unable to set a static route. It seems like in some CentOS releases it works only when naming the file route-dev_name and in some it only works when naming the file static-routes.. Can anyone please assist me? This is the content of my route file:

192.168.20.0/24 via 192.168.20.253 dev eth0

Thanks in advance

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146

6 Answers6

9

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
arl
  • 376
  • 3
  • 3
8

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

vijay rajah
  • 161
  • 2
  • 9
  • 1
    Okay's that's the approach I've been using myself until now, but recently on CentOS 6.5 machines I find that the `route-devX` file is being ignored and the servers do not Up the route when the server is rebooted or the network service is being restarted, any idea what could cause it? – Itai Ganot Feb 17 '14 at 19:10
  • Not sure.. Does the interface come up on boot/restart?.. I do not have a CENTOS/RHEL 6.5 test box ATM.. I'm not sure if there is any change in ifup-routes on RHEL 6.5. – vijay rajah Feb 18 '14 at 08:24
  • Yea the interfaces operate properly... only the route is not coming up and i'm talking about freshly installed machine or about 2-3 months old. Anyways, I see that I use the correct approach so i'll just accept your question, thanks. – Itai Ganot Feb 18 '14 at 08:57
5

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)

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
Tom
  • 51
  • 1
  • 1
2

I always use the route-ethX file approach on my CentOS 6.4 and 6.5 :

Create/edit file /etc/sysconfig/network-scripts/route-ethX (where X is your interface number) and set your route in this file :

192.168.20.0/24 via 192.168.20.253 dev eth0

You can also edit this file using the following format (personally i prefer the first syntax) :

GATEWAY0=192.168.20.253
NETMASK0=255.255.255.0
ADDRESS0=192.168.20.0

Then restart the network to apply the changes :

service network restart

Further reading : https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s1-networkscripts-static-routes.html

krisFR
  • 13,280
  • 4
  • 36
  • 42
0

In /etc/sysconfig/network-scripts there's a script ifup-routes. It looks for files with the name route-dev_name, e.g. route-eth0.

sciurus
  • 12,678
  • 2
  • 31
  • 49
0

On my case the file "/etc/sysconfig/network-scripts/route-device" didnt exist.

I proceed to add the required network or ip on the file "/etc/sysconfig/static-routes" and to make it permanently, I rebooted the machine and it turned out working as expected.

it was done in a : cat /etc/centos-release CentOS Linux release 7.5.1804 (Core)

Best regards,

Manuel Lazo

Manuel Lazo
  • 131
  • 2