Knowledge

IPv4: subnets and CIDR

Updated 2026-11-08 · Present

Estimated time: 50 minutes.

Quick summary

Step 1 — Determine how many bits to add

/24 has 8 host bits. To get 4 subnets → you need to move 2 bits from the host part to the network part (2^2 = 4). Result: /26.

When it breaks: Miscounting the bits → picking the wrong CIDR → either too few subnets (not enough to split departments/tiers) or subnets that are too small (not enough IPs for hosts). In a cloud deployment, a subnetting mistake at this step means tearing down the whole VPC.

# Rule: need N subnets → take ceil(log2(N)) bits from the host part
# 4 subnets → 2 bits → /24 + 2 = /26

Observe with:

python3 -c "import math; n=4; bits=math.ceil(math.log2(n)); print(f'Need {bits} bits, new CIDR: /24 + {bits} = /{24+bits}')" — verify the calculation.

Step 2 — Calculate the size of each /26 subnet

/26 has 32−26 = 6 host bits → 2^6 = 64 addresses/subnet → 62 usable hosts. Enough for ~60 hosts per department.

When it breaks: Confusing 64 total addresses with 64 usable → planning comes up 2 IPs short → the last subnet lacks an address for 1 host. On AWS, with 5 more reserved IPs subtracted, a /26 has only 57 usable in practice.

# Block size = 256 / number of subnets = 256 / 4 = 64
# Or: 2^(32 - CIDR) = 2^6 = 64

Observe with:

python3 -c "import ipaddress; n=ipaddress.ip_network('192.168.10.0/26'); print('Total:', n.num_addresses, '| Usable:', n.num_addresses-2, '| First:', next(n.hosts()), '| Last:', list(n.hosts())[-1])" — see the full subnet information.

Step 3 — List the 4 subnets

Networks start at multiples of 64 (the block size): 0, 64, 128, 192. This rule lets you work it out in your head, no pen and paper needed.

When it breaks: Not grasping the rule "boundary = a multiple of the block size" → miscalculating the network/broadcast of each subnet → assigning conflicting IPs or running short of IPs. This error is only discovered when two machines cannot ping each other.

Subnet 1: 192.168.10.0/26    (hosts .1 → .62,   broadcast .63)
Subnet 2: 192.168.10.64/26   (hosts .65 → .126, broadcast .127)
Subnet 3: 192.168.10.128/26  (hosts .129 → .190, broadcast .191)
Subnet 4: 192.168.10.192/26  (hosts .193 → .254, broadcast .255)

Observe with:

python3 -c "import ipaddress; [print(str(s)) for s in ipaddress.ip_network('192.168.10.0/24').subnets(new_prefix=26)]" — prints the 4 /26 subnets from the /24.

Symptoms when it breaks:

How to fix:

  1. Detect the IP conflict with arping — see whether 2 MACs reply

    sudo arping -D -I eth0 192.168.10.65
    
  2. Look at the ARP table to find the duplicate

    ip neigh show | sort
    
  3. Change the conflicting IP to an unused IP in the subnet

    sudo ip addr del 192.168.10.65/26 dev eth0 && sudo ip addr add 192.168.10.70/26 dev eth0
    

Step 4 — Verify in binary

The 2 bits at positions 25-26 are the "subnet ID". 00/01/10/11 are exactly the 4 subnets. When you hesitate between /27 and /28, looking at these bits tells you right away.

When it breaks: Skipping the binary check → when you meet an unfamiliar CIDR (/19, /21) you cannot work out the subnet boundary → you depend entirely on tools → when no tool is available (a VPS during an incident) you are blind.

              bit 25-26↓
S1: 192.168.10. 00 000000 = .0
S2: 192.168.10. 01 000000 = .64
S3: 192.168.10. 10 000000 = .128
S4: 192.168.10. 11 000000 = .192

Observe with:

python3 -c "ip='192.168.10.64'; print(' '.join([format(int(o),'08b') for o in ip.split('.')]))" — see the binary representation of any IP.

Step 5 — Subnet mask for /26

26 one-bits + 6 zero-bits = 11111111.11111111.11111111.11000000 = 255.255.255.192. The number 192 comes from 128+64 = the first 2 bits of the last octet.

When it breaks: Entering the wrong subnet mask when configuring a static IP on a server → the host sends traffic to the wrong gateway → connectivity is lost immediately. This is the #1 error when admins configure servers manually without DHCP.

255.255.255.192
# Rule to remember: the last octet of the mask for CIDRs that fall in the last octet:
#   /25=128, /26=192, /27=224, /28=240, /29=248, /30=252

Observe with:

python3 -c "import ipaddress; n=ipaddress.ip_network('192.168.10.0/26'); print('Mask:', n.netmask, '| Wildcard:', n.hostmask)" — confirm the subnet mask and wildcard.

Step 6 — Assign subnets to departments + gateway

Convention: the gateway is usually the .1 of the subnet. Fix it so devs/admins can remember it. The DHCP pool starts at .2.

When it breaks: Assigning a gateway IP outside the subnet → the host cannot ping the gateway → cannot reach the internet even though the switch/cable are fine. Example: subnet 192.168.10.64/26 with gateway 192.168.10.1 = the gateway is in a different subnet.

Sales    : 192.168.10.0/26   GW 192.168.10.1
Eng      : 192.168.10.64/26  GW 192.168.10.65
HR       : 192.168.10.128/26 GW 192.168.10.129
Guest    : 192.168.10.192/26 GW 192.168.10.193

Observe with:

ip route show — after configuring, check whether the default route goes via the correct gateway address. ping <gateway_ip> — if the gateway answers ping, routing within the subnet is OK.

Step 7 — Verify with a tool

After calculating by hand you must cross-check. One wrong bit = the whole subnet is broken.

When it breaks: Not verifying → deploying with a wrong subnet → a production incident. A verification tool gives results instantly; there is no reason to skip it. Always cross-check in at least 2 ways (manual + tool).

# Install once: sudo apt install ipcalc
ipcalc 192.168.10.100/26

# Key lines in the output (binary column omitted):
# Network:   192.168.10.64/26
# HostMin:   192.168.10.65
# HostMax:   192.168.10.126
# Broadcast: 192.168.10.127
# Hosts/Net: 62
# Correct: .100 belongs to subnet 2 (64-127)

Observe with:

ipcalc 192.168.10.100/26 — if it is installed (apt install ipcalc). Or: python3 -c "import ipaddress; h=ipaddress.ip_interface('192.168.10.100/26'); print('Network:', h.network, '| Host OK:', h.ip in h.network)"

Scripting: Use this when you need to quickly verify a subnet on a server without ipcalc. Run it ad hoc or embed it in a deploy script to validate the CIDR input before ip addr add.

#!/bin/bash
# subnet-info.sh — Print subnet information from CIDR notation
# Usage: ./subnet-info.sh 192.168.10.100/26
# No need to install ipcalc, only python3 (preinstalled on Ubuntu/Debian)
CIDR=${1:?"Usage: $0 <ip/prefix> — e.g. 192.168.10.100/26"}
python3 - <<EOF
import ipaddress, sys
try:
    iface = ipaddress.ip_interface('$CIDR')
    net   = iface.network
    hosts = list(net.hosts())
    print(f'Input    : $CIDR')
    print(f'Network  : {net.network_address}/{net.prefixlen}')
    print(f'Broadcast: {net.broadcast_address}')
    print(f'Mask     : {net.netmask}')
    print(f'Usable   : {len(hosts)}  ({hosts[0]} — {hosts[-1]})')
except Exception as e:
    print(f'Error: {e}'); sys.exit(1)
EOF

Common misconceptions

Confusing classful addressing with CIDR leaves you unable to design arbitrary subnets — you keep thinking a subnet must be /8, /16, /24 along classful boundaries, ignoring the possibility of /22, /26, /28 for specific use cases.

Designing a site-to-site VPN without checking for IP overlap first → the tunnel comes up but routing is ambiguous → traffic goes to the wrong destination. You must use different CIDR notation for each site or use NAT-over-VPN to resolve the conflict.

Choose /28 for a production subnet (16 − 5 = 11 usable on AWS), then add more services → you run out of IPs → re-subnetting is very laborious. Calculate enough up front before creating the VPC, because AWS does not let you change the CIDR of a subnet that has already been created.

Logging and compliance auditing: the server log records the NAT gateway's IP, not the real machine's IP. You need to enable VPC Flow Logs (AWS) or an equivalent to trace a private IP back when investigating a security incident.

Configuring a server to bind to 127.x.x.x (not 127.0.0.1) thinking it is a LAN IP → the service is only accessible from loopback, and no host in the LAN can connect — a hard error to spot if you do not know the loopback range.

Try it at home

1. Check the current IP + subnet mask on a Linux machine — identify the subnet in use

ip -br addr show

Observation: Look for the interface line with state UP and an IP/CIDR address. Format: 'eth0 UP 192.168.1.10/24'. The /24 is the CIDR of the current subnet. If it is /24, the subnet range is .0-.255 and the gateway is usually .1.

2. Look at the routing table — see which subnet goes through which gateway

ip route

