Enable IP Forwarding in Linux

What is IP Forwarding?

Normally, a system can not communicate with another system belonging to a different network address. IP forwarding is the mechanism of forwarding an IP packet from one network (example: 192.168.1.0) to another network (example: 192.168.2.0).

How to enable IP Forwarding in Linux?

By default, IP forwarding is disabled in linux. The current setting can be verfied using the command:

cat /proc/sys/net/ipv4/ip_forward

This will give the output: 0

Another way to test is to run:

sysctl net.ipv4.ip_forward

This will give the output:

net.ipv4.ip_forward = 0

Where 0 means disabled and 1 means enabled.

Enabling IP Forwarding for the Current Running Kernel

Running either of commands will perform the task:

sysctl -w net.ipv4.ip_forward = 1

or

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

But this is only for the current running kernel session. After reboot the old values will be restored.

Permanently Enabling IP Forwarding

Open the required in VIM or any other text editor:

vim /etc/sysctl.conf

Locate the line and modify it as under:

net.ipv4.ip_forward = 1

But these changes will not take effect unless the system is restarted or the command is run:

sysctl -p /etc/sysctll.conf

For Redhat systems, restarting the network service will automatically reload the changes to sysctl.conf:

service network restart

On Ubuntu, this is also possible by restarting the procps service:

/etc/init.d/procps.sh restart

For Debian distributions open the /etc/network/options and make the following changes and restart the network service or reboot:

ip_forward = yes

For Redhat distribution open /etc/sysconfig/network and do the same:

FORWARD_IPV4 = true

The changes can be viewed using the commands mentioned above.