How To Enable IP Forwarding in Linux with Easy Steps

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. You can now proceed to the guide steps below on the Orcacore website to enable and disable IP Forwarding in Linux.

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 to enable the Linux IP forwarding.

1. Check IP Forwarding Status

First, you must check your current IP forwarding status, which is enabled or disabled on your server. To do this, you can use the following command:

sysctl net.ipv4.ip_forward
Enable IP Forwarding in Linux - Check Status

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
Check IP Forwarding in Linux

2. 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 of 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

3. 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

4. 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.

5. 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. Enabling IP forwarding in Linux allows the system to route network traffic between interfaces, acting as a gateway. Disabling IP forwarding prevents the system from routing traffic, limiting it to only local communications.

Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.

You may also like these articles:

Stress tests and benchmark CPU performance in Ubuntu

5 Ways To Check your Ubuntu Version

Allow a Specific Port for a Particular IP in CSF

Share your love

Stay informed and not overwhelmed, subscribe now!