Unable to forward all traffic from one interface to another with iptables

Abstract: Resolve Linux traffic forwarding failures by enabling kernel IP forwarding and configuring iptables NAT masquerade rules.

Created: 2026-09-12 by
To successfully forward all traffic between two network interfaces on Linux, you must enable IP forwarding in the kernel and configure iptables with NAT rules. The most common failure occurs because the kernel drops packets by default or because return traffic is not masqueraded. This guide provides the exact commands to resolve these issues permanently.

Understanding Kernel-Level Forwarding

Before touching firewall rules, the Linux kernel itself must permit packet routing between interfaces. By default, many distributions disable this feature for security reasons. If you attempt to route packets without enabling this setting, the system will silently drop them regardless of your iptables configuration. You can check the current status by reading the value of net.ipv4.ip_forward. A value of 0 indicates that forwarding is disabled, while 1 means it is active. To temporarily enable forwarding for the current session, use the following command:
sysctl -w net.ipv4.ip_forward=1
This change takes effect immediately but will revert after a reboot. To make this persistent across reboots, you need to modify the sysctl configuration file. Open /etc/sysctl.conf or create a new file in /etc/sysctl.d/ and add the line net.ipv4.ip_forward=1. Then apply the changes using sysctl -p. Without this step, no amount of iptables magic will allow traffic to traverse from one interface to another.

Configuring NAT Rules for Outbound Traffic

Once kernel forwarding is enabled, you must handle Network Address Translation (NAT). When a client on your internal network sends a packet to an external server, the source IP is private (e.g., 192.168.1.x). The external server will reply to that private address, which is unreachable from the public internet. To fix this, you need to masquerade the source address of outgoing packets. Assuming eth0 is your internal interface and eth1 is your external gateway interface, the command looks like this:
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
The -t nat flag specifies the NAT table. The POSTROUTING chain is used because we are modifying the packet just before it leaves the system. The -o eth1 option ensures the rule only applies to traffic exiting via the external interface. The MASQUERADE target replaces the source IP with the IP address of eth1. This is dynamic and ideal for DHCP-assigned IPs where the external IP might change. If your external IP is static, you can use SNAT instead with the --to-source flag. For a more comprehensive approach, consider reading Optimizing Firewall Configuration with Iptables: A Comprehensive Guide, which covers advanced filtering strategies alongside basic NAT.

Allowing Forwarded Packets in the Filter Table

Enabling NAT does not automatically mean the firewall allows the traffic through. The filter table, which handles standard input, output, and forwarding decisions, often has a default policy of DROP or REJECT for the FORWARD chain. If this chain is restrictive, packets will be dropped even if NAT is correctly configured. You need to explicitly allow traffic to pass through the router. Use the following command to accept forwarded packets:
iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
This rule accepts traffic coming from the internal interface (eth0) and going out to the external interface (eth1). You may also need to allow return traffic:
iptables -A FORWARD -i eth1 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
The -m state --state ESTABLISHED,RELATED module ensures that only responses to existing connections or related new connections are allowed back into the internal network. This prevents unauthorized incoming connections from bypassing your security posture. For more details on securing specific addresses, see Block MAC Address with Iptables During Attack.

Troubleshooting Common Issues

If traffic still does not flow, verify your routing table. Run ip route to ensure that the default gateway points to the correct next-hop address for the external interface. Additionally, check if any other firewall tools like firewalld or ufw are interfering. These services often manage their own iptables chains and may override or conflict with manual rules. Disabling them or ensuring they are aware of your custom NAT rules is crucial. Another frequent issue is DNS resolution. Ensure that clients on the internal network are using a DNS server that is reachable via the forwarded path. If the DNS server is on the same subnet as the external interface, clients might fail to resolve names unless the router forwards UDP port 53 traffic specifically. You can test connectivity using ping from the internal client to an external IP address. If ping works but web browsing fails, the issue is likely DNS-related rather than a forwarding problem. For broader connectivity diagnostics, refer to Troubleshooting Home Network Connectivity Issues on Linux Computer. Finally, save your iptables rules so they survive a restart. On Debian-based systems, install iptables-persistent and run dpkg-reconfigure iptables-persistent. On RHEL/CentOS systems, use service iptables save or iptables-save > /etc/sysconfig/iptables. Without saving, your configuration will be lost upon reboot, leading to intermittent failures that are difficult to diagnose.

Frequently Asked Questions

Why does ping work but HTTP traffic fail?

This usually indicates a DNS resolution issue rather than a forwarding problem. Since ICMP echo requests do not require domain name resolution, they may succeed while TCP connections to websites fail if the client cannot reach the DNS server. Check the client's resolv.conf settings and ensure UDP port 53 is allowed in your FORWARD chain.

Can I use SNAT instead of MASQUERADE?

Yes, SNAT is suitable if your external interface has a static IP address. MASQUERADE is preferred for dynamic IPs because it queries the interface's current IP at runtime. Using SNAT with a dynamic IP will break connectivity whenever the ISP changes the assigned address.

How do I check if iptables is blocking my traffic?

Use the command iptables -L -v -n to list all rules with packet counters. Look at the pkts column in the FORWARD chain to see if packets are being matched and accepted. If the counter is zero, the traffic is either not reaching the chain or is being dropped by a default policy before matching any explicit rules.

Does enabling ip_forward expose my system to attacks?

Enabling forwarding alone does not open ports, but it allows your system to act as a router. Without proper firewall rules, this could allow malicious actors to traverse your network. Always pair ip_forward with strict FORWARD chain policies that only allow established or explicitly permitted traffic.

What happens if I forget to save iptables rules?

All rules will be lost when the system reboots, causing immediate loss of internet access for clients behind the router. You will need to manually re-enter all NAT and filter rules after every restart. Using persistence packages like iptables-persistent automates this process and prevents service interruptions.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More