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
Create a file in /etc/syconfig/network-scripts/route-eth0
add add the following
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
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:Problems:
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
: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 routesYou 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 :
This is the best way for adding static routes, 1 file for everything (not X files for X interfaces)
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 :You can also edit this file using the following format (personally i prefer the first syntax) :
Then restart the network to apply the changes :
Further reading : https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s1-networkscripts-static-routes.html
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.
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