Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    NAT: Many Private Devices, One Public Address

    NAT tables have timeouts. Idle entries are removed, and the connection silently dies — the

    Chapter 10 of 125 min read

    Now that ports are explained, we can finish the story from Networking 1: Addresses and Routing Ch 6.

    The Plain Picture#

    An office has one public phone number. Inside, every employee has an extension. When an employee calls outside, the receptionist places the call from the main number and writes down "the outside call on line 7 belongs to extension 214". When the other person replies on line 7, the receptionist knows to put it through to 214.

    But if an outsider calls the main number out of the blue and asks for "the person on line 7" — and there's no note — the receptionist has no idea who they mean, and hangs up.

    The Real Name#

    NAT (Network Address Translation) lets many devices with private addresses share one public address. Your home router does it. It rewrites the source IP and port of outgoing packets, and keeps a NAT table so it can rewrite replies back. (Strictly, sharing one address using ports is called PAT or NAPT, but everyone just says NAT.)

    Under the Hood#

    Your laptop (192.168.1.37) and phone (192.168.1.52) both open HTTPS connections. Your home's public address is 49.207.1.10.

       inside (private)                       ROUTER                      outside (public)
       ────────────────                    ───────────                    ────────────────
       192.168.1.37:52341  ──▶  rewrite source ──▶  49.207.1.10:40001  ──▶  13.235.10.20:443
       192.168.1.52:61022  ──▶  rewrite source ──▶  49.207.1.10:40002  ──▶  13.235.10.20:443
    
       NAT table
       ┌──────────────────────────┬───────────────────────┐
       │ public side              │ private side          │
       ├──────────────────────────┼───────────────────────┤
       │ 49.207.1.10:40001        │ 192.168.1.37:52341    │
       │ 49.207.1.10:40002        │ 192.168.1.52:61022    │
       └──────────────────────────┴───────────────────────┘
    
       Reply to 49.207.1.10:40002  →  table says 192.168.1.52:61022  →  deliver to the phone

    The server only ever sees 49.207.1.10. It has no idea there's a laptop and a phone behind it.

    What NAT means for you#

    1. Devices behind NAT can't easily accept incoming connections. An unsolicited packet arriving at the router has no table entry — just like the receptionist with no note. That's why you can't simply run a server on your laptop at home and have friends connect. The workarounds are port forwarding (a fixed rule: "anything to public port 8080 goes to 192.168.1.37:8000") or tunnelling tools like ngrok.

    2. NAT is not a firewall, though it blocks unsolicited incoming connections as a side effect.

    3. Carrier-grade NAT (CGNAT): many ISPs (especially mobile networks) put your router behind their NAT too, using the 100.64.0.0/10 range (Networking 1: Addresses and Routing Ch 6). Thousands of customers share one public IP.

    4. In the cloud: servers in a private subnet (Networking 1: Addresses and Routing Ch 11) often still need to reach the internet outwards — to download packages or call an AI API. A NAT gateway in a public subnet does this: outgoing connections work, incoming ones from the internet can't start. You'll set one up in VPC and Networking (coming soon).

    Gotchas

    • NAT tables have timeouts. Idle entries are removed, and the connection silently dies — the keep-alive problem from Ch 8.

    • Rate limits by IP address (System design (coming soon)) can punish whole groups of users who share one CGNAT address.

    Check yourself

    4 questions
    1

    What does NAT rewrite on outgoing packets?

    Show answer

    The source IP address and source port.

    2

    How does the router know which device a reply belongs to?

    Show answer

    It looks up the reply's destination port in its NAT table.

    3

    Why can't a friend connect to a server running on your home laptop, without extra setup?

    Show answer

    Incoming connections have no NAT table entry, so the router doesn't know which device to send them to (you'd need port forwarding or a tunnel).

    4

    What does a cloud NAT gateway allow, and what does it prevent?

    Show answer

    It lets private-subnet servers make outgoing connections to the internet, while nothing on the internet can open connections to them.