Lesson 1 of 8 in Networking for DevOps
IP addresses, CIDR and subnets
An IP address is the number that identifies a network interface so other machines can send it packets. A subnet is a block of consecutive addresses, written in CIDR notation as an address plus a prefix length such as 10.0.1.0/24. The prefix length says how many leading bits are fixed for the whole block.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Linux and the shell
The problem this solves
Every container host, cloud network and VPN you meet carves addresses into blocks. If you cannot read 10.0.0.0/16 or 172.16.0.0/12, firewall rules, route tables and cloud network settings look like random numbers.
Blocks also collide. Two networks that use the same range cannot be joined cleanly later, so the choice you make on day one is painful to undo.
How it works
- People and clients
- Network
- Runs code
Traffic from the internet reaches a router, and the router forwards it to the public subnet or, only from inside, to the private subnet.
- Internet to Router: 1 traffic arrives
- Router to Public subnet: 2 to 10.0.1.x
- Router to Private subnet: 2 to 10.0.2.x, inside only
- Runs code
- Network
A host compares the destination with its own subnet. A match is delivered directly, and anything else goes to the default gateway.
- Your host to 10.0.1.35: 1 same prefix: direct
- Your host to Default gateway: 2 other prefix: gateway
- Default gateway to 10.0.2.9: 3 gateway forwards
An IPv4 address is 32 bits, written as four numbers from 0 to 255. In 10.0.1.0/24 the /24 means the first 24 bits name the network and the last 8 bits are free for hosts. The free bits give you 2^8 = 256 addresses. A /16 leaves 16 free bits, which is 65,536 addresses. A /28 leaves 4, which is 16.
Three blocks are reserved for private use and are never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 (that is 172.16 through 172.31 only) and 192.168.0.0/16. Your laptop, your containers and most cloud servers sit in these ranges. 127.0.0.1 is loopback, an address that always means this same machine.
In the first diagram the router is the only box that touches several subnets, so it is where traffic moves between them. In the second, a host does a simple check before sending anything. If the destination shares its prefix, it talks to it directly on the local network. If not, it hands the packet to the default gateway and lets the router decide.
Plan subnet sizes with room to grow, and expect some addresses to be taken. Classic networks reserve the first address (the network) and the last (broadcast). Cloud providers reserve more, and AWS reserves five in each subnet. IPv6 uses 128-bit addresses and the same prefix idea, with the same planning questions.
Try it yourself
The ipaddress module ships with Python 3, so you can do subnet maths without installing anything. Nothing here changes your system.
- 1Find your own address
ip -4 addr show
On macOS use
ifconfig. Look for aninetline such as192.168.1.23/24or10.x.x.x/... The part after the slash is the prefix length of your subnet. - 2Count the addresses in a block
python3 -c 'import ipaddress as i; n=i.ip_network("10.0.0.0/24"); print(n.num_addresses, n.network_address, n.broadcast_address)'You should see
256 10.0.0.0 10.0.0.255
- 3Check if an address is inside a subnet
python3 -c 'import ipaddress as i; n=i.ip_network("10.0.1.0/24"); print(i.ip_address("10.0.1.35") in n, i.ip_address("10.0.2.9") in n)'You should see
True False
This is the same test a host performs in the second diagram.
- 4Spot the edge of a private range
python3 -c 'import ipaddress as i; print([i.ip_address(a).is_private for a in ("172.31.255.1","172.32.0.1","192.168.4.4")])'You should see
[True, False, True]
172.32.0.1is outside172.16.0.0/12, so it is a public address. - 5Split a big block into smaller subnets
python3 -c 'import ipaddress as i; print(list(i.ip_network("10.0.0.0/16").subnets(new_prefix=24))[:3])'You should see
[IPv4Network('10.0.0.0/24'), IPv4Network('10.0.1.0/24'), IPv4Network('10.0.2.0/24')]
What breaks, and where to look
You cannot reach a server on your office VPN or home network while Docker is running, or two networks refuse to connect.
- WHY IT HAPPENS
- The ranges overlap. Docker's default bridge network commonly uses
172.17.0.0/16, and VPNs, VPCs and home routers often use10.0.0.0/8or192.168.0.0/16blocks. When two networks claim the same addresses, the host sends packets to the wrong one. - CHECK FIRST
- Compare the ranges with
ip routeandip -4 addr, and in Docker rundocker network inspect bridgeto see its subnet. Pick a non-overlapping range before you build anything that has to be peered later.
New instances or pods fail to start, and the platform says the subnet has no free addresses.
- WHY IT HAPPENS
- The subnet was sized too small. A
/24holds 256 addresses before the provider takes its share, and every instance, container interface or load balancer node can use one. - CHECK FIRST
- Look up the subnet's CIDR in your cloud console or config, calculate
2^(32 - prefix), and compare it with the number of interfaces you actually run. Choose a larger block, such as/20, for networks that will grow.
When not to use this
- You do not need to memorise binary subnet maths. Knowing
/24is 256 addresses,/16is 65,536 and the three private ranges covers most daily work. Useipaddressor an online calculator for the rest. - Large IPv6 networks are not planned by counting hosts, because a single
/64subnet holds far more addresses than you will ever use. Plan by purpose and hierarchy instead.
Check yourself
How many addresses are in a /24, and why?
256. A /24 fixes 24 of the 32 bits, which leaves 8 free host bits, and 2^8 is 256.
Is 172.32.0.1 a private address?
No. The private block 172.16.0.0/12 covers 172.16.0.0 to 172.31.255.255 only.
What does a host do when the destination is in a different subnet?
It sends the packet to its default gateway, and the router works out the next hop.
Keep going
Lesson 16 of 92 in the DevOps path