Count the devices in your home that use the Internet: phones, laptops, a smart TV, maybe a printer. Your Internet Service Provider (ISP) has probably given your home only one public IPv4 address. So how do all those devices browse the web at the same time?
The answer is NAT (Network Address Translation).
NAT is a technique in which a router changes the IP address information in packets as they pass through it, usually translating between private addresses used inside a network and public addresses used on the Internet.
Laptop 192.168.1.10 ─┐
Phone 192.168.1.11 ─┼── Home Router (NAT) ── 203.0.113.5 ── Internet
TV 192.168.1.12 ─┘ private side public side
To the rest of the Internet, the whole home looks like a single device with the address 203.0.113.5.
(In this chapter we use 203.0.113.0/24, 198.51.100.0/24 and 192.0.2.0/24 as "public" addresses. These ranges are reserved for documentation, so they are safe to use in examples.)
Why Do We Need NAT?
IPv4 addresses are 32 bits long, so there are only about 4.3 billion of them, and many are reserved. There are far more Internet-connected devices than that. The regional registries that hand out IPv4 addresses have essentially run out of fresh blocks.
In the IP Addressing chapter you saw the private IPv4 ranges:
Private range | CIDR block |
|---|---|
10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 |
172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 |
192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 |
Any organisation can reuse these addresses inside its own network, but routers on the public Internet do not route them. NAT is the bridge: devices use private addresses inside, and the NAT router translates them to a public address on the way out.
NAT helps by:
Saving public IPv4 addresses: many devices share one public address
Letting networks keep their internal addressing even if they change ISP
Hiding internal addresses from the outside world
A Quick Word About Ports
To understand the most common type of NAT, you need one idea that is covered in detail later, in the Ports & Sockets chapter.
An IP address identifies a device. A port number (0 to 65535) identifies a specific conversation or application on that device. For example, a web server usually listens on port 443 for HTTPS, and your browser picks a temporary port such as 51000 for its side of the connection.
192.168.1.10 : 51000 → 198.51.100.20 : 443
(your laptop, browser) (web server, HTTPS)
The transport protocols TCP and UDP (also covered later) carry these port numbers.
Types of NAT
1. Static NAT
Static NAT creates a permanent one-to-one mapping between one private address and one public address.
192.168.1.50 ←→ 203.0.113.50 (always)
It is used when an internal server must always be reachable at the same public address. It does not save addresses, because every inside host still needs its own public IP.
2. Dynamic NAT
Dynamic NAT uses a pool of public addresses. When an inside host starts talking to the Internet, the router gives it a free address from the pool for a while.
Pool: 203.0.113.10 – 203.0.113.12 (3 public addresses)
192.168.1.10 → 203.0.113.10
192.168.1.11 → 203.0.113.11
192.168.1.12 → 203.0.113.12
192.168.1.13 → no free address, must wait
It is still one-to-one at any moment, so only as many hosts as there are pool addresses can be online together.
3. PAT / NAPT (NAT Overload)
PAT (Port Address Translation), formally called NAPT (Network Address and Port Translation), translates both the IP address and the port number. This lets many inside hosts share one public IP address at the same time. Cisco calls it NAT overload, and Linux calls it IP masquerading.
This is what your home router does. When people say "NAT" in everyday conversation, they almost always mean PAT.
Type | Mapping | Translates | Saves addresses? | Typical use |
|---|---|---|---|---|
Static NAT | One-to-one, permanent | IP address | No | Publishing an internal server |
Dynamic NAT | One-to-one, from a pool | IP address | A little | Organisations with a small public pool |
PAT / NAPT | Many-to-one | IP address and port | Yes, greatly | Home routers, offices, mobile networks |
How PAT Works, Step by Step
Your laptop (192.168.1.10) and your phone (192.168.1.11) both open the same website at 198.51.100.20 on port 443. By coincidence, both pick source port 51000. The router's public address is 203.0.113.5.
Step 1: Laptop sends a packet
Inside: 192.168.1.10:51000 → 198.51.100.20:443
Step 2: Router rewrites the source and records it
The router replaces the private source address and port with its public address and a port it chooses (here 40000), and stores the mapping in its NAT translation table.
Outside: 203.0.113.5:40000 → 198.51.100.20:443
Step 3: Phone sends a packet with the same source port
The router cannot use port 40000 again for a different inside host, so it picks 40001.
Inside: 192.168.1.11:51000 → 198.51.100.20:443
Outside: 203.0.113.5:40001 → 198.51.100.20:443
Step 4: Translation table
Inside (private) address:port | Outside (public) address:port | Remote server |
|---|---|---|
192.168.1.10:51000 | 203.0.113.5:40000 | 198.51.100.20:443 |
192.168.1.11:51000 | 203.0.113.5:40001 | 198.51.100.20:443 |
Step 5: Replies come back
The web server only ever sees 203.0.113.5. It replies to port 40000 or 40001. The router looks up the destination port in its table and rewrites the destination back to the correct private address and port.
Reply arrives: 198.51.100.20:443 → 203.0.113.5:40001
Router lookup: 40001 → 192.168.1.11:51000
Delivered as: 198.51.100.20:443 → 192.168.1.11:51000 (the phone)
When translating, the router must also recompute the IP header checksum and the TCP/UDP checksum, because the TCP and UDP checksums also cover the IP addresses.
A Small PAT Simulator
The following Python program models the translation table of a PAT router. Real NAT devices also track the protocol, the remote address and timers, but the core idea is the same.
class PatRouter:
def __init__(self, public_ip, first_port=40000):
self.public_ip = public_ip
self.next_port = first_port
self.out_map = {} # (private ip, private port) -> public port
self.in_map = {} # public port -> (private ip, private port)
def outbound(self, src_ip, src_port, dst):
key = (src_ip, src_port)
if key not in self.out_map: # new conversation
self.out_map[key] = self.next_port
self.in_map[self.next_port] = key
self.next_port += 1
port = self.out_map[key]
print(f"OUT {src_ip}:{src_port} -> {dst} becomes "
f"{self.public_ip}:{port} -> {dst}")
def inbound(self, src, dst_port):
if dst_port not in self.in_map:
print(f"IN {src} -> {self.public_ip}:{dst_port} DROPPED (no mapping)")
return
ip, port = self.in_map[dst_port]
print(f"IN {src} -> {self.public_ip}:{dst_port} becomes "
f"{src} -> {ip}:{port}")
def show_table(self):
print("\nInside (private) Outside (public)")
for (ip, port), pub in self.out_map.items():
print(f"{ip + ':' + str(port):22} {self.public_ip}:{pub}")
nat = PatRouter("203.0.113.5")
web = "198.51.100.20:443"
nat.outbound("192.168.1.10", 51000, web) # laptop
nat.outbound("192.168.1.11", 51000, web) # phone, same source port!
nat.outbound("192.168.1.10", 51000, web) # laptop again: reuse mapping
nat.inbound(web, 40001) # reply to the phone
nat.inbound(web, 40000) # reply to the laptop
nat.inbound("192.0.2.66:5555", 40050) # unsolicited packet
nat.show_table()
Output:
OUT 192.168.1.10:51000 -> 198.51.100.20:443 becomes 203.0.113.5:40000 -> 198.51.100.20:443
OUT 192.168.1.11:51000 -> 198.51.100.20:443 becomes 203.0.113.5:40001 -> 198.51.100.20:443
OUT 192.168.1.10:51000 -> 198.51.100.20:443 becomes 203.0.113.5:40000 -> 198.51.100.20:443
IN 198.51.100.20:443 -> 203.0.113.5:40001 becomes 198.51.100.20:443 -> 192.168.1.11:51000
IN 198.51.100.20:443 -> 203.0.113.5:40000 becomes 198.51.100.20:443 -> 192.168.1.10:51000
IN 192.0.2.66:5555 -> 203.0.113.5:40050 DROPPED (no mapping)
Inside (private) Outside (public)
192.168.1.10:51000 203.0.113.5:40000
192.168.1.11:51000 203.0.113.5:40001
Notice three things:
Two devices with the same private source port get different public ports, so replies are never mixed up.
A repeated packet from the laptop reuses its existing mapping.
A packet from the outside with no matching entry is dropped. The router has no idea which inside device it is for.
The Inbound Connection Problem
That last point is very important. With PAT, mappings are normally created only when an inside device starts a conversation. So an outside device cannot just start a connection to a laptop behind NAT.
This causes trouble when you want to host something at home, such as a game server or a CCTV recorder. Common solutions:
Port forwarding (a static NAPT entry): you tell the router "send anything that arrives on public port 8080 to 192.168.1.20 port 80". Only one inside device can own a given public port.
Automatic port mapping protocols such as UPnP IGD or PCP, which let an application ask the router to create a mapping.
NAT traversal techniques used by video calls and peer-to-peer apps: STUN helps a device discover its public address and port, TURN relays traffic through a server when direct connection fails, and ICE tries the options and picks one that works. Using these, two devices behind different NATs can often connect directly by "hole punching".
Mappings Expire
A NAT router cannot keep every mapping forever, so idle mappings time out. The IETF guidelines say a UDP mapping should not expire in less than 2 minutes (5 minutes or more is recommended), and an established TCP mapping should not be removed until it has been idle for at least 2 hours and 4 minutes. In practice, many home routers use shorter timeouts. This is why chat and messaging apps send small keepalive packets on idle connections.
Carrier-Grade NAT (CGNAT)
Even one public IPv4 address per home is too many for some ISPs, especially mobile operators with millions of customers. They add a second layer of NAT inside their own network, called Carrier-Grade NAT (CGNAT) or Large-Scale NAT.
Phone 192.168.1.11 → Home router → 100.64.12.7 → ISP CGNAT → 203.0.113.80 → Internet
(NAT #1) (NAT #2)
The range 100.64.0.0/10 is reserved as shared address space for the link between customers and the CGNAT. If your router's "WAN IP" starts with 100.64 to 100.127, or simply does not match the address websites see, you are probably behind CGNAT. Many mobile data connections work this way, which is why port forwarding on a mobile hotspot usually does not work.
Advantages and Disadvantages of NAT
Advantages | Disadvantages |
|---|---|
Saves public IPv4 addresses | Breaks the end-to-end principle: outside hosts cannot directly reach inside hosts |
Internal addressing can stay the same when changing ISP | Hosting servers and peer-to-peer apps need port forwarding or traversal tricks |
Hides the internal network layout | Protocols that carry IP addresses inside their data (such as active-mode FTP or SIP) need special helpers called ALGs |
Unsolicited inbound packets are dropped by default | Extra processing and state in the router; the table can fill up |
Many devices share one connection easily | Many users behind one public IP (CGNAT) makes logging, rate limiting and IP bans harder |
NAT Is Not a Firewall
Because PAT drops unsolicited inbound packets, people often say "NAT keeps me secure". That is only partly true.
NAT was designed to save addresses, not to enforce security rules.
It does nothing against malware you download yourself, phishing, or attacks over connections that you started.
Port forwarding and UPnP can expose inside devices without you noticing.
Real protection comes from a firewall with deliberate rules, which you will study in the Firewalls, Proxy & VPN chapter. Most home routers include both features in one box, which is why they are easy to confuse.
NAT and IPv6
IPv6 has so many addresses that every device can have its own globally unique address, so NAT is generally not needed for address saving. IPv6 networks normally rely on a firewall instead of NAT to block unwanted inbound traffic.
NAT still appears in the IPv6 world as a transition tool. For example, NAT64 lets IPv6-only devices reach IPv4-only servers, and many mobile networks use it.
Common Mistakes
Thinking NAT and PAT are completely different things. PAT/NAPT is one type of NAT; it is the most common one.
Saying static NAT saves addresses. It is one-to-one, so it does not.
Assuming the router only changes the IP address. With PAT it usually changes the port too, and it must update the checksums.
Believing your public IP is unique to you. Behind CGNAT, hundreds of customers may share it.
Treating NAT as a security feature. Use a proper firewall.
Interview Tips
"How does a reply find the right device behind NAT?" Explain the translation table and that the public port identifies the inside host and port.
"Two devices behind the same NAT use the same source port. What happens?" The router assigns them different public ports.
"Why can't my friend connect to the game server on my laptop?" No mapping exists for inbound traffic; use port forwarding, and check for CGNAT.
"Does IPv6 need NAT?" Not for saving addresses; a firewall gives the protection.
Know the three types (static, dynamic, PAT) and the private ranges by heart.
Conclusion
NAT translates addresses as packets pass through a router, letting devices with private IP addresses communicate on the public Internet.
Static NAT maps one private address to one public address, dynamic NAT uses a pool, and PAT/NAPT lets many devices share a single public address by also translating port numbers. The router keeps a translation table so replies reach the correct device, and drops inbound packets that have no matching entry.
NAT slowed down IPv4 address exhaustion, but it complicates inbound connections and is not a replacement for a firewall. IPv6 removes the need for NAT for address saving.
The easiest way to remember it is:
NAT → one public face for many private devices. The translation table remembers who asked for what.