Concepts / Address translation

Address translation

Port forward

Also called DNAT, destination NAT, port mapping

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.

The forward changes one address
The forward changes one address

The customer's request arrives for the router's public address. The rule puts the server's address in the receiver's place and leaves the sender as it was. From the Port forwarding: let the internet reach one machine behind NAT lab.

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:

  1. 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.
  2. 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.
  3. 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.
  4. The server answers the real client. The source was never changed, so the server sees 198.51.100.50 and replies to it, through its default gateway, the router.
  5. The reply is translated back. Conntrack matches it and rewrites the source from 192.168.50.20:80 to 203.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.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab