Platform Engineering · Lesson F14

Private and Public IP Addresses

Millions of homes use the exact same address, 192.168.1.1, and the internet does not get confused. That is because some addresses are private: they work inside a network and nowhere else. This lesson is about the two kinds of address, the translator at your front door, and the one address that means this machine.

Difficulty: Beginner · Know this first: F13 — What Is an IP Address? IPv4

Run it as:

Your laptop, your phone, your TV, all on one home network. Watch which address each one has, and which address the internet sees.

When it breaks, I check

One thing to remember

How big each private range is

The three private ranges are very different sizes, and the bar below is exact. The 10 range holds 16,777,216 addresses. The 172.16 range holds 1,048,576. The 192.168 range holds 65,536. The loopback range, 127, holds 16,777,216. Add them up and the bar shows why the big range is the one companies pick and the small one is the one homes pick.

Extra diagram · from scratch to master

A packet leaves the house

Follow one packet from your laptop to the internet. Watch the address change at the front door, and change back on the way home.

2 addresses on one packet1 translation table1 front door
Laptop
192.168.1.5
Home router
the front door
NAT table
remembers the swap
Internet
sees 203.0.113.45
Reply
comes back
Laptop again
192.168.1.5
Request / flowResponse · the way backFailure / dead end

Extra diagram · from scratch to master

Three ranges, three homes

Three networks, three private ranges. Same idea, different sizes, and each size fits the network that picked it.

3 private ranges17,891,328 private addresses1 rule
Your home
192.168.1.0
A small office
192.168.0.0
A big company
10.0.0.0
A cloud network
172.16.0.0
Loopback
127.0.0.1
The internet
public only
Request / flowResponse · the way backFailure / dead end

Try it: change the inputs and watch it bend

The simulator below checks any address you type and tells you which kind it is. It also shows what happens at the front door: if the address is private, the router translates it to a public address before it leaves the house. The translation uses the documentation range 203.0.113.0, which is reserved for examples like this one. Every value is computed live.

e.g. 192.168.1.5

Four numbers, 0 to 255, separated by dots. The checker reads the kind and shows the NAT translation the router would apply.

Try this: (1) Type 10.4.2.9. It is private, in the 10/8 range, and the router translates it to 203.0.113.45. (2) Type 172.20.5.1. It is private, in the 172.16/12 range. (3) Type 127.0.0.1. It is loopback, and it never leaves the machine, so there is no translation. (4) Type 8.8.8.8. It is public, and it needs no translation at all.

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Basic to master

I take the same idea four times here. First I tell private from public. Then I read the ranges. Then I design addressing for a network. Last I run it for the world.

Basic: Private and public

Definition: A public address is unique on the whole internet. A private address is unique only inside one network, and millions of networks can reuse the same one. Private addresses never travel on the public internet.

Types:

  • Public addresses, one per house or office on the internet.
  • Private addresses, inside homes and offices.
  • Loopback, 127.0.0.1, which means this machine.
  • Link-local, 169.254, used when no address is assigned.

Uses: I use this split every time I see an address and need to know where it works. Private means inside. Public means anywhere. Loopback means here.

Dimensions: At home the split is invisible: everything just works. What breaks first is my assumption that an address I can see from inside can be reached from outside, and I notice it when a friend cannot reach my home server. It costs nothing to learn the ranges. It costs real confusion to guess. The trade is simple: private addresses are free to reuse, and they cannot be reached from outside.

Intermediate: Reading the ranges

Definition: At this level I know the three private ranges by heart: 10/8 with 16,777,216 addresses, 172.16/12 with 1,048,576, and 192.168/16 with 65,536. I also know loopback, 127/8, and link-local, 169.254/16. The range tells me the kind, and the kind tells me where the address works.

Types:

  • 10/8, the big range, for companies and clouds.
  • 172.16/12, the middle range, for clouds and offices.
  • 192.168/16, the small range, for homes and small offices.
  • 127/8, loopback, for one machine talking to itself.

Uses: I use this when a log shows an address I do not recognise. The range names the kind in one glance, and the kind tells me whether the address should be reachable from where I am sitting.

Dimensions: As the networks grow, the ranges show up everywhere in logs and configs. What breaks is a range used in the wrong place, like a home range in a company network, and I notice it when two networks that must connect turn out to use the same range. It costs a written plan for which range goes where. The trade is that three ranges are easy to remember, and easy to collide if nobody plans.

Advanced: Designing with NAT

Definition: At this level I design the translation. The router holds the public address. Inside, machines use private addresses. NAT keeps a table of every conversation, rewriting addresses on the way out and back. Servers that must be reachable from outside get a fixed rule that forwards one port to one inside machine.

Types:

  • Outbound NAT, which lets a whole network share one public address.
  • Port forwarding, which lets the outside reach one inside machine.
  • NAT tables, which remember who asked for what.
  • Static mappings, which never change.