Observation: Look for the line 'default via X.X.X.X' = the default gateway. The other lines are 'connected routes' — the subnets the machine is directly attached to. A subnet in the routing table = the machine can send packets to it directly without a router.

3. Test whether 2 IPs are in the same subnet using the ipcalc tool (needs installing: apt install ipcalc)

ipcalc 192.168.1.100/26
ipcalc 192.168.1.200/26
# Compare the Network of the 2 lines — different = different subnets

Observation: Compare the 'Network:' line of the 2 commands. 192.168.1.100/26 → Network 192.168.1.64. 192.168.1.200/26 → Network 192.168.1.192. Different = the 2 machines are in different subnets → they must go through a router.

4. Create a custom Docker network with its own CIDR — avoid clashing with the VPN/office

docker network create --subnet 10.99.0.0/24 mylab
docker network inspect mylab | grep Subnet

Observation: docker network inspect mylab | python3 -m json.tool — see the full config. IPAM.Config will contain the Subnet and Gateway. Run a container in this network and ping the gateway to confirm the subnet works.

5. Verify broadcast: ping the broadcast address → see many hosts reply (if the OS allows it)

# /24 broadcast
ping -b 192.168.1.255

Observation: If several machines in the subnet reply, you see multiple 'icmp_seq' lines from different IPs. Many modern OSes ignore broadcast ping by default. Use sudo tcpdump -i any 'icmp' to see the broadcast ICMP going out and back.

6. See the binary bits of an IP with Python — understand how the mask AND works

python3 -c "import ipaddress; n=ipaddress.ip_network('192.168.10.100/26', strict=False); print(n.network_address, n.broadcast_address, n.num_addresses)"

Observation: Output: network_address broadcast_address total_addresses. Try changing /26 to /27 and /25 — see how the network/broadcast change. strict=False allows any IP inside the subnet, not only the network address.

Review questions

Question 1. Which subnet does 192.168.1.130/25 belong to?

  1. 192.168.1.0/25
  2. 192.168.1.128/25
  3. 192.168.1.64/25
  4. 192.168.1.192/25

Answer: 192.168.1.128/25. /25 → block size 128. Subnets start at 0 and 128. .130 > 128 ⇒ subnet 192.168.1.128/25 (covering .128 → .255).

Question 2. How many usable hosts does a /27 have?

  1. 32
  2. 30
  3. 14
  4. 62

Answer: 30. 32 − 27 = 5 host bits → 2^5 = 32 addresses. Subtract network + broadcast = 30 usable.

Question 3. Which subnet mask is equivalent to /29?

  1. 255.255.255.224
  2. 255.255.255.248
  3. 255.255.255.240
  4. 255.255.255.252

Answer: 255.255.255.248. /29 = 29 one-bits = 11111111.11111111.11111111.11111000. The last octet 11111000 = 128+64+32+16+8 = 248.

Question 4. Are the two machines 10.0.0.5/28 and 10.0.0.17/28 in the same subnet?

  1. Yes
  2. No

Answer: No. The /28 block size = 16. .5 belongs to 10.0.0.0/28 (0-15), while .17 belongs to 10.0.0.16/28 (16-31). Different subnets → they must go through a router.

Question 5. Why can a /31, which has only 2 addresses, still be used?

  1. Because a /31 does not need network/broadcast addresses, only 2 endpoints for a point-to-point link
  2. /31 cannot be used in practice
  3. Because /31 allows more than 2 hosts
  4. Because the router creates extra addresses itself

Answer: Because a /31 does not need network/broadcast addresses, only 2 endpoints for a point-to-point link. RFC 3021 defines /31 for point-to-point links (router-to-router). With no need for network/broadcast → 2 addresses are enough for the 2 ends of the link. It saves public IPs.

Question 6. Is the IP 172.20.0.1 private?

  1. Yes
  2. No — because it is not in 192.168/16
  3. No — because 172.20 differs from 172.16
  4. Cannot be determined

Answer: Yes. The RFC 1918 private range for 172 is 172.16.0.0/12 = 172.16.0.0 → 172.31.255.255. 172.20 falls within it.

Question 7. You need a subnet that holds at least 100 hosts. What is the smallest suitable CIDR?

  1. /24
  2. /25
  3. /26
  4. /23

Answer: /25. /25 = 126 usable (128 − 2), enough for 100. /26 has only 62 < 100. /24 is excessive.

Question 8. In the default Docker bridge 172.17.0.0/16, what is the maximum number of container IPs?

  1. 255
  2. 65534
  3. 254
  4. 65536

Answer: 65534. /16 = 2^16 = 65536 addresses, minus network + broadcast = 65534 usable. (In practice Docker also keeps .1 for the bridge gateway.)

Flashcards