Enable IP Forward in Linux - Make Your Linux Computer a Gateway Device

Enable IP Forward in Linux - Make Your Linux Computer a Gateway Device
Page content

IP Forwarding

IP Forwarding is generally useful if you’re thinking of using your Linux computer as a router/gateway or a VPN server. Since most users are not going to use their computers for either of those purposes, IP forwarding is disabled by default in most Linux distributions.

Enabling IP forwarding is an easy job though. First, we must check if it is turned off or not. To do that, type the following command at the command line. Make sure you have privileges to do system-wide changes, either by having the root password or sudo permissions:

cat /proc/sys/net/ipv4/ip_forward

If the output of this command is 0, follow the instructions. If the output is 1, IP forwarding is enabled and you do not need to do anything.

Enable IP Forwarding

IP Forwarding can be turned on temporarily or permanently. We’ll first go through the steps to turn on IP forwarding temporarily. I’d like to remind all of you that these commands will only work if you have the proper permissions. This can either be in the form of a root password which can be used with the “su” command or by having relevant privileges for the “sudo” command.

Temporary:

sysctl -w net.ipv4.ip_forward=1

or

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

Either of these commands will turn on IP forwarding in the Linux kernel. Unfortunately, these settings will be lost the next time you reboot your computer. To make them permanent, you have to save the settings in a file.

These settings are stored in the file sysctl.conf in the /etc folder. To edit this file, use your favorite editor. I prefer nano for small tasks, so here’s what I type:

sudo nano -w /etc/sysctl.conf

This will open the relevant file in nano. Inside, search for the parameter “net.ipv4.ip_forward”. You can search for text using the key combination “Ctrl + W”. If it’s in the file, it should be as “net.ipv4.ip_forward = 0”. Change the 0 to 1. If the setting is not present in the file, make a new line at the end of the file and add the following:

net.ipv4.ip_forward = 1

Then, just press “Ctrl + X” to save and exit the editor. This will make the change permanent and IP forwarding will be turned on automatically at every bootup. To use the newly changed configuration file without having to restart your computer, just type the command “sysctl -p /etc/sysctl.conf”.