DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · ADDRESSING · 29 OF 187

IP Addresses, Subnets and CIDR, Explained Simply

How IP addresses and subnets work without memorising tables: 32 bits in four octets, the network and host parts, CIDR notation (a /24 holds 256 addresses, a /26 64, a /28 16), netmasks, splitting 10.0.0.0/16 into four /18s for a cloud VPC, why AWS reserves five addresses per subnet, and checking any answer with Python's ipaddress module.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • 32 bits
  • network + host
  • size = 2^(32 - prefix)
  • /24 = 256
  • never overlap
  • reserved addresses

Notes

An IPv4 address

An IP version four address is thirty two bits, written as four numbers separated by dots. Each number is one octet, eight bits, so it goes from zero to two hundred and fifty five. Ten dot zero dot one dot seventy seven is just these thirty two bits in a readable form. Two to the power thirty two is about four point three billion addresses, which the internet ran out of long ago, and that is why private networks and NAT, next episode, exist.

10.0.1.77
10       .0       .1       .77
00001010 00000000 00000001
  01001101

four octets of 8 bits
each from 0 to 255
about 4.3 billion in total
  (2 to the power 32)

Network and host

An address has two parts. The first bits name the network, the rest name the machine within it. The slash number, the prefix length, says where the split is. Slash twenty four means the first twenty four bits, ten dot zero dot one, are the network, and the last eight bits, seventy seven, are the host. Machines with the same network part talk directly. Anything else goes through a router, which episode four explains.

10.0.1.77/24
  • first 24 bits: network 10.0.1
  • last 8 bits: host .77
  • same network = same prefix → talk directly
  • different network → send to the router (episode 4)

CIDR sizes

This notation is called CIDR. The size of a network is two to the power of thirty two minus the prefix. We checked each one with Python's ipaddress module. Slash sixteen holds sixty five thousand five hundred and thirty six addresses. Slash twenty four holds two hundred and fifty six. Slash twenty six, sixty four. Slash twenty eight, sixteen. Slash thirty two is a single host. Every extra bit in the prefix halves the network. The netmask is the same idea written as an address.

Checked with Python
/16  65,536 addresses
/24     256  255.255.255.0
/26      64  255.255.255.192
/28      16  255.255.255.240
/32       1  one host

size = 2 ^ (32 - prefix)
each +1 halves the network

Is it inside?

Is ten dot zero dot one dot seventy seven inside ten dot zero dot one dot zero slash twenty six? A slash twenty six holds sixty four addresses, so it runs from dot zero to dot sixty three. Seventy seven is outside, and Python agreed: False. This is exactly the check a security group rule or a firewall makes. When a rule does not match the traffic you expect, check the range first.

ipaddress (our run)
>>> n = ip_network(
...   "10.0.1.0/26")
>>> n[0], n[-1]
10.0.1.0  10.0.1.63
>>> ip_address("10.0.1.77")
...   in n
False

Plan a VPC

Now plan a cloud network. A VPC gets one large block, say ten dot zero slash sixteen, and you carve it into subnets. Splitting it into four slash eighteens gives four blocks of sixteen thousand three hundred and eighty four addresses each: public, private for apps, private for data, and one spare. The golden rule: never overlap with another VPC or your office network, because one day you will need to connect them, and overlapping ranges cannot be routed.

10.0.0.0/16 into four /18s
10.0.0.0/18    public
10.0.64.0/18   private app
10.0.128.0/18  private data
10.0.192.0/18  spare

each /18 = 16,384 addresses
leave room to grow
never overlap with other VPCs
  or the office network

Reserved addresses

Not every address can be used. Traditionally, the first address names the network and the last one is the broadcast address. AWS reserves five in every subnet: the first four and the last, for the router, DNS and future use. So a slash twenty eight in AWS gives you eleven usable addresses, not sixteen. And with some Kubernetes setups every pod takes a VPC address, so a cluster can drain a small subnet surprisingly fast. Size generously.

Lost addresses
  • classic network: first = network, last = broadcast
  • AWS reserves 5 per subnet: .0 .1 .2 .3 and the last
  • a /28 on AWS: 16 - 5 = 11
  • Kubernetes pods may each take a VPC address → size subnets generously

Special ranges

A few ranges come up daily. One two seven is loopback, the machine itself. Ten, one seven two dot sixteen and one nine two dot one six eight are private ranges, next episode. One six nine dot two five four is link-local, and one six nine dot two five four dot one six nine dot two five four is the cloud metadata service every VM talks to. One hundred dot sixty four is carrier grade NAT. And zero dot zero dot zero dot zero, in a listen address, means every interface on the machine.

Know these
  • 127.0.0.0/8 loopback
  • 10/8 172.16/12 192.168/16 private
  • 169.254.0.0/16 link-local 169.254.169.254 = cloud metadata service
  • 100.64.0.0/10 carrier NAT
  • 0.0.0.0 "all" or "any address"

Common mistakes

Mistakes. Overlapping ranges between VPCs or with the office. Subnets too small for autoscaling or Kubernetes pods. Forgetting reserved addresses. Guessing whether an address is in a range instead of computing it. And writing zero dot zero dot zero dot zero slash zero, the whole internet, into a rule by habit.

Avoid these
  • overlapping VPC or office ranges
  • subnets too small for autoscaling or pods
  • forgetting reserved addresses
  • guessing a range instead of computing it
  • 0.0.0.0/0 rules by habit

Say it like this

In the interview: a slash twenty six is two to the power six, sixty four addresses, with netmask two five five dot two five five dot two five five dot one nine two. In AWS five are reserved, so fifty nine are usable. Then go further: for a new VPC I pick a slash sixteen that overlaps nothing, split it into public and private subnets per availability zone, and keep spare space for growth.

In the interview
  • "2 to the 32 minus 26 = 64
  • addresses, mask .192. In AWS
  • 5 are reserved, so 59 usable.
  • For a VPC I take a /16 that does not overlap anything, split
  • it per AZ into public and private subnets, and leave
  • spare space for growth."

Interview questions

Q1How many addresses are in a /24, a /26 and a /28?

256, 64 and 16. The size is 2 to the power (32 minus the prefix length), so each extra bit halves the network.

Q2What does CIDR notation like 10.0.0.0/16 mean?

The first 16 bits (10.0) identify the network; the remaining 16 bits number hosts inside it, giving 65,536 addresses.

Q3What is a netmask?

The prefix written as an address: /24 is 255.255.255.0, /26 is 255.255.255.192. Network bits are 1s, host bits are 0s.

Q4How many usable addresses does an AWS /28 subnet have?

11. AWS reserves 5 addresses in every subnet (the first four and the last), so 16 minus 5.

Q5Why must VPC CIDR ranges not overlap?

Peering, VPNs and transit gateways cannot route between overlapping ranges, so connecting them later becomes impossible without renumbering.

Q6What is 169.254.169.254?

The link-local address of the cloud instance metadata service, used by VMs to get credentials, identity and configuration.

Q7How do you check if an IP is in a range?

Compute it: Python ipaddress (ip_address(x) in ip_network(y)), ipcalc, or compare the network bits; never eyeball it.