Pointer Walk

IP Addresses, Subnets and NAT

Every device needs an address, and there are not enough to go round. How a home router shares one public address between everything behind it.

NetworkingBeginner10 min read

An address for every device

To send a letter you need an address. To send a packet across the internet you need an IP address: a number that identifies where it should go.

The original format, IPv4, writes an address as four numbers from 0 to 255, separated by dots:

203.0.113.10

Underneath, that is just 32 bits: four groups of eight. Thirty-two bits give about 4.3 billion possible addresses. That seemed endless when it was designed. There are now far more than 4.3 billion phones, laptops, TVs and servers.

The internet has kept working anyway, thanks to two ideas: dividing addresses into networks, and letting many devices hide behind one address.

Network part and device part

An address is read in two parts. The first part says which network. The rest says which device on that network. It is like a street name and a house number.

Where the split falls is written after a slash: 192.168.1.37/24 means the first 24 bits are the network.

192.168.1.37 / 24Interactive
Prefix

192

11000000

168

10101000

1

00000001

37

00100101

Violet: the network part. Mint: the device part.

The first 24 bits name the network. The last 8 name a device on it. Two addresses are on the same network when their first 24 bits match, which means they can reach each other directly without going through a router.

Network
192.168.1.0/24
Addresses in it
256
Usable for devices
254

A network defined this way is a subnet. The slash notation is called CIDR.

Example

Reading a /24

In 192.168.1.37/24 the first three numbers, 192.168.1, are the network. The last number is the device.

That leaves 8 bits for devices: 256 values, from 192.168.1.0 to 192.168.1.255. Two are reserved, the first for the network itself and the last for "everyone on this network", so 254 devices fit.

Each bit removed from the network part doubles the room: a /23 holds 512 addresses, a /16 holds 65,536.

Why does the split matter? Because it decides where a packet goes first. If the destination is on my network, send it there directly. If not, hand it to the router, whose job is to pass it towards the right network.

Private addresses

Some ranges are set aside as private. They can be used freely inside any home or office, and are never routed on the public internet.

RangeTypical use
10.0.0.0/8Large companies and cloud networks
172.16.0.0/12Medium networks, containers
192.168.0.0/16Home routers

Your laptop's address is very likely 192.168.something. So is your neighbour's, and millions of others. That is fine, because those addresses only mean something inside each home.

But a private address cannot be the "from" address on a packet going out to the internet. No reply could ever find its way back.

NAT: many devices, one address

Your home has one public address, given to your router by your internet provider. Everything inside shares it. The trick that makes this work is network address translation, NAT.

One public address, shared by a whole homeInteractive
Laptop 192.168.1.5
Home router
Website 203.0.113.10

The router has one public address: 198.51.100.7. Everything behind it uses private addresses.

  1. A packet leaves your laptop with a private sender address

    It goes to the router.

  2. The router swaps in its own public address and a port number

    It keeps a table: this port belongs to that device.

  3. The website replies to the router's address and port

    It has no idea there is a laptop behind it.

  4. The router looks up the port and forwards the reply inside

    Rewriting the destination back to the laptop's private address.

A port is a number from 0 to 65,535 attached to each connection. It lets one address carry thousands of separate conversations at once, and it is what the router uses to tell your laptop's traffic from your phone's.

What NAT costs

NAT stretched IPv4 decades past its limit. It also changed how the internet works.

  • Connections can only start from inside. The router's table gets an entry when something inside sends a packet out. A packet arriving from outside with no matching entry has nowhere to go, and is dropped. That gives a little protection, and it is why hosting a game or a server at home needs "port forwarding": a permanent entry added by hand.
  • Two devices behind different routers cannot easily reach each other directly. Video calls and games use extra helper servers to get around this.
  • Many people can share one public address. Sometimes a whole neighbourhood or mobile network does. So an IP address does not identify a person, and blocking one address can block thousands of users.

IPv6: enough addresses

The long-term fix is IPv6, which uses 128 bits instead of 32.

2001:db8:85a3::8a2e:370:7334

The number of addresses is about 340 followed by 36 zeros: enough for every device to have its own public address many times over, with no need for NAT. IPv4 and IPv6 run side by side today, and most large sites and mobile networks support both.

Key takeaways

  • An IPv4 address is 32 bits, written as four numbers, and there are only about 4.3 billion.
  • An address splits into a network part and a device part. The number after the slash says where.
  • Private ranges such as 192.168.x.x are reused in every home and are not valid on the internet.
  • NAT lets a router share one public address by rewriting addresses and tracking port numbers.
  • IPv6 uses 128 bits, which gives enough addresses to make NAT unnecessary.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    8 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress