Sub topic 10
Troubleshooting port forwarding
This chapter assumes you have read the earlier chapters on destination NAT, the translation table, and the Linux configuration. You should be comfortable with iptables or nft, ss, and reading a short tcpdump capture.
Port forwarding fails silently. A client sees a timeout or a refusal, and neither message says which of several components is at fault. The fix is to follow the packet through the gateway one stage at a time and stop at the first stage where it disappears.
The five places a forward can fail
A forwarded connection passes through five stages. Each stage maps to one family of causes.
- The packet reaches the gateway's public address. Failures here are ISP blocking, the wrong public address, or carrier-grade NAT.
- The
PREROUTINGDNAT rule matches and rewrites the destination. Failures are a wrong interface, port, or protocol, or rule order. - The kernel forwards the packet, and the
FORWARDchain accepts it. Failures are disabled IP forwarding or a default drop policy. - The internal server accepts the connection. Failures are no listener, a loopback-only listener, or a host firewall.
- The reply returns through the gateway so it can be un-translated. Failures are a wrong default gateway on the server, or hairpin traffic.
Step 1: Test from the right place
Run the final test from a host that is genuinely outside your network, such as a phone on mobile data or a cloud VM. A test from inside the LAN exercises hairpin NAT, which is a different mechanism and can fail even when the forward works for everyone else.
First, on the internal server, confirm something is listening:
# On the internal server: confirm the service is listening
sudo ss -ltn 'sport = :80'
# From a host outside the network: test the forwarded port
curl -v http://203.0.113.5:8080/Read the Local Address:Port column from ss. 0.0.0.0:80 or [::]:80 accepts connections on every interface. 127.0.0.1:80 accepts only local connections, so forwarded traffic is refused no matter how correct the NAT rules are.
The error message from curl is a first clue:
| What the client sees | What it usually means |
|---|---|
| Timeout | Packets are dropped somewhere: firewall, no matching DNAT rule, wrong route, or ISP filtering |
| Connection refused | A host answered with a TCP reset: no listener, a loopback-only bind, or a REJECT rule |
| Connects, then hangs or resets | The path works, but the application, a TLS setup, or the MTU is the problem |
| Works from outside, fails from the LAN | Hairpin NAT is missing |
Step 2: Check that the gateway forwards at all
A Linux gateway only routes between interfaces when forwarding is on:
# Print the current value: it must be 1
sysctl net.ipv4.ip_forward
# Enable it for the running system
sudo sysctl -w net.ipv4.ip_forward=1The -w setting is lost at reboot. To persist it, put net.ipv4.ip_forward=1 in a file under /etc/sysctl.d/ and run sudo sysctl --system. A forward that works until the next reboot is almost always this.
Step 3: Use rule counters to see where packets stop
Packet and byte counters are the quickest way to tell whether a rule matched. Send one test connection, then list the rules with counters:
# DNAT and MASQUERADE rules, with hit counters
sudo iptables -t nat -L -n -v --line-numbers
# Filter rules in the forwarding path
sudo iptables -L FORWARD -n -v --line-numbersInterpret the counters like this:
- PREROUTING counter stays at 0. The packet either never reached the gateway or did not match. Check
-i eth0, because the rule matches the interface the packet arrives on. Check the protocol, because-p tcpdoes not match UDP. Check--dport, which is the port the client connects to (8080), not the internal port (80). - PREROUTING counter increases, FORWARD counter stays at 0. The packet was rewritten but something dropped it before the filter rule, or the rule does not match. Remember that FORWARD sees the packet after DNAT, so it must match the internal address
192.168.1.10and port 80, not the public address and port 8080. - FORWARD counter increases, but the client still times out. The problem is on the server or on the reply path. Go to Step 5.
If you use nftables, add counter to the rules and list them with sudo nft list ruleset. Rule order matters in both tools. An earlier rule that already matched the traffic, such as a REDIRECT, a different DNAT, or an ACCEPT in the same chain, wins before your rule is reached.
Also look at the FORWARD policy. With -P FORWARD DROP, a DNAT rule alone is not enough. You must also accept the traffic, and you usually want a rule that accepts established return traffic:
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTStep 4: Inspect the translation table
The connection tracking table shows whether the kernel created the mapping and whether a reply was ever seen:
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpEach entry lists an original tuple and a reply tuple. The state field tells you how far the handshake got:
SYN_SENTwith[UNREPLIED]means the gateway forwarded the SYN and no SYN-ACK came back. Look at the server, its firewall, and the reply path.ESTABLISHEDmeans traffic flows in both directions. The NAT layer works, so look at the application.- No entry at all means the packet never matched the rules or never arrived.
The reply tuple should show the internal server as the source of replies. If the destination in the original tuple is still the public address, the DNAT did not apply.
Step 5: Capture on both sides of the gateway
When counters and conntrack disagree with the symptom, capture packets on the WAN and LAN interfaces at the same time, in two terminals. This example assumes eth0 is the WAN and eth1 is the LAN:
# Terminal 1: traffic arriving from outside
sudo tcpdump -ni eth0 'tcp port 8080'
# Terminal 2: traffic after translation, toward the server
sudo tcpdump -ni eth1 'tcp port 80 and host 192.168.1.10'Compare what each capture shows:
- The SYN appears on
eth0only. The DNAT rule or the FORWARD chain is blocking it. - The SYN appears on
eth1and no SYN-ACK comes back. The server's firewall is dropping it, or nothing is listening and the server's reset is being lost. - The SYN-ACK appears on
eth1, but nothing leaves oneth0. The gateway is not un-translating the reply. Check that the conntrack entry exists and that a stateful filter rule is not dropping the reply. - Nothing appears on
eth0at all. The traffic never reaches the gateway. Check the public address, the ISP, and any upstream router.
Step 6: Check the reply path
The server must send its replies back through the gateway. Its default gateway must be the NAT device, or a route toward the client must point there. If the server has another default route, such as a second router or a VPN, the reply leaves by that path with the server's private address as its source. The client discards it because it comes from an address the client never contacted. Check with ip route get <client-address> on the server, and confirm the next hop is the gateway.
The same problem is the reason hairpin NAT needs an extra source-NAT rule. When a LAN client uses the public address, the server sees a LAN source address and replies directly, bypassing the gateway.
Causes outside your gateway
If every stage above looks correct, the packet may be stopped before it reaches you.
- Wrong public address. Compare the address on the gateway's WAN interface with what an outside service reports. If they differ, you are behind another NAT device or carrier-grade NAT, and a forward on your gateway cannot work without a forward on the upstream device.
- Double NAT. A modem or ISP router in front of your gateway needs its own forward to your gateway's WAN address.
- ISP port blocking. Some providers block inbound ports such as 25 or 80. Test with a high port number like 8080 to find out.
- Other tools editing rules. Container runtimes, firewall managers, and cloud agents rewrite NAT and filter chains. A rule you added by hand can vanish on reload. Check which tool owns the ruleset.
- Mixing iptables and nftables. Rules in both systems apply, and a drop in either one blocks the packet. List both when behavior is hard to explain.
Quick reference
| Symptom | Check first | Likely fix |
|---|---|---|
| Refused immediately | `ss -ltn` on the server | Bind to `0.0.0.0`, or start the service |
| Timeout, PREROUTING counter 0 | Interface, protocol, and port in the rule | Correct the match, or find the upstream block |
| Timeout, FORWARD counter 0 | `iptables -P FORWARD`, `ip_forward` | Enable forwarding, add an ACCEPT rule |
| `SYN_SENT [UNREPLIED]` in conntrack | `tcpdump` on the LAN side | Server firewall or default gateway |
| Works outside, fails inside | Hairpin rules | Add the extra POSTROUTING masquerade |
| Works until reboot | `sysctl` and rule persistence | Persist the sysctl setting and the ruleset |
Exercise
You forwarded TCP 8080 on the gateway to 192.168.1.10:80. From a phone on mobile data, curl times out. On the gateway, the PREROUTING DNAT counter increases with each attempt, but the FORWARD ACCEPT rule counter stays at 0, and the FORWARD policy is DROP. What do you check first, and what is the likely cause?
Show answer
The packet is being rewritten but then dropped in the filter table. Check that the FORWARD ACCEPT rule matches the translated packet: destination 192.168.1.10, port 80, arriving on the WAN interface. A rule written with the public address or port 8080 never matches, because FORWARD sees the packet after DNAT. Fix the rule and add an ESTABLISHED,RELATED accept rule for the replies.
Exercise
A capture on the LAN interface shows the SYN reaching 192.168.1.10 and the server sending a SYN-ACK, but no SYN-ACK is seen leaving the WAN interface. The server's default gateway is a VPN router, not the NAT gateway. Why does the client never connect?
Show answer
The reply goes out through the VPN router instead of the gateway that holds the translation entry. It carries the private source address 192.168.1.10 and is never un-translated, so the client discards it or never receives it. Point the server's default route at the NAT gateway, or add a route for the client networks through it.