Private addresses (10/8, 172.16/12, 192.168/16) cannot travel on the internet. Network Address Translation rewrites addresses as packets cross a router, so a whole home or office can share one public address. NAT is the main reason IPv4 has lasted this long, and it is everywhere: your home router, every cloud “NAT gateway”, Docker on your laptop and Kubernetes Services.
The three flavors
| Type | Mapping | Typical use |
|---|---|---|
| Static NAT | One private ↔ one public, permanent | Publishing an internal server with its own public IP |
| Dynamic NAT | Private addresses borrow from a pool of public ones | Rare today |
| PAT (port address translation, NAT overload, masquerade) | Many private → one public, told apart by port | Every home router, cloud NAT gateways |
When people say “NAT” they almost always mean PAT.
How PAT works
A laptop at 192.168.1.42 opens a connection to a web server. The router’s public address is 203.0.113.7.
| Step | Source | Destination |
|---|---|---|
| Laptop sends | 192.168.1.42:51644 | 198.51.100.80:443 |
| Router rewrites source and sends out | 203.0.113.7:40001 | 198.51.100.80:443 |
| Server replies | 198.51.100.80:443 | 203.0.113.7:40001 |
| Router looks up 40001, rewrites destination | 198.51.100.80:443 | 192.168.1.42:51644 |
The router keeps a translation table (on Linux, the connection tracking table) linking each public port to a private address and port. With about 64,000 ports per public address per protocol and destination, one IP can serve thousands of devices.
$ sudo conntrack -L -p tcp --dport 443 | head -2
tcp 6 431996 ESTABLISHED src=192.168.1.42 dst=198.51.100.80 sport=51644 dport=443 src=198.51.100.80 dst=203.0.113.7 sport=443 dport=40001 [ASSURED] mark=0 use=1
Each entry shows the original direction first and the expected reply second: you can read the translation straight off it.
SNAT and DNAT
- SNAT (source NAT) changes the source of outgoing packets: that is PAT and masquerading.
- DNAT (destination NAT) changes the destination of incoming packets: that is port forwarding.
Port forwarding
Inbound connections have no table entry, so the router drops them. That is why you cannot reach a server at home from outside unless you forward a port:
“Anything arriving on 203.0.113.7:8443 → send to 192.168.1.50:443.”
On a Linux router with nftables:
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat;
iifname "wan0" tcp dport 8443 dnat to 192.168.1.50:443
}
chain postrouting {
type nat hook postrouting priority srcnat;
oifname "wan0" masquerade
}
}
$ sudo nft -f nat.nft
$ sudo nft list table ip nat
$ sudo sysctl -w net.ipv4.ip_forward=1
masquerade is SNAT to whatever address the outgoing interface has, which is ideal when the ISP changes it. With a fixed address, snat to 203.0.113.7 is slightly faster. Do not forget to also allow the forwarded traffic in the filter table.
NAT in the tools you already use
$ docker run -d -p 8080:80 nginx
$ sudo iptables -t nat -L DOCKER -n
Chain DOCKER (2 references)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:172.17.0.2:80
Docker’s -p 8080:80 is a DNAT rule, and containers reach the internet through a masquerade rule. Kubernetes Services, AWS and GCP NAT gateways and Azure’s outbound rules are all variations of the same idea.
Carrier-grade NAT (CGNAT)
Many ISPs, especially mobile ones, no longer give customers a public IPv4 at all. Your router gets an address from 100.64.0.0/10 and the ISP NATs again: double NAT. Side effects:
- You cannot port-forward from the internet; there is no public address that belongs to you.
- Thousands of customers share one public IP, so a block or rate limit on that IP hits all of them.
- Some games, VPNs and peer-to-peer apps struggle.
If your router’s WAN address is in 100.64.0.0/10, or differs from what curl -s https://ifconfig.me reports, you are behind CGNAT. IPv6 avoids the problem entirely.
Hairpin NAT
A device inside the LAN tries to reach the public address of a server that is also inside the LAN. Without hairpin (NAT loopback) support, the router does not translate the reply correctly and the connection fails. Fixes: enable hairpin NAT on the router, or use split DNS so internal clients resolve the name to the private address.
NAT traversal
Two devices both behind NAT cannot simply connect to each other. Video calls, games and mesh VPNs use these techniques:
- STUN: a public server tells each peer what public address and port it appears as.
- Hole punching: both peers send packets to each other’s public mapping at the same time, so each NAT creates an entry the other side can use. Works through most home NATs.
- TURN: when punching fails (often with symmetric NAT or CGNAT), traffic is relayed through a public server.
- ICE: the framework (used by WebRTC) that tries all of the above and picks the best working path.
- UPnP / NAT-PMP / PCP: an app asks the home router to open a port for it. Convenient, but often disabled for security.
The good and the bad
| Benefit | Cost |
|---|---|
| Saves public IPv4 addresses | Breaks end-to-end connectivity |
| Hides internal addressing | Complicates peer-to-peer, VoIP and some VPNs |
| Inbound connections blocked by default | Logging must map ports to users for abuse reports |
| Allows renumbering behind the router | Translation tables can fill up under load |
NAT is not a firewall. The blocking of unsolicited inbound traffic is a side effect. Real protection comes from stateful firewall rules, which work the same way with IPv6 and no NAT at all.
Exercise
In PAT, how does the router know which inside host a reply belongs to?
Show answer
Answer: D. The router chooses a unique public port for each outgoing connection and stores the mapping. The reply arrives on that port and is translated back.
Exercise
You want https://home.example.net:8443 to reach a NAS at 192.168.1.50:443 behind your router. What do you configure?
Show answer
Answer: B. Inbound connections need a destination NAT rule. Masquerade (source NAT) only handles traffic leaving the network.
Exercise
Your router’s WAN address is 100.72.18.9. What does that mean?
Show answer
Answer: C. 100.64.0.0/10 is reserved for carrier-grade NAT. You share a public address with other customers and cannot receive inbound port forwards.
Key takeaways
- PAT lets many private hosts share one public IP, telling flows apart by port.
- SNAT/masquerade rewrites sources going out; DNAT/port forwarding rewrites destinations coming in.
- Docker port publishing, cloud NAT gateways and Kubernetes Services all use NAT.
- CGNAT (
100.64.0.0/10) adds a second NAT and blocks inbound connections. - NAT is not a firewall; IPv6 removes the need for it.
FAQ
Why can’t I host a game server at home?
Either you have not forwarded the port on your router, or you are behind carrier-grade NAT and there is no public address to forward from. Check your WAN address, ask your ISP for a public IP, or use IPv6 or a tunnel service.
What is symmetric NAT?
A NAT that picks a different public port for every destination. Hole punching depends on a predictable mapping, so symmetric NAT often forces a TURN relay.
Does NAT slow down my connection?
Barely, on modern routers. The real limits are the size of the connection tracking table and the CPU of cheap routers when thousands of connections are open.
Where does a cloud NAT gateway fit in?
Servers in private subnets have no public IP. A NAT gateway in a public subnet masquerades their outgoing traffic so they can download updates, while nothing on the internet can connect to them directly.