Skip to content

Linux Foundations / Follow traffic and find the fault ยท Step 12 of 46

Lesson: Subnet MathSwitch
โ† Linux Foundations

Network operations

Subnet Math

Calculate subnet sizes, network addresses, broadcast addresses and host ranges.

Subnet math is counting with powers of two. Once you know how many host bits there are, the rest follows.

Size of a subnet

Host bits = 32 minus the prefix length. The number of addresses is 2 to the power of the host bits.

  • /24: 8 host bits, 256 addresses, 254 usable
  • /26: 6 host bits, 64 addresses, 62 usable
  • /30: 2 host bits, 4 addresses, 2 usable

Usable hosts are the total minus 2 (network and broadcast). /31 and /32 are special cases where this rule does not apply.

Finding the block

Take 192.168.10.77/26. The mask is 255.255.255.192. In the last octet, the block size is 256 minus 192, which is 64. Blocks start at 0, 64, 128 and 192. The value 77 falls in the block starting at 64, so:

  • Network: 192.168.10.64
  • Broadcast: 192.168.10.127 (one below the next block)
  • Hosts: 192.168.10.65 to 192.168.10.126

Splitting a block

Each extra prefix bit halves a subnet. Borrowing n bits gives 2 to the power of n subnets. Splitting a /24 into /26 borrows 2 bits and gives 4 subnets.

To choose a size, start from the hosts you need and add 2. For 50 hosts you need at least 52 addresses. The next power of two is 64, so use a /26. Remember that the router's own interface takes one of the host addresses.

Larger blocks

The same method works in any octet. In 172.16.37.9/20, the mask is 255.255.240.0. The block size in the third octet is 16, so 37 lies in the block 32 to 47. The network is 172.16.32.0 and the broadcast is 172.16.47.255.

Check your work

Python's standard library can confirm the answer:

$ python3 -c 'import ipaddress; n = ipaddress.ip_interface("192.168.10.77/26").network; print(n, n.broadcast_address, n.num_addresses)'
192.168.10.64/26 192.168.10.127 64

And it can list the pieces of a split:

$ python3 -c 'import ipaddress; [print(s) for s in ipaddress.ip_network("192.168.10.0/24").subnets(new_prefix=26)]'
192.168.10.0/26
192.168.10.64/26
192.168.10.128/26
192.168.10.192/26

You may also find a tool called ipcalc, but distributions ship different programs under that name, so options and output differ.

More worked examples are in the IPv4 subnetting guide.

Try it

Solve 10.4.5.200/27 on paper: network, broadcast and host range. Then check it with the Python command. Next, plan subnets for 100, 50 and 20 hosts out of one /24, giving the largest one first.

Check your understanding

Subnet Math

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.