Uses: I use this when a service inside must be reachable from outside. I write one forwarding rule, for one port, to one machine, and I test it from outside the network, not from inside.

Dimensions: At ten times the machines the NAT table grows, and the router works harder to track every conversation. What breaks is a table that fills up, and I notice it when new connections fail while old ones still work. It costs a router with room in its table. The trade is that NAT buys sharing and costs a single point of translation: if the router dies, the whole network loses the internet.

Private on the inside, public on the outside. Your machine has a private address that only your network sees. The internet sees a different address, the public one. I compare the two, because a machine that shows its private address to the internet is a machine that cannot be reached, or a machine that is leaking its layout.

$ hostname -I
198.19.0.2 fd8b:4f84:7d32:99::2
$ curl -s -m 5 ifconfig.me
2606:54c1:1:14ed::53e:76

Every flag in the commands above, letter by letter.

  • hostname -I: -I prints the machine's IP addresses, nothing else. Capital I. The private address is the one your home network gave you. The public address comes back from ifconfig.me, which is a server on the internet answering with the address it saw.
  • curl -s: -s means silent. It hides the progress meter.
  • curl -m 5: -m 5 gives up after 5 seconds. It is the short form of --max-time 5. Same flag, shorter spelling. Without it, a slow server makes you wait.
  • One more flag worth knowing: hostname -f prints the full name with its domain, if the machine has one. On a machine with no domain, it prints the short name.

Check it: run hostname -I and curl -s -m 5 ifconfig.me. The first prints your private addresses. The second prints the public one. Then run hostname -f.

Try it: run both commands. The first prints your private address. The second prints the address the internet sees. They are different, and they should be. If they are the same, your machine is directly on the internet, and that needs a second look.

The address that changes. Most home and office networks hand out addresses with DHCP, and the address can change when the machine reconnects. I never write a private address down as if it were permanent. I check it again after a reconnect, because the service that worked yesterday may be on a different address today.

$ hostname -I
198.19.0.2 fd8b:4f84:7d32:99::2

Try it: run hostname -I, disconnect and reconnect your network, and run it again. If the address changed, anything that pointed at the old address is now pointing at nothing. A service that needs a stable address gets a reserved one, not a DHCP one.

A scope is the pool. The DHCP server does not hand out any address it likes. It hands out addresses from a scope, a range the admin wrote down, like 192.168.1.100 to 192.168.1.200. An address outside the scope is never handed out. When the scope runs dry, new machines get nothing, and that looks like a broken network when it is really a full pool.

A lease is a loan, not a gift. The address comes with an expiry, often a day. Halfway through, the machine asks to renew. If the machine leaves the network, its address goes back into the pool for the next machine. That is why your address can change after a reconnect.

A reservation is the same address every time. A printer or a server needs a stable address. The admin ties one address to that machine's MAC address, and the server hands it the same one at every reconnect. It is still DHCP. It just never changes.

A relay crosses routers. A machine that wants an address shouts. That shout is a broadcast, and broadcasts do not cross a router. So each network has a relay agent. It catches the shout and forwards it to the DHCP server, then carries the answer back. That is how one server hands out addresses for many networks.

Try it: run ip addr show and look for the valid_lft line. It shows how long your address is good for. On this machine I ran it and saw valid_lft forever, which means the address was set by hand, not leased. On your home network the same line shows seconds left on the lease, counting down to renewal.

$ ip addr show host0
    inet 198.19.0.2/30 scope global host0
       valid_lft forever preferred_lft forever

Master: Running it for the world

Definition: At this level public addresses are a managed resource across regions and clouds. Private ranges are planned so no two networks that must connect ever overlap. NAT is designed, monitored, and given spares. Every public address has an owner, a purpose, and a record.

Types:

  • Public blocks owned and registered per region.
  • Private ranges planned across every network.
  • NAT gateways with spares and monitoring.
  • Overlap checks before any two networks are joined.

Uses: I use this when networks span regions and clouds. It is also how I review addressing I did not build: I ask which ranges each network uses, where they overlap, and who owns each public address.

Dimensions: At a hundred times the scale the shortage is the story, and every public address is shared, translated, and guarded. What breaks is an overlap between two networks that must now join, and I notice it when the join works for half the machines and fails for the rest. It costs planning before building. The trade is that planning costs time up front, and it buys networks that can join without renumbering.

One public address, many private machines. A whole office can hide behind one public address. The router translates each private address to the public one on the way out, and back again on the way in. That is NAT, and it is why the internet did not run out of addresses years ago. It is also why a machine behind NAT cannot be reached from outside unless someone opens a door for it.

$ ss -tuln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process

Every flag in the commands above, letter by letter.

  • ss -tuln, letter by letter: -t means TCP sockets. -u means UDP sockets. -l means listening only. -n means numeric, show addresses and ports as numbers instead of looking up names.
  • Without -l, ss shows every connection, not just listeners. Without -n, it pauses to resolve names, and the output is slower and harder to match against a config file.
  • One more flag worth knowing: ss -p shows which process owns each socket. You need root to see other users' processes. When a port is already in use, -p names the program holding it.

