IPv4: subnets and CIDR

  1. Quick summary
  2. Step 1 — Determine how many bits to add
  3. Step 2 — Calculate the size of each /26 subnet
  4. Step 3 — List the 4 subnets
  5. Step 4 — Verify in binary
  6. Step 5 — Subnet mask for /26
  7. Step 6 — Assign subnets to departments + gateway
  8. Step 7 — Verify with a tool
  9. Common misconceptions
  10. Try it at home
  11. Review questions
  12. Flashcards

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
  1. Look at the ARP table to find the duplicate

ip neigh show | sort
  1. 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

Right: Class C is an address range (first octet 192–223) per RFC 791 §3.2; prefix length is a separate CIDR concept per RFC 4632.

A Class C block (192.168.1.0) can be split into /25, /26, /27 using VLSM.

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.

Right: RFC 1918 §3 specifies that private ranges are not routable on the internet and not globally unique — any organization uses the same ranges independently.

IP overlap is a real problem when connecting 2 companies over a site-to-site VPN.

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.

Right: Usable = 2^(32-n) − 2 because you must subtract the network address (host bits all-0) and the broadcast address (host bits all-1).

On an AWS VPC, each subnet reserves 5 more addresses (network, router, DNS, future, broadcast) → actual usable = 2^(32-n) − 5 per the AWS VPC subnet sizing docs.

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.

Right: RFC 3022 defines NAT as replacing the source IP from private → public — external servers see only the NAT gateway's public IP and do not know which private IP is the origin.

Only the NAT gateway has the port-mapping table needed to look it up in reverse.

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.

Right: 127.0.0.0/8 is the loopback range per RFC 5735 §3 — reserved, not allocated to ordinary hosts or networks.

Usable Class A starts at 1.0.0.0; 0.0.0.0/8 is also reserved (RFC 1122 §3.2.1.3) and cannot be used.

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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. /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

  1. 65536

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

Flashcards

The first n bits of the IP are the network, the following 32−n bits are the host. *CIDR replaced the rigid old class A/B/C — allowing flexible network division anywhere.

This is how modern internet routing works. *

2^(32−n) − 2. Subtract 2 because the network address (host bits all 0) + the broadcast address (host bits all 1) cannot be assigned to real hosts.

*Remembering the formula helps you calculate quickly in your head when planning IPs. /24 → 254, /26 → 62, /28 → 14, /30 → 2. *

255.255.255.192 — because the last octet 11000000 = 192. *A trick for remembering the last octet of the mask: /25=128, /26=192, /27=224, /28=240, /29=248, /30=252.

Each additional CIDR bit adds 64, 32, 16, 8, 4 in turn. *

The size of 1 subnet = 2^(32−n). A subnet boundary is always a multiple of the block size.

*Block size helps you find the network address fast: /26 block = 64, .100 ∈ subnet .64 (the nearest multiple ≤ 100). *

Network = host bits all 0 (identifies the subnet). Broadcast = host bits all 1 (sends to every host in the subnet). *Assigning these IPs to a host = conflict.

Broadcast is used for ARP/DHCP — it is how hosts find each other when they do not yet know the MAC. *

The bit-inverse of the subnet mask. 255.255.255.0 ↔ 0.0.0.255. Used in Cisco ACLs and OSPF.

*Cisco devices are designed to match 'which bits are allowed to differ' → this fits a wildcard better than a subnet mask. *

Variable Length Subnet Masking — splitting 1 block into several subnets of different sizes (e.g. /24 → 1 ×/25 + 2 ×/26).

*Departments have different needs: sales 100 hosts (/25), IT 50 hosts (/26), guest 20 hosts (/27). Splitting evenly wastes IPs. *

10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. Private = not routed on the internet, needs NAT to get out. Kubernetes/Docker/AWS VPC commonly use these ranges.

RFC 3021 — for point-to-point links between 2 routers. No network/broadcast needed. *It saves public IPs for ISPs.

A router-to-router link used to need a /30 (4 IPs, 2 usable); now it is just a /31 (2 IPs, both usable). *

/26 block size is 64. 130 ÷ 64 = 2 remainder 2 → network = 128 (2×64). Subnet: 192.168.5.128/26.

*No paper needed: every CIDR has its own block size; dividing and taking the integer part times the block size gives the boundary. *

Mask: a string of 1 bits (network) + 0 bits (host). Wildcard: the opposite. /24 = mask 255.255.255.0 / wildcard 0.0.0.255.

*Both describe the network/host boundary — they differ only in representation. Linux/Juniper use masks; Cisco ACLs/OSPF use wildcards. *

Because CIDR determines the boundary. 10.0.0.5 and 10.0.0.17 share a subnet under /24, but not under /28 (a block of 16 cuts between .15 and .16).

*When the CIDR is configured wrong on 2 machines, they 'think' they are in different subnets → they send via the gateway instead of ARPing directly → they cannot reach each other even on the same physical LAN.

/16 = 65536 (usable 65534). /24 = 256 (usable 254). Each time the CIDR drops by 1 → the address count doubles. /25 is twice /26. Use this to estimate quickly when planning a VPC/cloud subnet.

Convention: the first, .1 (e.g. 192.168.1.1). But it is a convention, not a requirement — it could be .254. *Easy for devs/admins to remember. Many routers/firewalls default to .1.

When troubleshooting 'cannot reach the internet', always check the gateway first. *

IPv4: subnets and CIDR