Enabling routing on Linux CentOS or RedHat

Practice



All we need is a console.

Enabling routing

Enabling and disabling routing in the OS kernel is controlled by the sysctl variable net.ipv4.ip_forward.

Setting its value to 1 enables routing, resetting it to 0 - disables it:

# sysctl net.ipv4.ip_forward=1
net.ipv4.ip_forward 0 = 1

For these values to apply after a restart, you need to edit the file /etc/sysctl.conf - uncomment or add (if it's not there yet) the line:

net.ipv4.ip_forward=1


Setting routes

The 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 10.0.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 10.0.5.0/24


Static routes after OS restart

However, 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 written into the configuration.

The default gateway is set in the file /etc/sysconfig/network via the GATEWAY directive:

...
GATEWAY=192.168.0.1
...

where 192.168.0.1 is the gateway.

Static routes are set in the route-ethX files. Instead of ethX you need to use the interface name, for example, eth0. These files are located in

/etc/sysconfig/network-scripts

Here's an example of the /etc/sysconfig/network-scripts/route-eth0 file:

192.168.5.0/24 via 192.168.0.1
192.168.10.0/24 via 192.168.0.2

Here static routes are set:
  • To reach subnet 192.168.5.0/24 - go through router 192.168.0.1
  • To reach subnet 192.168.10.0/24 - go through router 192.168.0.2

That is, this is the same as if, right after a reboot, the following commands had been issued:

# route add -net 192.168.5.0/24 gw 192.168.0.1
# route add -net 192.168.10.0/24 gw 192.168.0.2

Well, that's basically it. Now you can use your CentOS (or RedHat) as a router.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Computer networks"

Terms: Computer networks