Check it: run ss -tuln, then ss -tln, then ss -tulnp. The first shows TCP and UDP listeners. The second drops UDP. The third adds a Process column.

Try it: run ss -tuln. It lists what your machine is listening on. An empty list means nothing on your machine accepts connections from outside. That is the safe default. Every line in that list is a door, and every door needs a reason to be open.

Go deeper

Private is not the same as secure. A private address is not hidden from attackers. It is simply not routable on the internet. Anyone inside your network can reach it. Security comes from the firewall and the router rules, not from the address being private. I never rely on private addressing as a security measure.

NAT is a workaround that became permanent. It was invented to stretch the public address space, and it worked so well that it became part of how the internet runs. It also broke the original idea that every machine could talk directly to every other machine. That break is why some applications need special help to work through NAT.

The port in a NAT translation is doing real work. When two laptops in one house both visit the same website, the router gives each conversation a different port, so the replies can be told apart. The address is shared. The port is what keeps the conversations separate. That is why the table records both.

Link-local is the address a machine gives itself when nobody gives it one. If your laptop shows 169.254, it is telling you it asked for an address and nobody answered. That is a useful symptom: it points at the address service, not at the machine.

At 10x the machines, the NAT table is the thing to watch. Every conversation is an entry, and entries cost memory. A busy office can hold tens of thousands of entries. When the table fills, new connections fail first, and the symptom looks like the internet is down when it is not.

At 100x, the design changes. One router becomes a pair, so the translation survives a failure. Public addresses are pooled and shared. And the long-term answer is the same as before: a new address format with room for every device, which is the lesson after next.

Try it: the two addresses. Run hostname -I for your private address and curl -s ifconfig.me for your public address. Write both down. The private one starts with 10, 172.16 to 172.31, or 192.168. The public one does not. If you cannot tell which is which, the address ranges in this lesson are the check.

$ hostname -I
198.19.0.2 fd8b:4f84:7d32:99::2
$ curl -s -m 5 ifconfig.me
2606:54c1:1:14ed::53e:76

One time it broke

What broke

A small office moved into a new building, and the network team set up the new office with the same private range as the old one: 10.0.0.0. Then they connected the two offices with a VPN so people could reach both. It did not work. Half the machines were unreachable from the other office, because both offices had a machine called 10.0.0.50, and the network could not tell them apart.

Timeline

  • Mon 10:00,the new office network comes up on 10.0.0.0, the same range as the old office. Nobody checks for a clash.
  • Mon 14:00,the VPN link between the offices is turned on. The link itself works.
  • Mon 14:20,people in the new office try to reach the old file server at 10.0.0.50. They reach a printer in their own office instead.
  • Mon 15:00,I compare the two address plans. Both offices use 10.0.0.0. Every address exists twice, once in each office.
  • Tue 09:00,the new office is renumbered to 10.1.0.0. It takes a morning, and every fixed address is updated.
  • Tue 13:00,the VPN works. The file server is reachable, the printer is a printer again, and the address plans are written down side by side.

What changed after

Three things. First, a rule: no new network is built without checking its range against every network it will ever connect to. Second, an address plan on one page, listing every network and its range, kept where the network team can see it. Third, a habit: when two networks must join, the overlap check happens before the link is turned on, not after. Renumbering a live office costs a morning. Planning costs ten minutes.

If you remember nothing else

Design exercise

Design the addressing for a small company with two offices. The main office has 120 machines. The branch office has 30 machines. The two offices are joined by a VPN, so machines in each office must reach machines in the other. Pick the private ranges.

Clarifying questions to ask

  • Which private range does each office get, and why?
  • How do you make sure the two ranges never overlap?
  • Which machines get fixed addresses, and which get automatic ones?
  • What does the router in each office do with a packet addressed to the other office?
  • What breaks if the branch office grows to 300 machines?

Rough sizing

The main office gets 10.0.0.0, which holds 16,777,216 addresses, far more than 120 machines need, and leaves room to grow. The branch office gets 10.1.0.0, a different corner of the same big range, so the two offices can never collide. The branch has 30 machines today and room for thousands. Each office router holds one public address, and NAT translates for the whole office. The VPN carries packets between the two private ranges directly, with no translation, because both ranges are private and both routers know the road.

Trade-offs to name

Using the big 10 range for both offices buys room to grow and costs nothing, because the range is huge. Using the same range for both would buy nothing and cost a collision, so the ranges must differ. A VPN between private ranges buys direct reach and costs a link that must be watched. And the plan must be written down, because the person who builds the third office needs to know which corners are already taken.

Next in this series
Lesson F15 — Subnets: Cutting a Network into Pieces