CodePark/ questions / Network layers & IP
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › Network layers & IP

What is NAT, and how do dozens of devices on an office network share one public IPv4 address?

NAT (Network Address Translation) means a router rewrites IP addresses, and usually ports, as packets pass through it. Devices inside the network use private addresses such as 192.168.1.20.

How one public address is shared:

  • A device opens a connection to the Internet.
  • The router replaces the source address with its own public IP.
  • The router picks a source port.
  • The router saves this mapping in a translation table.
  • Replies arrive at that public IP and port.
  • The router looks up the table and rewrites the reply back to the internal address and port.

This port-based form is often called PAT or NAT overload. It is what made the limited IPv4 address space last much longer.

Consequences:

  • Outside hosts can't start connections to inside devices unless you set up port forwarding.
  • Idle mappings expire. This can silently break long-lived TCP connections that stay idle.
  • In the cloud, servers in private subnets reach the Internet through a NAT gateway for the same reason.
  • The router rewrites the private source address and port to its own public IP.
  • A translation table maps replies back to the internal host.
  • NAT lets many hosts share one public IPv4 address.
  • Inbound connections need port forwarding.
  • Idle mappings expire and can break idle connections.