Lesson 08 intermediate

Subnetting

Subnet masks and prefix lengths, the block-size shortcut for finding network, broadcast and host addresses in your head, splitting a network into equal subnets, VLSM for subnets of different sizes, supernetting and route summarization, /31 and /32, and checking your math with Python.

Subnetting means cutting one address block into smaller networks. You do it to keep broadcast domains small, to separate departments or tiers, to match a cloud VPC layout, and to avoid wasting addresses. It looks like math homework at first, but one shortcut, the block size, makes almost every question solvable in your head.

The subnet mask

The mask has 1s for the network bits and 0s for the host bits. A /26 mask is 26 ones followed by 6 zeros:

11111111.11111111.11111111.11000000  =  255.255.255.192

Logically AND-ing an address with its mask gives the network address. Every host uses its mask to decide: “is the destination on my network (send directly) or not (send to the gateway)?”

The cheat table

PrefixMaskAddressesUsable hostsBlock size (in the interesting octet)
/24255.255.255.0256254256
/25255.255.255.128128126128
/26255.255.255.192646264
/27255.255.255.224323032
/28255.255.255.240161416
/29255.255.255.248868
/30255.255.255.252424
/22255.255.252.01,0241,0224 (third octet)
/20255.255.240.04,0964,09416 (third octet)
/16255.255.0.065,53665,5341 (second octet)

Two formulas cover everything:

  • Addresses in a subnet = 2^(32 − prefix).
  • Usable hosts = addresses − 2, because the first address is the network address and the last is the broadcast address.
  • Block size = 256 − the mask value in the octet where the mask stops being 255.

Finding network, broadcast and host range

Example 1: 192.168.10.77/26.

  1. The mask is 255.255.255.192, so the block size is 256 − 192 = 64.
  2. Subnets start at multiples of 64: .0, .64, .128, .192. 77 falls in the one starting at .64.
  3. Network: 192.168.10.64. Broadcast: one before the next block, 192.168.10.127.
  4. Hosts: 192.168.10.65 to 192.168.10.126, which is 62 hosts.

Example 2: 172.16.45.200/20. The mask stops in the third octet: 255.255.240.0, block size 16. Multiples of 16 in the third octet: 0, 16, 32, 48. 45 falls in 32. Network 172.16.32.0, broadcast 172.16.47.255, hosts 172.16.32.1 to 172.16.47.254.

Check your work with Python, which every Linux system has:

$ python3 -c "import ipaddress as i; n=i.ip_interface('192.168.10.77/26').network; print(n, n.netmask, n.broadcast_address, n.num_addresses-2)"
192.168.10.64/26 255.255.255.192 192.168.10.127 62

Many distributions also ship ipcalc (ipcalc 172.16.45.200/20), which prints the same facts in a friendly layout.

Splitting a network into equal subnets

Borrowing n host bits creates 2^n subnets. To split 10.0.0.0/24 into 4 subnets, borrow 2 bits: /24 becomes /26.

$ python3 -c "import ipaddress as i; print(*i.ip_network('10.0.0.0/24').subnets(new_prefix=26))"
10.0.0.0/26 10.0.0.64/26 10.0.0.128/26 10.0.0.192/26

Work backwards from what you need. “At least 500 hosts per subnet” means 2^h − 2 ≥ 500, so h = 9 host bits and a /23 (510 hosts). “At least 12 subnets out of a /16” means 2^n ≥ 12, so n = 4 and /20 subnets (16 of them).

VLSM: subnets of different sizes

Equal subnets waste space when needs differ. VLSM (variable length subnet masking) gives each network a prefix that fits. The rule: allocate the largest first, so blocks stay aligned.

Given 172.16.0.0/22 (1,024 addresses):

NetworkNeedsPrefixSubnetHosts
Sales300 hosts/23 (510)172.16.0.0/23.0.1 to .1.254
Engineering120 hosts/25 (126)172.16.2.0/25.2.1 to .2.126
Operations50 hosts/26 (62)172.16.2.128/26.2.129 to .2.190
Router link A2 hosts/30 (2)172.16.2.192/30.2.193 to .2.194
Router link B2 hosts/30 (2)172.16.2.196/30.2.197 to .2.198
Free for later172.16.2.200 to 172.16.3.255

