How to enable IP forwarding in Linux

Written by
Date: 2011-01-23 10:36:30 00:00

Spanish version


If you want to use a Linux Operating System driven box, to act as a router or gateway, you need IP packets to pass through your Linux box. By default packets not directly addressed to a Linux powered PC will be discarded, so you need to enable IP Forwarding.

This can be done in different ways, and we’ll check here some of the most common.

Use procfs

This is maybe the most used way, it is a temporary change, and you need to enable it after every reboot.

 echo 1 > /proc/sys/net/ipv4/ip_forward

You can add this line to /etc/rc.local file, and that way, each time you reboot your computer it will be enabled again.

You can check if IP forwarding is enabled or disabled by checking the content of /proc/sys/net/ipv4/ip_forward file

 cat /proc/sys/net/ipv4/ip_forward

If the output is 1, it is enabled if 0, then it is disabled.

Use sysctl

sysctl let’s you change Kernel values on the fly, so you can use it, to change the IP forward behaviour of your Linux.

First, let’s check if it is enabled or disabled, as root run:

 sysctl -a | grep net.ipv4.ip_forward

Now you can set its value to 1, to enable ip forwarding.

 sysctl -w net.ipv4.ip_forward=1

This is also temporary, if you want it to be permanent, you can edit the file /etc/sysctl.conf

And modify or add this line:

 net.ipv4.ip_forward = 1

Now let Linux load the changes you’ve made.

 sysctl -p

As root.

You’re done, now you have IP forward enabled on your Linux, you can now proceed to configure your Linux router or Linux gateway.

This works for Ubuntu, Debian, Fedora, Slackware or any other Linux distribution

If you liked this article please share it.