Address translation
Port forward
One port on the router's public address passed to one device and port inside.
The router rewrites the destination on the way in (DNAT) and the source of the replies on the way out. It is separate from the firewall: a forward without a rule allowing it is dropped after the rewrite. The inside device still sees the real client's address.
How port forwarding works, step by step
A workshop's router has the public address 203.0.113.2 and the inside address 192.168.50.2. A web server
inside at 192.168.50.20 listens on port 80. The router forwards its own port 80 to it. Follow a customer at
198.51.100.50:
- The customer connects to the router. Its SYN is
198.51.100.50:57580 → 203.0.113.2:80. It never learns the server's address. - DNAT before routing. In the nat table's PREROUTING chain the first packet of the connection matches the
forward, and its destination is rewritten to
192.168.50.20:80. Conntrack records the translation, so the rest of the connection is rewritten without consulting the rules again. - Routing, then the firewall. With the new destination, the routing table sends the packet out of the inside card, and the FORWARD chain decides whether it may pass. A forward is only an address rewrite; the permission is a separate rule.
- The server answers the real client. The source was never changed, so the server sees
198.51.100.50and replies to it, through its default gateway, the router. - The reply is translated back. Conntrack matches it and rewrites the source from
192.168.50.20:80to203.0.113.2:80, so the customer gets an answer from the address it called.
A forward can keep the port (80 to 80) or change it (8080 to 80), and covers TCP or UDP. Routers that expose it as "virtual servers" or UPnP do the same rewrite. With carrier-grade NAT upstream, the router's outside address is itself private, and a forward on the home router cannot be reached from the internet at all.
What it looks like on a Linux machine
In the port forwarding lab the forward is one iptables rule, typed on the router.
-d limits it to packets addressed to the router's public address, so traffic passing through keeps its destination:
iptables -t nat -A PREROUTING -d 203.0.113.2 -p tcp --dport 80 -j DNAT --to-destination 192.168.50.20
The router lists it with its other forwards, and a capture on the server shows what the rule did:
$ iptables -t nat -S PREROUTING
-A PREROUTING -d 203.0.113.2/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.50.20
$ tcpdump -nn -i workshop -q 'tcp port 80'
IP 198.51.100.50.57580 > 192.168.50.20.80: tcp 0
IP 192.168.50.20.80 > 198.51.100.50.57580: tcp 0
The receiver is the server's private address, and the sender is the customer's real address. That is why a server
behind a forward logs its real visitors. In nftables the same forward is
ip daddr 203.0.113.2 tcp dport 80 dnat to 192.168.50.20 in a type nat hook prerouting chain.
What goes wrong, and how you see it
Forwarded but not allowed. The firewall runs after the rewrite, so it sees the inside port. A FORWARD rule that
allows port 8080 does nothing for a forward from 8080 to 80: the destination is rewritten, the packet is dropped, and
curl just times out. A ticket in the lab has exactly this fault. iptables -t nat -L -nv counts the DNAT; the FORWARD
chain's final DROP counts the loss.
Refused instead of a timeout. The DNAT rule is missing, so the connection is for the router itself, which has
nothing on port 80 and answers with a reset:
curl: (7) Failed to connect to 203.0.113.2 port 80 after 0 ms: Could not connect to server.
iptables -t nat -S shows which forwards exist.
A timeout after the server moved. The forward still points at the old address, and nothing answers there. Compare
iptables -t nat -S PREROUTING with ip -br addr on the server, and give the server a fixed address or a DHCP
reservation.
It works from outside but not from inside (hairpin NAT). A PC at 192.168.50.10 connects to
203.0.113.2:80. The router rewrites the destination to the server, but the server sees a source on its own network
and answers directly, bypassing the router. The PC gets a SYN-ACK from an address it never called and resets it:
192.168.50.10.56942 > 192.168.50.20.80: Flags [S], ...
192.168.50.20.80 > 192.168.50.10.56942: Flags [S.], ...
192.168.50.10.56942 > 192.168.50.20.80: Flags [R], ...
The fix is to also rewrite the source for inside-to-inside forwarded traffic, so replies return through the router:
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -d 192.168.50.0/24 -j MASQUERADE. Split DNS, giving
inside clients the inside address, avoids the detour.
The inside server uses another gateway. Its replies leave untranslated by another router and the client drops them. Its default route must point back through the forwarding router.
Questions people ask
Is port forwarding the same as DNAT? Yes: a port forward is destination NAT for one port. Source NAT for outgoing traffic is the other half of a home router.
Is port forwarding safe? It exposes one service to the whole internet, so that service must be patched and authenticated, and its logs watched. A DMZ network or an SSH bastion limits what an attacker reaches if it is broken.
Port forwarding vs SSH port forwarding? A router's forward rewrites packets in the kernel. ssh -L and ssh -R
carry a port through an encrypted SSH connection between two programs.
Read next
- NAT A router rewriting addresses as packets pass, usually to hide many private devices behind one address.
- Firewall Rules deciding, packet by packet, what may reach or cross a device.
- Bastion host One hardened machine that is the only way into a private network.
- DMZ A network apart for the servers the internet may reach, so breaking into one opens nothing behind it.
- Static NAT, dynamic NAT and PAT One address for one device (static), addresses from a pool as devices go out (dynamic), or one address shared by port (PAT).
- Published port A host port forwarded to a container's port with a DNAT rule, so others reach a container through its host.
- Pivoting Using a host you already control as a relay to reach a network that has no route from where you stand — the compromised box bridges the two segments.