Doing the small ones first would leave gaps that no large subnet can fit into.

Supernetting and summarization

The reverse of subnetting: combine contiguous networks into one shorter prefix. A branch with 192.168.0.0/24, 192.168.1.0/24, 192.168.2.0/24 and 192.168.3.0/24 can be advertised as one route, 192.168.0.0/22.

$ python3 -c "import ipaddress as i; print(*i.collapse_addresses([i.ip_network(f'192.168.{x}.0/24') for x in range(4)]))"
192.168.0.0/22

Summarization keeps routing tables small and hides flapping inside the summary from the rest of the network. It only works when the networks are contiguous and start on a boundary of the summary size: .0 to .3 sums to a /22; .1 to .4 does not.

Special prefixes

  • /31: only 2 addresses and no broadcast. Allowed for point-to-point router links (RFC 3021) and saves addresses compared with /30.
  • /32: a single host. Used for loopback addresses on routers, host routes and firewall rules.
  • /0: everything. 0.0.0.0/0 is the default route.

Subnetting in the cloud

Cloud VPCs follow the same math with a twist: providers reserve extra addresses. AWS keeps 5 per subnet (network, router, DNS, a future one, broadcast), so a /28 gives you 11 usable addresses, not 14. Plan VPC ranges that do not overlap with your offices or other VPCs, or you will not be able to connect them later with VPNs or peering.

Exercise

What is the broadcast address of the subnet containing 10.4.7.140/27?

  1. 10.4.7.143
  2. 10.4.7.159
  3. 10.4.7.191
  4. 10.4.7.255
Show answer

Answer: B. /27 has a block size of 32. Blocks start at 128 and 160, so 140 is in 10.4.7.128/27, whose broadcast is 10.4.7.159.

Exercise

You need subnets with at least 60 hosts each. Which prefix is the smallest that fits?

  1. /26
  2. /25
  3. /27
  4. /28
Show answer

Answer: A. A /26 has 64 addresses, 62 usable. A /27 has only 30 usable hosts. A /25 also fits but wastes 64 addresses.

Exercise

Which single route summarizes 10.1.8.0/24, 10.1.9.0/24, 10.1.10.0/24 and 10.1.11.0/24?

  1. 10.1.0.0/16
  2. 10.1.8.0/23
  3. 10.1.8.0/21
  4. 10.1.8.0/22
Show answer

Answer: D. Four contiguous /24s starting at 8 (a multiple of 4) form a /22 covering 10.1.8.0 to 10.1.11.255. /23 is too small and /21 also covers 12 to 15.

Key takeaways

  • Addresses per subnet = 2^(32 − prefix); usable hosts = that minus 2.
  • Block size = 256 − mask octet; subnets start at multiples of it.
  • Borrow host bits to make more subnets; work backwards from the hosts you need.
  • With VLSM, allocate the biggest subnets first.
  • Summarize contiguous, aligned networks into one shorter prefix.

FAQ

Why subtract 2 from the number of addresses?

The all-zeros host address names the network itself and the all-ones host address is the broadcast. Neither can be given to a device, except on /31 and /32 where the rule does not apply.

Is subnetting still needed with IPv6?

Much less arithmetic is needed. IPv6 LANs are almost always /64, and you hand out /48 or /56 blocks per site, so you count subnets in hex digits instead of squeezing hosts. Lesson 9 shows how.

How do I avoid mistakes in production?

Do the math in your head to understand it, then verify with ipcalc or Python before you configure anything. Keep an IP address management (IPAM) record, even a simple spreadsheet, of every allocated subnet.

What happens if two subnets overlap?

Routers cannot tell which one a destination belongs to and pick the more specific route, which sends traffic to the wrong place. Overlaps are the most common reason VPNs and cloud peering fail to connect.

↑ ↓ navigate Enter open Esc close