Sub topic 7
Port forwarding: reaching a private service from outside
This chapter assumes you know IPv4 addressing, what a default gateway does, how TCP ports work, and how source NAT and the translation table work from earlier chapters. You should also be comfortable in a Linux shell.
The problem
Source NAT lets private hosts start connections to the outside. It does not help in the other direction. An outside client has no way to name a private address such as 192.168.1.10, and the gateway has no table entry for a connection that began outside.
Port forwarding fixes this with a static destination NAT rule. The gateway listens on a port of its public address. When a packet arrives there, the gateway rewrites the destination to a chosen private host and port.
What changes in the packet
Suppose the gateway's public address is 203.0.113.5 and a web server runs at 192.168.1.10:80. You publish it on public port 8080.
| Stage | Source | Destination |
|---|---|---|
| Arrives at gateway | 198.51.100.7:50000 | 203.0.113.5:8080 |
| Sent to server (after DNAT) | 198.51.100.7:50000 | 192.168.1.10:80 |
| Reply from server | 192.168.1.10:80 | 198.51.100.7:50000 |
| Reply sent to client (reverse NAT) | 203.0.113.5:8080 | 198.51.100.7:50000 |
The rule matches only the first packet of a connection. The gateway records the mapping in the translation table, and the kernel applies the reverse rewrite to replies automatically.
Configuring it on Linux
The forward needs two rules. The DNAT rule rewrites the destination before the routing decision. The FORWARD rule permits the rewritten traffic if your filter policy would otherwise drop it. IPv4 forwarding must also be on, as in the previous chapter.
# 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 ACCEPTThe FORWARD rule matches the destination after DNAT, so it uses 192.168.1.10 and port 80. It does not use 8080.
Verifying the forward
Test in two steps. First confirm the service is listening on the server. Then test from a host outside your network.
# 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/Common errors
- Forwarding is disabled. If
net.ipv4.ip_forwardis 0, the kernel will not route the rewritten packet. - The FORWARD chain drops the traffic. The NAT rule alone does not open the firewall. Check the filter policy and rule counters.
- The server's default gateway is not the NAT device. The server then replies directly to the client. The client sees a reply from 192.168.1.10 instead of 203.0.113.5 and discards it.
- The service binds to 127.0.0.1. It accepts local connections only, so
ssshows the wrong listening address. - The public address is not reachable. Behind carrier-grade NAT, packets to your "public" address never reach your gateway.
Exercise
A client outside connects to 203.0.113.5:8080 and the connection hangs. ss on the server shows port 80 listening on all addresses, and the DNAT rule's packet counter increases. Which two causes fit these symptoms best?
Show answer
The DNAT rule is matching, so the packets reach the gateway. The likely causes are a FORWARD chain that drops the rewritten packets, or a server whose default gateway is not the NAT device, so replies bypass it. Check the FORWARD counters and the server's routing table.