You get a bonus - 1 coin for daily activity. Now you have 1 coin

Enabling routing in Debian and Ubuntu

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 for IPv4, and sysctl net.ipv6.conf.all.forwarding for IPv6.

By setting their values to 1 we enable routing, and by resetting to 0 - we disable it:

# sysctl net.ipv4.ip_forward=1
net.ipv4.ip_forward 0 --> 1
# sysctl net.ipv6.conf.all.forwarding=1
net.ipv6.conf.all.forwarding 0 --> 1

For these values to apply after a reboot, you need to edit the file /etc/sysctl.conf - uncomment or add (if they aren't there yet) the lines:

net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1

Naturally, if you want to enable only, say, IPv4 routing - you can change just one variable.


Setting routes

The system utility route lets us set static routes. For example, the following command will add a route to network 10.0.5.0/24 via router 10.0.1.1:

# route add -net 10.0.5.0/24 gw 10.0.1.1

We can view the routes via the netstat command:

# netstat -rn

And to remove a route - again via route:

# route delete -net 10.0.5.0/24

However, these routes will be lost on the very first reboot. To have static routes automatically set on every OS boot, they need to be added to the file /etc/network/interfaces. Here is an example of such a file.

auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet static
address 10.0.0.1
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
gateway 10.0.0.250
dns-nameservers 10.0.0.2
up route add -net 10.0.1.0/24 gw 10.0.0.201
up route add -net 10.0.2.0/24 gw 10.0.0.202
up route add -net 10.0.3.0/24 gw 10.0.0.203

allow-hotplug eth1
iface eth1 inet static
address 10.0.14.1
netmask 255.255.255.0
network 10.0.14.0
broadcast 10.0.14.255

Here we can see that we used the up directive, which fires when the interface is "brought up". In this case - when interface eth0 comes up (for example, when the cable is plugged in or the system starts) the route commands will be executed immediately, setting the necessary routes.
created: 2017-05-10
updated: 2026-03-09
2906



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


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