When working with CentOS 6.x systems, administrators often encounter confusion about the proper method for adding static routes that persist across reboots. The two most common approaches - using route-ethX
files versus static-routes
- stem from different documentation sources and legacy system behaviors.
The current best practice is to create interface-specific route files in /etc/sysconfig/network-scripts/
. For your example targeting eth0:
# Create/edit the route file
sudo vi /etc/sysconfig/network-scripts/route-eth0
# Add your route definition
192.168.20.0/24 via 192.168.20.253 dev eth0
While still functional in some distributions, this older approach uses a global static routes file:
sudo vi /etc/sysconfig/static-routes
# Format differs from interface-specific files
any net 192.168.20.0 netmask 255.255.255.0 gw 192.168.20.253 eth0
After making changes, apply them without reboot:
sudo service network restart
route -n
Common pitfalls to check:
- File permissions (should be 644)
- Correct interface name (match your actual interface)
- Network service running status
For complex routing needs, you might need multiple routes:
# Multiple routes in route-eth0
10.0.0.0/8 via 10.1.1.1 dev eth0
172.16.0.0/12 via 10.1.1.2 dev eth0
default via 10.1.1.254 dev eth0
For temporary testing (not persistent):
sudo ip route add 192.168.20.0/24 via 192.168.20.253 dev eth0
CentOS 6.x has an interesting quirk when it comes to static route configuration. There are actually two valid methods, but they behave differently across minor version updates:
# Method 1: Interface-specific route files
/etc/sysconfig/network-scripts/route-eth0
# Method 2: Global static routes file
/etc/sysconfig/static-routes
This is the preferred approach in newer CentOS 6.x versions. Create a file named after your network interface:
sudo vi /etc/sysconfig/network-scripts/route-eth0
Add your routes using either of these formats:
# CIDR notation format
192.168.20.0/24 via 192.168.20.253 dev eth0
# Traditional format
ADDRESS0=192.168.20.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.253
For older CentOS 6 installations, you might need to use:
sudo vi /etc/sysconfig/static-routes
The content format is different:
eth0 net 192.168.20.0 netmask 255.255.255.0 gw 192.168.20.253
If your routes aren't applying:
# Check which method your system prefers
ls /etc/rc.d/rc*.d/*network | grep static-routes
# Apply changes without reboot
service network restart
# Verify routes
route -n
ip route show
For temporary testing (lost on reboot):
# Using route command
route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.20.253 eth0
# Using ip command (modern approach)
ip route add 192.168.20.0/24 via 192.168.20.253 dev eth0
Remember to check /var/log/messages
for any network service errors after applying changes.