Port forwarding with iptables is three moving parts that must all agree: IP forwarding has to be enabled in the kernel, a DNAT rule in the nat table rewrites the destination of inbound packets, and — for the return traffic to find its way home — a MASQUERADE or SNAT rule rewrites the source. Miss any one of them and the packet either never leaves the host, never reaches the backend, or reaches it and dies there because the reply can’t route back. This article walks the packet through the nat table chain by chain, gives you copy-paste rules that actually forward, and shows how to make them persist.
What you’ll accomplish
- Enable and persist IPv4 forwarding so the kernel routes between interfaces at all.
- Forward an inbound port on the host to an internal host and port with DNAT.
- Rewrite outbound source addresses with MASQUERADE (dynamic IP) or SNAT (static IP).
- Open the forwarded flow through a default-DROP FORWARD chain, return traffic included.
- Inspect live NAT rules and connection tracking, then save the ruleset across reboots.
Prerequisites
- A Linux host acting as a gateway or forwarder, with two reachable networks (an inbound interface such as
eth0and an internal network holding the backend). - Root or
sudo, and theiptablesbinary. On Debian 12 / Ubuntu 22.04+ this isiptables-nft(iptables syntax translated onto the nftables backend) — the commands below are identical either way. - The
conntracktool for inspection (apt install conntrack), optional but recommended. - A backend that actually listens on the target address and port. Confirm before blaming NAT.
How it works
Every packet the kernel handles crosses a fixed sequence of netfilter chains. The nat table has three of them, and their order is the whole story: PREROUTING runs before the routing decision (so DNAT can change where a packet is headed), POSTROUTING runs after routing and just before the packet leaves (so SNAT/MASQUERADE can change who it appears to come from), and OUTPUT handles locally generated traffic. Between PREROUTING and POSTROUTING sits the routing decision and the filter table’s FORWARD chain, which decides whether a packet transiting the box is allowed through at all.
The critical detail is connection tracking (conntrack). NAT rules in iptables only match the first packet of a connection; conntrack records the translation and applies the reverse automatically to every subsequent packet in both directions. That is why you never write a “reverse DNAT” rule — the reply from the backend has its source rewritten back to the original destination without any rule of your own. It also means that if conntrack loses the flow (asymmetric routing, a firewall that drops the reply), the forward stops working even though your DNAT rule is still there.
Step-by-step
1. Enable IP forwarding. Nothing forwards while this is 0. Set it live, then persist it so a reboot doesn’t silently break everything.
sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
Persist it in a drop-in file rather than editing the main sysctl.conf:
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-ipforward.conf
sudo sysctl --system | grep ip_forward
net.ipv4.ip_forward = 1
2. Add the DNAT rule in PREROUTING. Redirect inbound TCP port 8080 on the host to the backend at 192.168.10.5:80. Scope it to the inbound interface so you don’t accidentally rewrite traffic arriving from the internal side.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 \
-j DNAT --to-destination 192.168.10.5:80
No output on success — iptables is silent when a rule is accepted. Verify with the listing in step 5.
3. Add MASQUERADE in POSTROUTING. Without this, the backend sees the client’s real source IP and sends its reply straight back to the client, which never asked 192.168.10.5 for anything and drops it. MASQUERADE rewrites the source to the gateway’s own address on the way out, so the reply returns to the gateway, where conntrack reverses everything.
sudo iptables -t nat -A POSTROUTING -o eth1 -p tcp -d 192.168.10.5 --dport 80 \
-j MASQUERADE
4. Allow the flow through FORWARD. If your FORWARD policy is DROP (it should be), the forwarded packets are dropped between the two NAT stages. Accept the new flow toward the backend, and accept the established return traffic coming back.
sudo iptables -A FORWARD -i eth0 -o eth1 -p tcp -d 192.168.10.5 --dport 80 \
-j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -m conntrack \
--ctstate RELATED,ESTABLISHED -j ACCEPT
The second rule is what lets replies home. Note it matches the DNAT-rewritten destination (192.168.10.5:80), because FORWARD runs after PREROUTING has already changed it.
5. List what you built. Confirm the rules landed in the right chains with packet counters, so you can watch them increment under load.
sudo iptables -t nat -L -n -v --line-numbers
Chain PREROUTING (policy ACCEPT 12 packets, 720 bytes)
num pkts bytes target prot opt in out source destination
1 4 240 DNAT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:192.168.10.5:80
Chain POSTROUTING (policy ACCEPT 8 packets, 480 bytes)
num pkts bytes target prot opt in out source destination
1 4 240 MASQUERADE tcp -- * eth1 0.0.0.0/0 192.168.10.5 tcp dpt:80
6. (Alternative) REDIRECT for a same-host remap. When the target is a port on the same machine — say you want inbound port 80 handled by a service already bound to 8080 locally — use REDIRECT instead of DNAT. It sends the packet to localhost on the new port and needs no MASQUERADE, because nothing leaves the box.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
-j REDIRECT --to-ports 8080
7. (Alternative) SNAT instead of MASQUERADE for a gateway with a fixed IP. A LAN gateway that masquerades the whole 192.168.10.0/24 out one WAN interface can use MASQUERADE (it reads the interface’s current IP each packet, ideal for DHCP/PPP links) or SNAT (you name the address, slightly cheaper, correct only if the address is static).
# MASQUERADE: dynamic outbound IP, e.g. DHCP or PPPoE WAN
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
# SNAT: static outbound IP, name it explicitly
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 \
-j SNAT --to-source 203.0.113.10
8. Persist the ruleset. Rules live in kernel memory and vanish on reboot. Save them and install the package that restores them at boot.
sudo apt install iptables-persistent
sudo netfilter-persistent save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables save
run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables save
You can also dump and reload by hand for review or version control:
sudo iptables-save > /etc/iptables/rules.v4
sudo iptables-restore < /etc/iptables/rules.v4
Validate it works
Drive real traffic and watch conntrack record the translated flow. From an external client, hit the host’s port; on the gateway, inspect the tracking table.
sudo conntrack -L -p tcp --dport 80
tcp 6 431999 ESTABLISHED src=198.51.100.20 dst=203.0.113.10 sport=51544 dport=8080 \
src=192.168.10.5 dst=203.0.113.10 sport=80 dport=51544 [ASSURED] mark=0 use=1
The two src=/dst= tuples are the original and reversed directions — proof that DNAT rewrote the destination to 192.168.10.5:80 and conntrack is applying the reverse to replies. If you don’t have the conntrack tool, read the same data from the kernel directly:
sudo grep 192.168.10.5 /proc/net/nf_conntrack
ipv4 2 tcp 6 431995 ESTABLISHED src=198.51.100.20 dst=203.0.113.10 sport=51544 \
dport=8080 src=192.168.10.5 dst=203.0.113.10 sport=80 dport=51544 [ASSURED] use=1
Troubleshooting
Forwarding silently does nothing. The DNAT counter increments but the backend never sees traffic — almost always net.ipv4.ip_forward=0. Check sysctl net.ipv4.ip_forward; if it survived your session but not a reboot, your drop-in in /etc/sysctl.d/ is missing or misnamed.
DNAT fires but the reply never returns. The backend receives the connection but the client sees a timeout. Either MASQUERADE/SNAT is missing (the backend replies to the client’s real IP and the client discards an unexpected packet), or routing is asymmetric — the reply leaves through a different path that never passes back through this gateway, so conntrack can’t reverse the NAT. Confirm the POSTROUTING counter is incrementing alongside PREROUTING.
FORWARD default DROP blocks the flow. DNAT and MASQUERADE both show hits, but the FORWARD chain drops the packet in between. List the filter table with iptables -L FORWARD -n -v; if the policy is DROP and your two ACCEPT rules aren’t there (or don’t match the rewritten destination), add them as in step 4.
Rule order and append vs insert. -A appends to the end of a chain, after any earlier matching rule that already jumped to ACCEPT, DROP, or RETURN. If a broad rule above yours already decided the packet’s fate, yours never runs. Use -I <chain> <n> to insert at a position, and read --line-numbers output to see the real evaluation order.
Rules gone after reboot. Everything worked, then a reboot wiped it. iptables rules are not persistent by default — you must netfilter-persistent save (or iptables-save to a restored file). Re-run step 8.
Hairpin NAT doesn’t work for internal clients. A host inside the LAN reaching the public IP:8080 gets no response, while external clients work fine. This is the classic loopback-NAT gap: the reply from the backend goes straight to the internal client (same subnet) with a source of 192.168.10.5, not the public IP the client expected. Fix it by also masquerading the hairpinned flow so the backend replies via the gateway:
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -d 192.168.10.5 \
-p tcp --dport 80 -j MASQUERADE
When this is the wrong approach
If you’re already on a modern Debian or Ubuntu release, the native tool is nftables — iptables is often just a translation shim over it now, and writing rules directly in nft gives you atomic updates, sets, and maps that iptables can’t match. See migrating from iptables to nftables before building a large ruleset by hand. For a plain host firewall where you’re allowing and denying ports rather than routing between networks, UFW is far less error-prone, and for reaching internal services across the internet a WireGuard tunnel is usually safer than exposing a DNAT port to the world. The authoritative reference for every target and match used here is the iptables(8) manpage and the netfilter documentation.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.