Master Enabling/Disable IP (ipv4) Forwarding On Linux Ubuntu 22.04

Posted September 15, 2023
Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04s

Master how to Enable and Disable IP (ipv4) Forwarding in your Linux Ubuntu 22.04 server in this Ubuntu IP Forwarding guide

Your Linux server relies on IP forwarding to route traffic between networks. A Linux server has IP forwarding disabled by default. If you use a router, gateway, firewall, or VPN server, the Linux kernel requires IP forwarding enabled to pass packets around the server. Here’s how to enable and disable IP forwarding to your liking.

Checking the Current Status of Ubuntu IP Forwarding

Linux kernel saves the IP forwarding state in the configuration file /etc/sysctl.conf. It contains a net.ipv4.ip_forward variable that holds the current IP forwarding status. To access the variable, run the sysctl command and pass the net.ipv4.ip_forward parameter to review the current IP forwarding status:

sysctl net.ipv4.ip_forward

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

This command can only output two values:

  • net.ipv4.ip_forward = 0 tells you the IP forwarding has been enabled.
  • net.ipv4.ip_forward = 1 means your Linux system has IP forwarding disabled.

Let’s go through the steps of enabling/disabling IP forwarding.

Temporarily Enabling/Disabling Ubuntu IP Forwarding

A Linux kernel uses the parameterip_forward in the /proc/sys/net/ipv4/ip_forward configuration file to temporarily hold the IP forwarding states. IP forwarding is disabled when the file contains a 0 and 1 if enabled.

To enable IP forwarding temporarily, run the command echo command to write a 1 value to the ip_forward file:

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

If IP forwarding is permanently enabled, and you want to temporarily turn it off, change the /proc/sys/net/ipv4/ip_forward file value 0:

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

To get the temporary value of ip_forward, use the cat command pointing to where these values are saved:

cat /proc/sys/net/ipv4/ip_forward

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

These values are short-lived in the system. Changing the ip_forward variable doesn’t persist these changes. The system will turn the setting on or off. However, the value will reset to default the next time Linux is rebooted.

You might want to use the temporary approach if your packets forwarding is short-lived and you don’t want your system to use a permanent IP forwarding state.

Permanently Enabling/Disabling Ubuntu IP Forwarding

IP forwarding permanent settings are saved in the /etc/sysctl.conf. Any changes added to this file will persist in your next boot.

To turn IP forwarding on/off, you’ll edit /etc/sysctl.conf configurations. Open the file editing mode using nano or vim based on your preference.

Ensure you have the root user privileges or use the sudo command directly.

Run

sudo nano /etc/sysctl.conf

To open this file.

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

Add one line based on whether you want to turn Linux IP forwarding on or off:

# Enabling the IP forwarding
net.ipv4.ip_forward = 1
# Turning off IP forwarding 
net.ipv4.ip_forward = 0

Here’s an example of the net.ipv4.ip_forward parameter set to 1:

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

Save the changes and exit the text editor. To apply them, you can either reboot the system or reload the sysctl configuration settings root privileges:

sudo sysctl -p

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

Check the status of IP forwarding and confirm if these changes were applied in your system:

sysctl net.ipv4.ip_forward

Master Enabling/Disabling IP Forwarding in Linux Ubuntu 22.04

# The IP forwarding is enabled if: 
net.ipv4.ip_forward = 1 
# If you have IP forwarding disabled: 
net.ipv4.ip_forward = 0 

Conclusion

IP forwarding routes traffic between networks to forward communication between them. If your Linux server doesn’t send packets to any network, always enable IP forwarding so you don’t waste server resources.

Master Enabling/Disable IP (ipv4) Forwarding On Linux Ubuntu 22.04

Written By:

Joseph Chege