There comes a time when you need to use your computer to forward traffic from one place to another. While in Windows it might be a little more difficult to do, in Linux it is a piece of cake(literally!). Here, we are going to enable IP forwarding in Linux.
IP forwarding is usually reserved for router systems with multiple ports or NICs(Network Interface Card). Traffic is forwarded from one network interface to another. Usually without interacting with the local Linux interface lo.
It can also be used for a VPN to route incoming and outgoing traffic through the server’s LAN network. It is imperative to do so otherwise traffic can only go in/out of the VPN interface.
This may seem easier to do in Linux distributions such as Debian. IP forwarding is usually configured through the system file sysctl.conf, located in /etc/. Let’s go ahead and open it by running the following command:
sudo nano /etc/sysctl.conf
Sometimes, you will find the option having already been written into the config file. net.ipv4.ip_forward=1 is what we’re looking for to enable IP forwarding. Currently, it is a comment because of the # therefore disabled. Go ahead and remove it then CTRL+O to save and CTRL+X to exit.
We still need to apply the new settings to our system. In order to do that, type:
sudo sysctl -p
The command prints out net.ipv4.ip_forward=1 indicating success.
With CentOS, it’s a little bit different. The sysctl file is still in /etc/, but is actually located under the /etc/sysctl.d/ folder. This file is blank and requires you to add the option net.ipv4.ip_forward=1 manually. The easiest way of doing that would be this:
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/99-sysctl.conf
Run the command sysctl -p to apply your changes to the system.