Share your love
How To Enable IP Forwarding in Linux

In this tutorial, we want to show you How To Enable IP forwarding in Linux. Also, you will learn to Disable IP Forwarding in Linux.
“IP forwarding” is a synonym for “routing.” It is called “kernel IP forwarding” because it is a feature of the Linux kernel.
A router has multiple network interfaces. If traffic comes in on one interface that matches a subnet of another network interface, a router then forwards that traffic to the other network interface.
When enabled, “IP forwarding” allows a Linux machine to receive incoming packets and forward them.
Steps To Enable and Disable IP Forwarding in Linux
To complete this guide, you need privileged access to your Linux system as a root or non-root user with sudo privileges.
Now follow the steps below.
Check IP Forwarding Status
First, you must check your current IP forwarding status that is enabled or disabled on your server. To do this, you can use the following command:
sysctl net.ipv4.ip_forward
Example Output
net.ipv4.ip_forward = 0
In this example output, you will see that net.ipv4.ip_forward = 0. It means that your IP forwarding is disabled. If it were set to 1, that would mean it’s enabled.
Alternatively, you can use the following command:
cat /proc/sys/net/ipv4/ip_forward
Output
0
Enable IP Forwarding in Linux
At this point, you can easily enable your IP forwarding by using the following command:
sysctl -w net.ipv4.ip_forward=1
Also, you can use the following command instead the above command:
echo 1 > /proc/sys/net/ipv4/ip_forward
Next, you need to make sure that your changes apply to the system reboot. To do this, you need to edit the /etc/sysctl.conf file. Open the file with your favorite text editor, here we use vi:
vi /etc/sysctl.conf
Add the following line to the bottom of the file:
net.ipv4.ip_forward = 1
When you are done, save and close the file.
To apply the changes, run the command below:
sysctl -p
Disable IP Forwarding in Linux
Disabling IP forwarding in Linux is the same step as enabling it. To disable it, run the command below:
sysctl -w net.ipv4.ip_forward=0
Or, you can use the following command instead:
echo 0 > /proc/sys/net/ipv4/ip_forward
To make sure the new setting survives a reboot, open the /etc/sysctl.conf file:
vi /etc/sysctl.conf
Add the following line to the bottom of the file:
net.ipv4.ip_forward = 0
When you are done, save and close the file.
To apply the changes, run the command below:
sysctl -p
IP Forwarding Troubleshooting
If you have successfully enabled the Linux IP forwarding (verified by checking the kernel variable after reboot), but you’re still not receiving traffic on destination systems, check the FORWARD rules of iptables. To do this, run the command below:
iptables -L -v -n
Your FORWARD chain should either be set to ACCEPT or have rules listed that allow certain connections. You can see if traffic is reaching the FORWARD chain of iptables by checking the number of packets and bytes that have hit the chain. If there aren’t any, then you may have some higher rules in your chain that are blocking traffic.
Manage sysctl Command
If the sysctl command is not activated on your server, you can use the following command to start your service:
sudo systemctl start sysctl
Conclusion
At this point, you have learned to Enable and Disable IP forwarding in Linux.
Hope you enjoy it.
You may be like these articles:
Stress tests and benchmark CPU performance in Ubuntu