html
When working with CentOS 5 systems, network administrators often need to add static routes manually. The standard ip route add
command we're familiar with:
ip route add 123.456.7.89/32 via 192.168.10.101 dev eth1
creates routes that disappear after system reboot. While adding this to /etc/rc.d/rc.local
works, it's not considered best practice for proper system configuration management.
For persistent static routes in CentOS 5, we should use the network configuration directory structure. Here's the recommended approach:
1. Creating the route-eth1 File
# Create or edit the route file for the specific interface
vi /etc/sysconfig/network-scripts/route-eth1
# Add the following content:
123.456.7.89/32 via 192.168.10.101 dev eth1
2. Alternative Format Option
For more complex routing needs, you can use this alternative format in the same file:
ADDRESS0=123.456.7.89
NETMASK0=255.255.255.255
GATEWAY0=192.168.10.101
INTERFACE0=eth1
After creating the configuration file, restart the network service:
service network restart
Verify the route is active with:
ip route show
# or
route -n
For systems requiring multiple static routes, you can add them all to the same file:
# /etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 192.168.10.101 dev eth1
172.16.0.0/12 via 192.168.10.102 dev eth1
192.168.2.0/24 via 192.168.10.103 dev eth1
For environments using NetworkManager (unlikely in CentOS 5 but worth mentioning):
chkconfig NetworkManager off
service NetworkManager stop
The configuration files method is more reliable and maintainable than rc.local modifications, especially in enterprise environments where configuration management tools might be in use.
When working with CentOS 5 systems, manually added static routes using the ip route
command are temporary by default. The route we added:
ip route add 123.456.7.89/32 via 192.168.10.101 dev eth1
will disappear after reboot, which can cause connectivity issues for critical services.
While adding the route command to /etc/rc.d/rc.local
works, it's not the most elegant solution. The rc.local file is typically meant for user-specific commands, not system configuration.
For CentOS 5, we have several better alternatives:
1. Using /etc/sysconfig/network-scripts/
Create a route-eth1 file in the network scripts directory:
# /etc/sysconfig/network-scripts/route-eth1
123.456.7.89/32 via 192.168.10.101 dev eth1
This method is preferred as it follows the standard RHEL/CentOS network configuration pattern.
2. Static Routes in ifcfg-eth1
You can also add the route directly to the interface configuration file:
# /etc/sysconfig/network-scripts/ifcfg-eth1
[...]
ADDRESS0=123.456.7.89
NETMASK0=255.255.255.255
GATEWAY0=192.168.10.101
3. Using /etc/sysconfig/static-routes (Deprecated)
Older versions of CentOS used this method, though it's considered deprecated now:
# /etc/sysconfig/static-routes
eth1 net 123.456.7.89 netmask 255.255.255.255 gw 192.168.10.101
After making changes, restart networking and verify:
service network restart
ip route show
You should see your persistent route in the output.
For multiple routes, simply add additional lines to your route-eth1 file:
# /etc/sysconfig/network-scripts/route-eth1
123.456.7.89/32 via 192.168.10.101 dev eth1
203.0.113.5/32 via 192.168.10.102 dev eth1
Remember that CentOS 5 is quite old and no longer receiving security updates. Consider upgrading to a newer version where network configuration might differ slightly. Also ensure your gateway address (192.168.10.101 in this case) is reachable through the specified interface.