Sub topic 10
Troubleshooting port forwarding
This chapter assumes you know IPv4 addressing, what a default gateway does, TCP and UDP ports, and basic Linux shell use. It also assumes you have read the earlier chapters on the translation table, DNAT, and hairpin NAT. A port forward fails in one of a few predictable places. The fastest fix is to test each hop in order instead of guessing.
The path a forwarded connection takes
A connection from the internet crosses several checkpoints. If any one fails, the client sees a timeout or a refusal.
Step 1: Is the service listening?
Start at the end of the path. On the internal server, check that something listens on the port.
# 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/If ss prints nothing, the service is down or bound to the wrong address. Fix that before touching NAT. Run the curl test from a host beyond the NAT device, such as a phone on mobile data. A test from inside the LAN may take a different path (see the hairpin section below).
Step 2: Does the gateway forward packets?
Check that IPv4 forwarding is on. Without it, the gateway drops packets that are not addressed to itself.
sysctl net.ipv4.ip_forwardThe value must be 1. A sysctl -w change does not survive a reboot, so also set it in /etc/sysctl.conf or a file under /etc/sysctl.d/.
Step 3: Do the rules match and count packets?
List the NAT rules with packet counters.
sudo iptables -t nat -L PREROUTING -n -v
sudo iptables -L FORWARD -n -vSend a test connection and run the commands again. If the DNAT counter does not rise, the packet never matched. Common causes are a wrong interface (-i eth0), a wrong port, or an upstream device that blocks the traffic first. If the DNAT counter rises but the FORWARD counter does not, a different rule or the default policy is dropping the packet. A DNAT rule alone does not permit the traffic when the FORWARD policy is DROP, so you also need an ACCEPT rule.
Step 4: Does the translation exist?
The kernel records each translated connection. Look for yours.
sudo conntrack -L -p tcpAn entry with the original destination 203.0.113.5:8080 and a reply source of 192.168.1.10:80 shows DNAT works. An entry with no reply packets means the server received the request but its answer is not returning.
Common causes of failure
| Symptom | Likely cause | Fix |
|---|---|---|
| Timeout from outside, DNAT counter at zero | Wrong interface or port, or the ISP blocks the port | Correct the rule, or try another port |
| Timeout, DNAT counter rising | FORWARD policy drops it | Add an ACCEPT rule for the destination |
| Connection refused | Nothing listening on the server | Start the service or fix its bind address |
| Conntrack entry with no replies | Server's default gateway is not the NAT device | Set the server's gateway to the NAT device |
| Works from outside, fails from the LAN | Missing hairpin NAT | Add the hairpin MASQUERADE rule |
| Never works from outside | Carrier-grade NAT, no public address | Use a relay or ask the ISP for a public address |
The server's default gateway deserves attention. If the server replies through a different router, the reply carries the wrong source address and is never reverse-translated, so the client discards it.
Hairpin failures
If the forward works from the internet but not from a LAN client using the public address, the reply bypasses the gateway. Add the rule from the hairpin chapter.
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.1.10 -p tcp --dport 80 -j MASQUERADEExercise
A forward for port 8080 to 192.168.1.10:80 times out from outside. The DNAT counter increases, but the FORWARD counter stays at zero and the FORWARD policy is DROP. What is wrong, and what rule fixes it?
Show answer
DNAT rewrites the destination, but the filter table then drops the forwarded packet. Add an ACCEPT rule: sudo iptables -A FORWARD -i eth0 -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT
Exercise
conntrack shows an entry for your forward, but it has no reply packets. Name one likely cause.
Show answer
The internal server sends replies through a gateway other than the NAT device, so the reply is never reverse-translated. Set the server's default gateway to the NAT device.