Lesson 1 of 8 in Networking for DevOps

IP addresses, CIDR and subnets

Core8 min

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

One network split into subnets by a router
One network split into subnets by a routerTraffic from the internet reaches a router, and the router forwards it to the public subnet or, only from inside, to the private subnet.1 trafficarrives2 to 10.0.1.x2 to 10.0.2.x, inside onlyInternetoutside worldRouterjoins the subnetsPublic subnet10.0.1.0/24Private subnet10.0.2.0/24
  • 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.

  1. Internet to Router: 1 traffic arrives
  2. Router to Public subnet: 2 to 10.0.1.x
  3. Router to Private subnet: 2 to 10.0.2.x, inside only
How a host decides where to send a packet
How a host decides where to send a packetA host compares the destination with its own subnet. A match is delivered directly, and anything else goes to the default gateway.1 same prefix: direct2 otherprefix:gateway3 gatewayforwards10.0.1.35same subnetYour host10.0.1.20/24Default gateway10.0.1.110.0.2.9other subnet
  • 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.

  1. Your host to 10.0.1.35: 1 same prefix: direct
  2. Your host to Default gateway: 2 other prefix: gateway
  3. 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.

  1. 1
    Find your own address
    ip -4 addr show

    On macOS use ifconfig. Look for an inet line such as 192.168.1.23/24 or 10.x.x.x/... The part after the slash is the prefix length of your subnet.

  2. 2
    Count 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
  3. 3
    Check 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.

  4. 4
    Spot 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.1 is outside 172.16.0.0/12, so it is a public address.

  5. 5
    Split 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 use 10.0.0.0/8 or 192.168.0.0/16 blocks. When two networks claim the same addresses, the host sends packets to the wrong one.
    CHECK FIRST
    Compare the ranges with ip route and ip -4 addr, and in Docker run docker network inspect bridge to 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 /24 holds 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 /24 is 256 addresses, /16 is 65,536 and the three private ranges covers most daily work. Use ipaddress or an online calculator for the rest.
  • Large IPv6 networks are not planned by counting hosts, because a single /64 subnet 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.

Lesson 16 of 92 in the DevOps path

Search

Search cheat sheets and projects