DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · ADDRESSING · 30 OF 187

NAT and Private IP Ranges: How Home and Cloud Work

Why your laptop and most cloud servers have private addresses (10/8, 172.16/12, 192.168/16) and how they still reach the internet: source NAT rewriting addresses and ports, the connection table, port exhaustion (our kernel offers 28,232 local ports per destination), destination NAT and port forwarding, cloud NAT gateways versus internet gateways, and why NAT is not a firewall.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • 10/8
  • 172.16/12
  • 192.168/16
  • SNAT out
  • DNAT in
  • conntrack table
  • port exhaustion
  • not a firewall

Notes

Private ranges

Three ranges are reserved for private use: ten slash eight, one seven two dot sixteen slash twelve, and one nine two dot one six eight slash sixteen. Routers on the internet drop them, so every home, office and cloud can reuse the same ranges without conflict. Python's ipaddress module confirms ten dot zero dot one dot seventy seven is private. Your servers stay off the public internet unless you deliberately expose them.

RFC 1918 ranges
10.0.0.0/8       16.7 million
172.16.0.0/12    1 million
192.168.0.0/16   65,536

>>> ip_address("10.0.1.77")
...   .is_private
True
the same ranges are reused
  in every home and company

Source NAT

Source NAT works like this. Our private server sends a request from ten dot zero dot sixty four dot twelve, port forty thousand one hundred and twelve. The NAT gateway replaces the source address with its own public address and picks a new source port. The remote server only ever sees the public address. When the reply comes back to that public address and port, the gateway rewrites it back and forwards it to the private server.

Outbound request
  • leaves server: 10.0.64.12:40112 → api.example:443
  • after NAT: PUBLIC_IP:61001 → api.example:443
  • reply comes back to PUBLIC_IP:61001 and is rewritten back

The NAT table

To rewrite replies correctly, the gateway keeps a table of every connection: which private address and port maps to which public port, and to which remote server. Entries expire when the connection goes idle. On Linux this is connection tracking, listed with conntrack. A packet arriving from the internet with no matching entry is dropped, which is why outsiders cannot start connections to private machines.

Connection tracking
  • private ip:port ↔ public ip:port ↔ remote ip:port
  • entries expire after idle timeouts
  • Linux: conntrack -L
  • replies with no entry are dropped → outsiders cannot start a connection inward

Port exhaustion

Here is the classic failure. Ports are a sixteen bit number, and our Linux machine used a range of twenty eight thousand two hundred and thirty two ports for outgoing connections. A NAT gateway has a similar limit per destination. Hundreds of servers opening short connections to one external API can exhaust it, and new connections fail. The fixes: reuse connections, add public addresses, and keep connections open longer.

Our kernel
$ sysctl net.ipv4.
    ip_local_port_range
32768  60999
  = 28,232 ports
per (public IP, remote IP,
  remote port)
many connections to one API
  → "cannot assign address"
fix: reuse connections, more
  NAT IPs, fewer TIME-WAITs

Destination NAT

Destination NAT works the other way. Traffic to a public address and port is rewritten to a private machine and port. Your home router's port forwarding is destination NAT, and so is Docker's minus p flag, which writes iptables rules that forward a host port into a container. In the cloud you usually do not forward ports by hand; a load balancer with a public address receives traffic and sends it to private servers.

Inbound
  • public 203.0.113.5:443 → private 10.0.1.20:8443
  • home router "port forward"
  • Docker -p 8080:80 does this with iptables rules
  • cloud: a load balancer is the usual inbound path
  • documentation IPs: 203.0.113 and 192.0.2 are examples

Cloud gateways

In AWS, a public subnet has a route to an internet gateway, and its machines can have public addresses that work both ways. A private subnet routes to a NAT gateway that lives in a public subnet, so its machines can reach out, for updates or APIs, but nothing can reach in. NAT gateways are per availability zone and charge per gigabyte, so heavy traffic to storage should use VPC endpoints instead. Part eight builds this.

AWS terms
  • public subnet: route to an internet gateway, machines have public IPs
  • private subnet: route to a NAT gateway in a public subnet: out only NAT gateway is per AZ and billed per GB processed
  • S3/DynamoDB: VPC endpoints

NAT is not security

One warning: NAT is not a firewall. It blocks unsolicited inbound connections only as a side effect, and it lets every outbound connection through, so a compromised server can still call home. IP version six usually has no NAT at all, every machine has a global address. Real control comes from firewalls, security groups and egress rules, which episode fourteen covers.

Remember
  • NAT blocks unsolicited inbound only as a side effect
  • outbound is wide open: malware can call home
  • IPv6 often has no NAT at all real control: firewalls, security groups, egress rules (episode 14)

Common mistakes

Mistakes. Treating NAT as a firewall. One NAT gateway shared by all availability zones, which fails together with its zone. Opening a new connection for every request and exhausting ports. Sending large storage traffic through a NAT gateway and paying for every gigabyte. And giving a partner a NAT address to allowlist without making sure it is a fixed elastic address.

Avoid these
  • treating NAT as a firewall
  • one NAT gateway for all AZs (single point of failure)
  • new connection per request → port exhaustion
  • huge S3 traffic through NAT (cost)
  • allowlisting a partner by a NAT IP that can change

Say it like this

In the interview: the private subnet's route table sends internet traffic to a NAT gateway in a public subnet. The gateway rewrites the source to its public address and a port, tracks the connection, and maps replies back, so the server can reach out but nothing can start a connection in. I deploy one NAT per availability zone, use endpoints for heavy storage traffic, and watch for port exhaustion.

In the interview
  • "Its subnet routes 0.0.0.0/0 to a NAT gateway in a public
  • subnet. The gateway rewrites
  • the source to its public IP and port and tracks the connection
  • to send replies back. Nothing
  • can connect in. One NAT per AZ, and watch port exhaustion."

Interview questions

Q1What are the private IPv4 ranges?

10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 (RFC 1918). They are not routed on the public internet and can be reused anywhere.

Q2What is the difference between SNAT and DNAT?

Source NAT rewrites the source address of outgoing traffic (private to public); destination NAT rewrites the destination of incoming traffic, as in port forwarding.

Q3What is the difference between an internet gateway and a NAT gateway?

An internet gateway allows two-way traffic for instances with public IPs; a NAT gateway lets private instances start outbound connections only.

Q4What is SNAT port exhaustion?

Running out of source ports for connections from one public IP to the same destination, causing new connections to fail; fix with connection reuse or more IPs.

Q5Is NAT a security feature?

Not really. It blocks unsolicited inbound connections as a side effect but does not filter outbound traffic; use firewalls and security groups for control.

Q6How does Docker publish a port with -p 8080:80?

It adds iptables DNAT rules so traffic to host port 8080 is forwarded to port 80 inside the container network namespace.

Q7Why deploy one NAT gateway per availability zone?

A NAT gateway lives in one AZ; if that zone fails, private subnets in other zones would lose outbound access, and cross-AZ traffic costs more.