Routing in CentOS/RedHat - routes on the flyThe system utility route lets us set static routes. For example, the following command will add a route to the 10.0.5.0/24 network via router 10.0.1.1:
# route add -net 192.168.5.0/24 gw 10.0.1.1
We can view the routes with the netstat command:
# netstat -rn
And a route can be removed - again via route:
# route delete -net 192.168.5.0/24
Permanent routes - persisting after rebootHowever, these routes will be lost on the very first reboot. For static routes to be automatically applied on every OS boot, they need to be set in configuration files.
Routes in RHEL are written into files tied to specific network interfaces (network cards).
For example, if traffic on this route should go through eth0 - then this route is written in the configuration for eth0. The same applies to eth1, and so on.
Routes are stored in the files /etc/sysconfig/network-scripts/route-ethX, where ethX is the network interface number. For example:
/etc/sysconfig/network-scripts/route-eth0 : routes for eth0
/etc/sysconfig/network-scripts/route-eth1 : routes for eth1
/etc/sysconfig/network-scripts/route-ppp0 : routes for ppp0
These files don't exist initially - they should be created when you need to set static routes for the required network card.
Here is an example of such a file (let's say it's route-eth0):
192.168.1.0/24 via 192.168.0.1
192.168.2.0/24 via 192.168.0.1
192.168.15.0/24 via 192.168.0.2
10.0.0.0/16 via 192.168.0.250
In this example:
- Traffic to the 192.168.1.0/24 subnet - goes through router 192.168.0.1
- Traffic to the 192.168.2.0/24 subnet - goes through router 192.168.0.1
- Traffic to the 192.168.15.0/24 subnet - goes through router 192.168.0.2
- Traffic to the 10.0.0.0/16 subnet - goes through router 192.168.0.250
As you can see - it's all quite simple.
ApplyingTo apply the changes made in these files - let's restart the network subsystem:
$ sudo /etc/init.d/network restart
Comments