Sub topic 7
Port forwarding: reaching a private service from outside
Prerequisites and scope
This chapter assumes you know IPv4 addressing and subnet masks, what a default gateway does, how TCP and UDP use port numbers, and how to run commands in a Linux shell. It also assumes you have read the earlier chapters on the translation table and source NAT. Port forwarding is the inbound counterpart of source NAT. Here we cover the working model and the common errors. The full Linux configuration walkthrough, hairpin NAT, and the troubleshooting workflow each have their own chapters, so this one only previews them.
The problem port forwarding solves
Source NAT lets a private host start a connection to the outside. The gateway creates a translation entry when the first outbound packet passes through. An unsolicited packet arriving from the internet matches no entry, so the gateway has nothing to map it to. It drops the packet or treats it as traffic for itself.
Suppose a web server at 192.168.1.10 listens on port 80. Its address is private, so no one on the internet can route to it. The only public address in play is the gateway's, 203.0.113.5. Port forwarding fixes this with a static rule: traffic that arrives at the gateway's public address on a chosen port is rewritten and sent to the private host.
Port forwarding is also called destination NAT (DNAT). Source NAT rewrites the source of a packet. DNAT rewrites the destination.
What the gateway rewrites
Take a forward of public port 8080 to 192.168.1.10:80. An outside client at 198.51.100.7 connects to 203.0.113.5:8080.
| Step | Packet | Source | Destination |
|---|---|---|---|
| 1. Client sends | Arrives at the gateway | 198.51.100.7:51000 | 203.0.113.5:8080 |
| 2. Gateway applies DNAT | Leaves toward the LAN | 198.51.100.7:51000 | 192.168.1.10:80 |
| 3. Server replies | Arrives at the gateway | 192.168.1.10:80 | 198.51.100.7:51000 |
| 4. Gateway reverses DNAT | Leaves toward the internet | 203.0.113.5:8080 | 198.51.100.7:51000 |
Three points follow from this table:
- The client never learns the private address. It sees only 203.0.113.5:8080.
- The server sees the real client address as the source. This differs from source NAT, where the source is replaced. Logs on the server keep the true client IP.
- The gateway creates the translation entry when the first packet of the connection arrives. Later packets, and the replies, use that entry. The rule only decides how new connections are rewritten.
Where the rewrite happens
The destination must be rewritten before the routing decision. The gateway has to know the packet's final destination is 192.168.1.10 so it can route the packet out the LAN interface instead of delivering it locally. For this reason Linux does DNAT in the PREROUTING hook. Source NAT happens in POSTROUTING, after routing.
The rewrite does not exempt the packet from the firewall. After DNAT, the packet is a forwarded packet, so it also passes through the FORWARD chain of the filter table. If your policy there is DROP, you need an explicit accept rule. A forward that works in the NAT table but fails to deliver is often blocked at this step.
A minimal Linux example
The rules below forward TCP port 8080 on the gateway's WAN interface eth0 to the internal web server. The first command enables IPv4 forwarding. Without it, the kernel will not route packets between interfaces at all.
# Allow the Linux host to forward packets between interfaces
sudo sysctl -w net.ipv4.ip_forward=1
# Forward TCP port 8080 on the gateway's WAN interface to 192.168.1.10:80
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80
# Allow the forwarded traffic through the filter table
sudo iptables -A FORWARD -i eth0 -p tcp -d 192.168.1.10 --dport 80 -j ACCEPTNotice that the FORWARD rule matches the translated destination, 192.168.1.10 and port 80. By the time the packet reaches the filter table, DNAT has already changed it. A rule that matches port 8080 there would never fire.
The next chapter walks through the same setup in nftables and adds masquerading, so we do not repeat it here.
The reply path must return through the gateway
DNAT works only if the reply goes back through the same gateway. The server's reply is addressed to 198.51.100.7. The server routes it using its default gateway. If that gateway is the NAT device, connection tracking reverses the rewrite and the client sees a reply from 203.0.113.5:8080.
If the server has a different default route, for example a second router, the reply leaves by another path. The client sent its packet to 203.0.113.5:8080 and receives a reply from 192.168.1.10:80. That is a different 5-tuple, so the client's TCP stack discards it and the connection hangs.
When the server cannot use the gateway as its default route, you can force the reply through the gateway by also applying source NAT for forwarded traffic. This hides the real client address from the server, so use it only if you cannot fix the routing.
Port forwarding versus static NAT
Both let outside hosts reach a private host. They differ in scope:
| Feature | Static (one-to-one) NAT | Port forwarding |
|---|---|---|
| Public addresses used | One public address per private host | One public address shared by many services |
| Mapping | All ports of an address | Selected protocol and port pairs |
| Exposure | Whole host reachable unless filtered | Only forwarded ports reachable |
| Typical use | Large address pools, servers with many services | Home and small office networks with one public IP |
Port forwarding also lets you remap ports. Public port 8080 can point to private port 80. Several private hosts can then offer the same service under different public ports, for example 8081 to one server's port 80 and 8082 to another's.
Common errors
- IP forwarding is off. The rules look right but nothing is delivered. Check
sysctl net.ipv4.ip_forward. A value set withsysctl -wis lost on reboot unless you also put it in a file under/etc/sysctl.d/. - No
FORWARDaccept rule. The packet is rewritten, then dropped. Counters on theFORWARDchain (iptables -L FORWARD -v -n) will show drops growing. - The service is not listening, or listens only on localhost. The client receives a connection refused error, or a timeout if a firewall on the server drops it. A server bound to 127.0.0.1 never accepts traffic from the LAN.
- A host firewall on the server. The gateway may be configured correctly while the server's own firewall blocks the gateway's LAN address or the client.
- Asymmetric return path. Covered above. The symptom is a connection that hangs after the SYN.
- Matching the wrong interface or address. A rule with
-i eth0does not match traffic that arrives on another interface. If the public address changes, as with DHCP, a rule matching a fixed destination address breaks. Matching on the interface avoids that. - Testing from inside the LAN. A client on the same network using the public address may fail even when the forward is correct. That is the hairpin case, and it has its own chapter.
- The upstream does not give you a public address. Behind carrier-grade NAT or an ISP-provided router in front of yours, your gateway never receives the inbound packets. Check that the WAN address of your gateway is actually public.
Security considerations
Each forward opens a path from the whole internet to an internal host. Forward only the ports you need. Keep the service patched. Where possible, restrict the source with -s to known client ranges. Prefer a non-default external port only as a way to reduce log noise, because it does not make an exposed service secure.
Verifying a forward
Test in two steps. First confirm the service listens on the internal server. Then test from a host outside the network, since a test from inside may hit the hairpin problem and give a misleading result. A phone on mobile data works well.
# 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/On the gateway, sudo conntrack -L -p tcp shows the entry created for the connection. The original tuple shows the destination 203.0.113.5:8080, and the reply tuple shows the source 192.168.1.10:80. That pairing confirms DNAT is in effect.
Exercise
A gateway at 203.0.113.5 has the rule iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80. The FORWARD chain policy is DROP. Outside clients time out. You add iptables -A FORWARD -i eth0 -p tcp --dport 8080 -j ACCEPT, and it still times out. Why does the new rule not help, and what rule should you write?
Show answer
The FORWARD chain sees the packet after DNAT has rewritten it, so the destination port is already 80 and the destination address is 192.168.1.10. A rule matching port 8080 never matches. Use iptables -A FORWARD -i eth0 -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT.
Exercise
The forward is configured correctly and the firewall allows it, yet an outside client's connection hangs after the first packet. The server at 192.168.1.10 has a default route through a different router, 192.168.1.2, instead of the gateway. Explain what the client sees and give one way to fix it.
Show answer
The server's reply leaves through 192.168.1.2 and reaches the client with source 192.168.1.10:80, not 203.0.113.5:8080. The client's TCP stack does not recognize it as part of its connection and discards it. The best fix is to make the gateway the server's default route, or add a route on the server so replies to outside clients go through the gateway. As a fallback, apply source NAT to forwarded traffic so the server's replies return to the gateway.