← All lessons
IP addressing and subnetting · Lesson 5 of 7

Private addresses and NAT

There aren't enough IPv4 addresses for every device, so three ranges are set aside as private: 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31) and 192.168.0.0/16. Anyone can use them inside their own network, and they're never routed on the public internet. That's why so many home networks are 192.168.1.something.

import ipaddress
for a in ["10.1.2.3", "172.20.0.5", "8.8.8.8", "192.168.0.10"]:
    print(a, ipaddress.ip_address(a).is_private)
Prints
10.1.2.3 True
172.20.0.5 True
8.8.8.8 False
192.168.0.10 True

Two other ranges are worth knowing. 127.0.0.0/8 is loopback: 127.0.0.1 always means "this computer", and packets to it never leave the machine. 169.254.0.0/16 is link-local: a device gives itself one of these when it asked for an address with DHCP and nobody answered, so seeing one usually means something is wrong.

Your home router has one public address from your internet provider. NAT (network address translation) lets every device behind it share that one address. When your laptop sends a packet out, the router swaps the private source address and port for its public address and a port of its own, and remembers the swap. When the reply comes back to that port, it swaps them back and passes it to your laptop.

A side effect: devices outside can't start a connection to a device behind NAT, because the router has no remembered swap for it. To run a server at home, you set up port forwarding, which tells the router to send everything that arrives on one port to one inside device.

Practice

Which of these is a private address?

More info

Quick reference

10.0.0.0/8Private.
172.16.0.0/12Private: 172.16 to 172.31.
192.168.0.0/16Private. Most home networks.
127.0.0.1Loopback: this computer.
169.254.x.xLink-local: DHCP didn't answer.
NATMany private addresses share one public one.
Port forwardingSend one outside port to one inside device.

The other networking courses