How to enable IP forwarding on Linux (IPv4 / IPv6)

September 10, 2020 / How-to Guide

IP forwarding is the feature that allows Linux OS to receive network packets arriving on one interface and then forward them to another network. Most Linux distributions disable IP forwarding because not everyone need this feature. However, you have to enable IP forwarding if you want to set up a Linux router, gateway, VPN server, or connect multiple networks together.

Enable IP Forwarding

There are two ways to enable IP forwarding on Linux: Temporary activation and Permanent Activation

 

First we will check if current IP forwarding is enabled or not

Here we have to query the sysctl kernel value net.ipv4.ip_forward to check if IP forwarding is enabled or not: Using sysctl:

sysctl net.ipv4.ip_forward 
net.ipv4.ip_forward = 0

Or just check out the value in the /proc system:

cat /proc/sys/net/ipv4/ip_forward 
0

As per the above examples, we can see this was disabled (as shown by the value 0).

Temporary Activation

The activation and deactivation of IP forwarding, in IPv4, as does IPv6 is handled in /proc files. This is /proc/sys/net/ipv4/ip_forward” for IPv4 and “/proc/sys/net/ipv6/conf/all/forwarding for IPv6.

If we do a “cat” on these files, we will see that they are by default to 0, to activate the IP forwarding temporarily, it is enough to put them at 1. One can then modify the file or use the “sysctl” command:

| sysctl -w net.ipv4.ip_forward=1

Or, following is one more command to enable temporary:

| echo 1 > /proc/sys/net/ipv4/ip_forward
Please note that this is a temporary activation. So the IP forwarding setting will return to its default value when the machine is restarted.
 

Now, let’s check out the Permanent Solution for it.

Permanent Activation

To enable these changes permanently, you have to modify the configuration file of sysctl so that it loads our modifications each start. It is the file “/etc/sysctl.conf” to activate IPv4 IP forwarding. We will add or uncomment this line:

| net.ipv4.ip_forward = 1

For IPv6, we will do the same with this line:

| net.ipv6.conf.all.forwarding=1

The configuration can then be reloaded so that the changes take effect immediately:

| sysctl -p /etc/sysctl.conf

That’s it! You have successfully performed the activation.

You have now successfully set up IP forwarding on your Linux system by following these steps. It can now forward network packets between networks.

 

Also Read : How to add an IPv6 range via WHM

Dominos Search