How to Add Persistant Static Routes in Linux
At times, when I work on my linux box I forget about the configuration file game and expect some tasks to be completed just by putting some commands in the line!
This is the second time that I forget adding my static routes to the config file and wake up after my server needs a reboot and things start going wrong afterwards!
The easy way, which works in any distribution is to simply add routes in /etc/rc.local but this is not welcomed by many professionals:
route add -net 192.168.125.0 netmask 255.255.255.0 gw 192.168.110.1
route add -net 192.168.145.0 netmask 255.255.255.0 gw 192.168.110.1
But to do it properly in Redhat and Fedora distributions we have to create a configuration file for each interface. For example, for all routes that need to go out from "eth1" a config file named "route-eth1" must be created in "/etc/sysconfig/network-scripts/route-eth1" containing the following:
(I will take the above route as example)
GATEWAY0=192.168.110.1
NETMASK0=255.255.255.0
ADDRESS0=192.168.125.0
GATEWAY1=192.168.110.1
NETMASK1=255.255.255.0
ADDRESS1=192.168.145.0
So if there are different interfaces that correspond to different routes we should expect config files like "route-eth0", "route-eth1","route-eth2" in "/etc/sysconfig/network-scripts/"
Labels: Linux, Networking



5 Comments:
You have not explained anything in this post. What does does ADDRESS0 mean in
this context? Why are there
two instances of each in your example? If this file is for one NIC, then why two sets?
Actually it depends on how you approach this article and your level of understanding linux networking.
If you think of this as 'Saeed's Notes' or 'linux routing to the point' it makes perfect sense.
Thanks Saeed, this was exactly what I was looking for as I could not remember the syntax for the route-eth* config file.
It appears to me that Saeed was not trying to explain exactly how it all works just simply How to get it done...
What the article says is that you have essentially two (or more) ways to add routes, one is the old method of adding a route statement to the '/etc/rc.d/rc.local' file using the "route add -net ..." lines in the beginning of the article.
Although this will work perfectly in large linux server environments adding anything to the /etc/rc.d/rc.local file is frowned upon because you then have to keep track of custom rc.local entries but if you want to do it that way it's fine, use one method or the other, not both, and stick to it on all servers.
The preferred method would be to create the "route-eth*' file like so;
# route-eth1 additional routes needed for ...
ADDRESS0=192.168.125.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.110.1
# end route-eth1
This will be parsed by the networking scripts upon boot and will actually do the same thing as the following entry in 'rc.local'
route add -net 192.168.125.0 netmask 255.255.255.0 gw 192.168.110.1
Which means "Any traffic destined for network 192.168.125.0 is to be routed via the interface with the address 192.168.110.1"
The syntax of "ADDRESS0", "ADDRESS1", "ADDRESS2" etc... allows the networking scripts to add multiple routes to multiple destinations via the same interface, in this case the file "route-eth1" gets parsed as the eth1 interface is brought up and routes for the 192.168.125.0 and 192.168.145.0 networks get added at that time.
If you only need one route then "ADDRESS0, NETMASK0, GATEWAY0" is all you need, if a second or third route is needed simply adding another "ADDRESS1, NETMASK1, GATEWAY1" entry would add the route.
Just remember you are adding routes here based on the IP address of the interface, since these entries appear in the "route-eth1" file it is assumed that when eth1 is brought up these routes will use it as a gateway and that eth1 will be the interface with the IP Address of 192.168.110.1
Hope this helps!
Thanks again Saeed!!
For me, thanks as well. But I have to point out (in case someone is looking for this info) that the extra info provided is not quite correct but close
The comment : Just remember you are adding routes here based on the IP address of the interface, since these entries appear in the "route-eth1" file it is assumed that when eth1 is brought up these routes will use it as a gateway and that eth1 will be the interface with the IP Address of 192.168.110.1
is wrong.
You are adding routes here based on the IP Network defined by the interface. Therefore the Gateway referenced for each route must be on the same network as the interface, ie. reachable. Since these entries above appear in the "route-eth1" file, then when eth1 is brought up these routes will be added. Conversely, when eth1 is taken down, the routes will be removed.
Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!
another question. How do you add a default route using this method?
Post a Comment
<< Home