IP Addressing & Subnetting
IPv4 addresses, CIDR and subnet masks decide how networks are carved and routed. Learn the math for subnetting by hand and how private ranges, NAT and IPv6 fit together.
What is this?
An IPv4 address is a 32-bit number written as four octets (e.g. 192.168.1.20). CIDR notation (RFC 4632) pairs an address with a prefix length, such as 192.168.1.0/24, describing how many leading bits are the network part. The rest of the bits identify hosts inside that network.
Why do you need it?
- Subnetting is how you split a VPC or data-center block into app, db and public tiers.
- Every DHCP range, security group and routing table references a CIDR block.
- Understanding why 10.0.1.5 cannot talk directly to 10.0.2.5 saves hours of debugging.
- Private ranges (RFC 1918) plus NAT are why most internal infrastructure uses 10.x, 172.16-31.x or 192.168.x.
Mental model
A street address with a zip code
IP address + subnet mask
The zip code (network part) routes mail to the right town; the street number (host part) finds the house. The subnet mask tells you which digits are the zip and which are the street.
How it works
- A /N prefix means the first N bits of the address are the network identifier; the remaining 32-N bits address hosts.
- The subnet mask writes those N bits as ones, e.g. /24 = 255.255.255.0.
- The first address in a subnet is the network address and the last is the broadcast address; both are unusable for hosts.
- Usable hosts in a /N network = 2^(32-N) - 2.
- A router connects subnets: hosts route everything outside their own prefix to the gateway, which forwards between subnets.
Anatomy of the CIDR block 10.0.1.0/24.
Key commands
ip addr showShow your own IP, prefix and subnet mask.
ipcalc 192.168.1.0/24Print network, usable hosts and broadcast for a CIDR block.
ip routeSee which subnets are reachable directly and via which gateway.
ping 192.168.1.1Verify a specific host in your subnet responds.
hostname -IPrint all local IP addresses without the interface listing.
Examples
Break down a subnet with ipcalc
$ ipcalc 10.0.0.0/21Address: 10.0.0.0 00001010.00000000.00000000.00000000Netmask: 255.255.248.0 = 21 11111111.11111111.11111000.00000000Network: 10.0.0.0/21 00001010.00000000.00000000.00000000HostMin: 10.0.0.1 00001010.00000000.00000000.00000001HostMax: 10.0.7.254 00001010.00000000.00000111.11111110Broadcast: 10.0.7.255 00001010.00000000.00000111.11111111Hosts/Net: 2046 Class A, Private InternetA /21 keeps 11 host bits, giving 2^11 = 2048 addresses minus network and broadcast = 2046 usable. The range spans 10.0.0.0 through 10.0.7.255 — note the fourth octet and part of the third together hold the host bits.
In production
- Cloud VPCs are carved with CIDR blocks; each subnet maps to an availability zone.
- Route tables (main and subnet-specific) choose whether traffic crosses NAT gateways, VPC peers or VPNs.
- Cluster networking (e.g. Kubernetes Pod CIDRs) assigns a whole range per node and per service.
- Reserved ranges — the first four and last IP in every AWS VPC subnet — force careful planning of host counts.
Best practices
- Always reserve larger CIDR blocks in the cloud than you think you need; growing a VPC later is painful.
- Use RFC 1918 private ranges internally and NAT for anything that must reach the internet.
- Keep subnet boundaries aligned to /N boundaries so they stay contiguous and routable.
- Document which subnet hosts which tier (public, app, db, management).
Common mistakes
- Treating the network or broadcast address as usable host addresses.
- Overlapping CIDR blocks between VPCs that later need to be peered.
- Using 255.255.255.255 masks or host bits inside the network part when planning.
- Forgetting IPv6: even dual-stack VPCs still need IPv4 address planning for DNS and legacy clients.
Common issues
Can reach the gateway but not another host in the same subnetDifferent subnet masks, or the target host has a firewall.
Compare ip addr show on both; ensure both use the same prefix, e.g. /24.
Address already in use when setting a static IPDHCP gave the address to another device.
Use a reservation outside the DHCP pool or check the DHCP server's leases.
Packets route but connections never establishSubnets configured correctly but a stateful firewall drops the return path.
Verify symmetric routing and that conntrack/security groups allow return traffic.
Debugging tips
- ip addr show tells you the prefix — compute whether two hosts share it before blaming routing.
- If hosts in the same /24 cannot talk, check the subnet mask, not the gateway.
- Overlapping subnets cause asymmetric routes; compare ip route on both hosts.
- Use ipcalc on any borrowed CIDR to avoid planning mistakes.
Interview checkpoint
Hands-on lab
- Compute network, broadcast and usable host ranges from a CIDR block
- Verify calculations with ipcalc
- Split a /24 into two /25 subnets
- Read your own subnet from ip addr output
Subnet a Private Network by Hand
25 min · intermediate
Quick quiz
How many usable host addresses are in a /24 network?
Which of these is a valid private IPv4 address (RFC 1918)?
127.0.0.1 is reserved as the IPv4 loopback address.
Cheat sheet
/8 → 255.0.0.016,777,216 addresses (10.x private range)/16 → 255.255.0.065,536 addresses, 65,534 usable/21 → 255.255.248.02,048 addresses, 2,046 usable/24 → 255.255.255.0256 addresses, 254 usable/26 → 255.255.255.19264 addresses, 62 usable/28 → 255.255.255.24016 addresses, 14 usable10.0.0.0/810.0.0.0 – 10.255.255.255 (one huge block)172.16.0.0/12172.16.0.0 – 172.31.255.255192.168.0.0/16192.168.0.0 – 192.168.255.255