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
| Prefix | Mask | Addresses | Usable hosts | Block size (in the interesting octet) |
|---|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 254 | 256 |
| /25 | 255.255.255.128 | 128 | 126 | 128 |
| /26 | 255.255.255.192 | 64 | 62 | 64 |
| /27 | 255.255.255.224 | 32 | 30 | 32 |
| /28 | 255.255.255.240 | 16 | 14 | 16 |
| /29 | 255.255.255.248 | 8 | 6 | 8 |
| /30 | 255.255.255.252 | 4 | 2 | 4 |
| /22 | 255.255.252.0 | 1,024 | 1,022 | 4 (third octet) |
| /20 | 255.255.240.0 | 4,096 | 4,094 | 16 (third octet) |
| /16 | 255.255.0.0 | 65,536 | 65,534 | 1 (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.
- The mask is 255.255.255.192, so the block size is 256 − 192 = 64.
- Subnets start at multiples of 64: .0, .64, .128, .192. 77 falls in the one starting at .64.
- Network:
192.168.10.64. Broadcast: one before the next block,192.168.10.127. - Hosts:
192.168.10.65to192.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):
| Network | Needs | Prefix | Subnet | Hosts |
|---|---|---|---|---|
| Sales | 300 hosts | /23 (510) | 172.16.0.0/23 | .0.1 to .1.254 |
| Engineering | 120 hosts | /25 (126) | 172.16.2.0/25 | .2.1 to .2.126 |
| Operations | 50 hosts | /26 (62) | 172.16.2.128/26 | .2.129 to .2.190 |
| Router link A | 2 hosts | /30 (2) | 172.16.2.192/30 | .2.193 to .2.194 |
| Router link B | 2 hosts | /30 (2) | 172.16.2.196/30 | .2.197 to .2.198 |
| Free for later | 172.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/0is 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?
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?
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?
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.