Sub topic 10
Public and private addresses
This chapter assumes you can read dotted-decimal addresses and understand prefix lengths such as /24 and /12. It also assumes you know that a router forwards packets between networks. The Python examples use only the standard library.
Why the split exists
An IPv4 address is a 32-bit number, so there are at most 2^32 = 4,294,967,296 of them. Many are reserved, and far more devices exist than addresses. Two ideas stretch the supply.
- Some addresses are reserved for use inside organizations. Many unrelated networks can reuse them at the same time.
- Everything else is allocated so that each address belongs to exactly one holder on the internet.
The first group is private. The second group is public. The split is a matter of routing policy, not of the address bits. Nothing in the binary form of 192.168.1.10 marks it as private. It is private because a standards document says so and because internet routers are configured to act on that.
What "public" means
A public address is globally unique and globally routable. Regional internet registries allocate blocks to providers and large organizations, and those holders announce the blocks to the rest of the internet through routing protocols (BGP in practice). If you hold 203.0.113.0/24, routers everywhere learn that traffic for that block should be sent toward you.
Two properties follow from this.
- Uniqueness. Only one holder can own a given public address at a time. If two networks both announced
8.8.8.0/24, traffic would go to whichever announcement routers preferred, and the other party would be unreachable. - Reachability. A host with a public address can, in principle, be contacted from anywhere. Whether it is actually reachable depends on firewalls and services, but the routing path exists.
What "private" means
A private address comes from a range that no one owns globally. RFC 1918 reserves three such ranges for use in private networks. The next chapter covers them in detail, so here is only the list.
| Range | Prefix | Typical use |
|---|---|---|
| 10.0.0.0 to 10.255.255.255 | 10.0.0.0/8 | Large organizations, cloud virtual networks |
| 172.16.0.0 to 172.31.255.255 | 172.16.0.0/12 | Medium networks, container platforms |
| 192.168.0.0 to 192.168.255.255 | 192.168.0.0/16 | Home and small-office networks |
Anyone may use these addresses without asking a registry. The rule that makes this safe is that private addresses are never routed across the public internet. Internet service providers filter them, so a packet whose destination is 10.1.2.3 is dropped at the edge or is delivered to a network of your own, never to someone else's 10.1.2.3.
Because the addresses are reused everywhere, a private address only identifies a host within one network. Your laptop at 192.168.1.10 and a stranger's laptop at 192.168.1.10 do not conflict, because their networks never meet directly.
How the two kinds interact
A private host cannot be reached from the internet by its private address, since no route exists. It can still talk to public servers, because a device at the network edge rewrites the source address of outgoing packets to a public one. That mechanism is network address translation (NAT), covered in its own chapter. Remember for now that "private" describes the address, and "can reach the internet" depends on whether a translating device sits at the edge.
Addresses that are neither public nor RFC 1918
Public and private are not the only two categories. Several other ranges are reserved for particular jobs, and none should appear as an ordinary public host address.
| Range | Name | Meaning |
|---|---|---|
| 127.0.0.0/8 | Loopback | Traffic to the host itself. Never leaves the machine. |
| 169.254.0.0/16 | Link-local | Self-assigned when no DHCP server answers. Valid only on the local link. |
| 224.0.0.0/4 | Multicast | One sender, many receivers. Not a host address. |
| 240.0.0.0/4 | Reserved | Formerly class E. Not usable as normal host addresses. |
| 100.64.0.0/10 | Shared address space | Used between a carrier's customer equipment and its carrier-grade NAT. |
| 0.0.0.0/8 | "This network" | `0.0.0.0` means "unspecified" and appears as a source before a host has an address. |
Documentation ranges (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24) are also reserved. They are safe to use in examples and are never assigned to real hosts. This tutorial's own examples avoid inventing "public" addresses by using them where it can.
The shared range 100.64.0.0/10 is the easiest to misclassify. It is not RFC 1918, so a check for the three private ranges will say "no". It is also not globally routable, so treating it as public is wrong. A later chapter covers it in full.
Checking an address in Python
The first approach is to test membership in the three RFC 1918 networks explicitly. This is precise and easy to audit.
import ipaddress
RFC1918 = [
ipaddress.ip_network("10.0.0.0/8"),
ipaddress.ip_network("172.16.0.0/12"),
ipaddress.ip_network("192.168.0.0/16"),
]
def is_rfc1918(text):
address = ipaddress.ip_address(text)
return any(address in network for network in RFC1918)
print(is_rfc1918("10.20.30.40")) # True
print(is_rfc1918("172.31.255.254")) # True
print(is_rfc1918("172.32.0.1")) # False
print(is_rfc1918("8.8.8.8")) # FalseThe 172.32.0.1 result matters. The /12 mask means the second octet's upper four bits are fixed at 0001, so the range ends at 172.31.255.255. Addresses starting 172.32. and above are public, and people who match on the text prefix 172. get this wrong.
The standard library also provides an is_private property. It is convenient, but it answers a broader question than RFC 1918.
import ipaddress
print(ipaddress.ip_address("192.168.1.10").is_private) # True
print(ipaddress.ip_address("127.0.0.1").is_private) # True
print(ipaddress.ip_address("8.8.8.8").is_private) # False127.0.0.1 is not an RFC 1918 address, yet is_private returns True because the property also covers loopback, link-local, and other special-purpose ranges.
For a fuller classification, combine explicit checks with the other properties. The order matters, because the shared range must be tested before is_global.
import ipaddress
RFC1918 = [
ipaddress.ip_network("10.0.0.0/8"),
ipaddress.ip_network("172.16.0.0/12"),
ipaddress.ip_network("192.168.0.0/16"),
]
SHARED = ipaddress.ip_network("100.64.0.0/10")
def classify(text):
address = ipaddress.ip_address(text)
if any(address in network for network in RFC1918):
return "private (RFC 1918)"
if address in SHARED:
return "shared (carrier-grade NAT)"
if address.is_loopback:
return "loopback"
if address.is_link_local:
return "link-local"
if address.is_multicast:
return "multicast"
if address.is_global:
return "public"
return "other special-purpose"
for sample in ["192.168.0.1", "172.32.0.1", "100.64.0.1", "127.0.0.1",
"169.254.10.20", "224.0.0.5", "8.8.8.8", "255.255.255.255"]:
print(sample, "->", classify(sample))Expected output:
192.168.0.1 -> private (RFC 1918)
172.32.0.1 -> public
100.64.0.1 -> shared (carrier-grade NAT)
127.0.0.1 -> loopback
169.254.10.20 -> link-local
224.0.0.5 -> multicast
8.8.8.8 -> public
255.255.255.255 -> other special-purposeCommon errors
- Assuming "private" means "secure". A private address only means the address is not routable from the internet. It gives no protection by itself. Anything on the same internal network, and anything reached through a VPN, can still talk to the host.
- Assuming "public" means "exposed". A host with a public address can still be protected by a firewall that drops unsolicited traffic.
- Matching on text prefixes. Checking
startswith("172.")classifies172.32.0.1as private. Check against networks instead. - Treating everything not private as public. Loopback, link-local, multicast, and shared addresses are not public hosts either.
- Using the same private range in two networks you later join. Overlapping
10.0.0.0/8subnets cannot be routed to each other without translation. A later chapter on pitfalls covers this.
Summary
Public addresses are unique and routable worldwide. Private addresses are reused inside many networks and are filtered at the internet's edge. A third group of special-purpose ranges is neither. When you must make a decision from an address, test it against explicit networks, and test the shared and special ranges before you call anything public.
Exercise
A script classifies addresses with address.startswith("172.") as private. Name one address that it classifies wrongly, and say what the correct classification is.
Show answer
172.32.0.1 (or any address from 172.32.0.0 upward in the 172 block, such as 172.200.1.1). The RFC 1918 range is 172.16.0.0/12, which ends at 172.31.255.255, so these addresses are public. The same script also misses private addresses that do not start with 172., but the key bug is the false positive.
Exercise
ipaddress.ip_address("127.0.0.1").is_private returns True. Is 127.0.0.1 an RFC 1918 address? Which function from this chapter answers that question correctly?
Show answer
No. 127.0.0.1 is a loopback address from 127.0.0.0/8, which is not one of the three RFC 1918 ranges. is_private is broader than RFC 1918 because it also covers loopback and other special-purpose ranges. is_rfc1918("127.0.0.1") returns False, which is the correct answer to the RFC 1918 question.
Exercise
Your cloud provider gives a host the address 100.64.3.9. Is it public, RFC 1918 private, or something else? Would is_rfc1918 and is_global agree with your answer?
Show answer
It is something else: shared address space (100.64.0.0/10), used for carrier-grade NAT. is_rfc1918 returns False because the address is not in any of the three RFC 1918 ranges. is_global also returns False, because the address is not globally routable. So the host is neither RFC 1918 private nor public, and only an explicit check against 100.64.0.0/10 identifies it.