3.1
IP Addresses, CIDR and Subnet Maths
Read and write any CIDR block, count its addresses, split it into subnets, and tell private ranges from public ones.
30 min Reading + animations
Tip: tap or hover any dotted-underlined term to see what it means.
By the end of this lesson you will be able to
- Read an IPv4 address as 32 bits and convert any octet between binary and decimal.
- Read any CIDR block (like
10.0.0.0/16), say how many addresses it holds, and write its first and last address. - Convert between a prefix length and a subnet mask (
/28=255.255.255.240). - Split a range into equal subnets and check that each one starts on the right boundary.
- Tell private (RFC 1918) addresses from public ones, and explain why the VPC range must never overlap a network it will talk to.
- Use
0.0.0.0/0and/32correctly in AWS route tables and firewall rules.
#Why you have to plan addresses first
When you create a VPC (Virtual Private Cloud, your own private network inside AWS), the first thing AWS asks for is an address range in CIDR notation, for example 10.0.0.0/16. Every server, database and load balancer you later put inside the VPC gets an address from that range.
That one choice is hard to undo:
- The VPC’s first (primary) range can never be changed after creation. You can add extra ranges later, but you cannot resize or renumber the first one.
- If the range overlaps your office network or another VPC, those networks can never talk to each other properly (you will see exactly why below).
- If the range is too small, you run out of addresses as the system grows.
So before you build anything on AWS, you need to be fluent in IPv4 addresses and CIDR. The System Design course covers IP addressing in general; this lesson focuses on what you need for AWS.
#IPv4 in one minute: 32 bits, 4 octets
An IPv4 address is a 32-bit number. To make it readable we split it into four groups of 8 bits (called octets) and write each group in decimal, separated by dots:
10 . 0 . 1 . 10
00001010 . 00000000 . 00000001 . 00001010
Each octet is 8 bits, so it ranges from 00000000 (0) to 11111111 (255). The bit positions in an octet are worth, from left to right:
| Bit | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
To turn binary into decimal, add the values of the bits that are 1: 11110000 = 128 + 64 + 32 + 16 = 240. To go the other way, take the largest value that fits and subtract: 192 = 128 + 64, so 11000000.
32 bits give 2³² = 4,294,967,296 possible addresses. After large blocks are set aside for private networks, multicast, documentation and other special uses, about 3.7 billion are usable on the public internet, which is far too few for every device on Earth. That shortage is why private ranges and NAT exist, and why AWS now charges for public IPv4 addresses (lesson 3.6).
#CIDR notation: a base address and a prefix
Originally the internet handed out addresses in fixed classes: a Class A network had 16.7 million addresses, Class B 65,536 and Class C 256. An organisation that needed 2,000 addresses had to take a whole Class B and waste over 63,000 of them. In 1993 classes were replaced by CIDR (Classless Inter-Domain Routing), which lets a block be any power-of-two size.
A CIDR block is written as base address / prefix length:
10.0.0.0/16
└──┬───┘ └┬┘
base prefix: the first 16 bits are fixed
- The prefix length (
/16) says how many bits, from the left, are fixed. These bits name the network. - The remaining 32 − prefix bits are free. Every combination of them is one address in the block.
So the number of addresses in a block is:
addresses = 2^(32 − prefix)
| CIDR | Free bits | Addresses | Which octets change |
|---|---|---|---|
/32 | 0 | 1 | none (exactly one address) |
/28 | 4 | 16 | part of the last octet |
/27 | 5 | 32 | part of the last octet |
/26 | 6 | 64 | part of the last octet |
/24 | 8 | 256 | the whole last octet |
/20 | 12 | 4,096 | part of the 3rd + all of the 4th |
/16 | 16 | 65,536 | the last two octets |
/8 | 24 | 16,777,216 | the last three octets |
/0 | 32 | 4,294,967,296 | all of them: every IPv4 address |
A few quick reads:
10.20.0.0/16→10.20.0.0to10.20.255.255(65,536 addresses).10.20.5.0/24→10.20.5.0to10.20.5.255(256).198.51.100.7/32→ just198.51.100.7.0.0.0.0/0→ every IPv4 address there is.
Play with the planner: type a block, drag the prefix and watch the blue (fixed) and grey (free) bits change. Ignore the subnet part for now; we use it in a moment.
VPC CIDR planner
Pick a block for a VPC, then cut it into subnets. Blue bits are fixed (the network), grey bits are free (the hosts).
10.0.0.0/16
First address
10.0.0.0
Last address
10.0.255.255
Addresses in the block
2^16 = 65,536
Subnet mask
255.255.0.0
Kind of range: Private (RFC 1918, 10/8): the usual choice for VPCs
Valid size for a VPC (AWS allows /16 to /28).
- 10.0.0.0/24 → 10.0.0.0 – 10.0.0.255
- 10.0.1.0/24 → 10.0.1.0 – 10.0.1.255
- 10.0.2.0/24 → 10.0.2.0 – 10.0.2.255
- 10.0.3.0/24 → 10.0.3.0 – 10.0.3.255
- 10.0.4.0/24 → 10.0.4.0 – 10.0.4.255
- 10.0.5.0/24 → 10.0.5.0 – 10.0.5.255
- … +250 more
Reserved in the first subnet
- 10.0.0.0 network address
- 10.0.0.1 VPC router
- 10.0.0.2 Amazon-provided DNS
- 10.0.0.3 reserved for future use
- 10.0.0.255 broadcast (not supported, so reserved)
Subnets must also be between /16 and /28.
#Subnet masks: the same thing written differently
Older tools and many operating systems express the prefix as a subnet mask: a 32-bit number whose network bits are all 1 and whose host bits are all 0.
| Prefix | Mask | Last-octet bits |
|---|---|---|
/8 | 255.0.0.0 | |
/16 | 255.255.0.0 | |
/20 | 255.255.240.0 | 3rd octet = 11110000 |
/24 | 255.255.255.0 | 00000000 |
/26 | 255.255.255.192 | 11000000 |
/27 | 255.255.255.224 | 11100000 |
/28 | 255.255.255.240 | 11110000 |
/32 | 255.255.255.255 | 11111111 |
To convert, count the 1s: 255.255.255.240 is 8 + 8 + 8 + 4 = 28 ones, so /28. Going the other way, /28 leaves 4 ones in the last octet: 11110000 = 240.
#Finding the range when the prefix is not a multiple of 8
For /8, /16 and /24 the answer is easy: whole octets are fixed or free. For anything in between, use the block size trick on the one octet that is split:
- Find the octet where the prefix ends. For
/20that is the 3rd octet (bits 17–24). - Block size in that octet = 256 − mask value. For
/20the 3rd-octet mask is 240, so the block size is 16. - Blocks start at multiples of 16 in that octet: 0, 16, 32, 48…
Example: which /20 contains 10.0.37.9? 37 lies between 32 and 47, so the block is 10.0.32.0/20, covering 10.0.32.0 to 10.0.47.255 (16 × 256 = 4,096 addresses).
#Splitting a range into subnets
A VPC range is always cut into smaller blocks called subnets (lesson 3.2 shows how AWS uses them). Splitting is just fixing more bits:
- Going from
/16to/24fixes 8 more bits, so you get 2⁸ = 256 subnets of 256 addresses each. - Going from
/16to/18fixes 2 more bits: 4 subnets of 16,384. - In general, splitting a
/ainto/bblocks gives 2^(b − a) subnets.
A typical plan for a VPC 10.0.0.0/16 used across two Availability Zones (separate data centres, explained in lesson 3.2):
| Subnet | CIDR | Range |
|---|---|---|
| public, zone A | 10.0.0.0/24 | 10.0.0.0 – 10.0.0.255 |
| public, zone B | 10.0.1.0/24 | 10.0.1.0 – 10.0.1.255 |
| private app, zone A | 10.0.10.0/24 | 10.0.10.0 – 10.0.10.255 |
| private app, zone B | 10.0.11.0/24 | 10.0.11.0 – 10.0.11.255 |
| database, zone A | 10.0.20.0/24 | 10.0.20.0 – 10.0.20.255 |
| database, zone B | 10.0.21.0/24 | 10.0.21.0 – 10.0.21.255 |
Most of the /16 is left free on purpose, for subnets you will need later.
In the planner, drag the green split slider. AWS keeps 5 addresses in every subnet for itself (the network address, the VPC router, DNS, one for future use and the last one), so a /24 subnet gives you 256 − 5 = 251 usable addresses. Lesson 3.2 explains each of the five. Try the “servers I need” box with 200: a /25 holds only 128 − 5 = 123, so the answer is a /24.
Self-checkQuestion
How many addresses are in 10.0.0.0/20, and what is the last one?
#Private and public addresses
Not every IPv4 address may be used on the internet. RFC 1918 sets aside three ranges for private networks:
| Range | CIDR | Size | Typical use |
|---|---|---|---|
| 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | 16.7 million | Large networks; the most common choice for VPCs |
| 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | 1 million | AWS’s default VPC uses 172.31.0.0/16 from here |
| 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | 65,536 | Home Wi-Fi routers and small offices |
Everything outside the special-purpose ranges is public.
| Public address | Private address | |
|---|---|---|
| Reachable from the internet | Yes, routers everywhere forward it | No, internet routers drop it |
| Unique | Globally: two machines can never share one | Only within one network: two companies can both use 10.0.1.5 |
| Who assigns it | Regional registries, through ISPs and cloud providers like AWS | You choose it freely |
| Can be located | Roughly geo-locatable | Means nothing outside its network |
| How it reaches the internet | Directly | Through NAT, which swaps it for a public address (lesson 3.4) |
A few other ranges are worth recognising on AWS:
169.254.0.0/16(link-local): AWS uses169.254.169.254for the instance metadata service and169.254.169.123for its time service. It can never be a VPC range.100.64.0.0/10(shared address space, also used for carrier-grade NAT): allowed in a VPC with some restrictions, sometimes used when RFC 1918 space runs out.127.0.0.0/8(loopback) and224.0.0.0/4(multicast): never usable for a VPC.192.0.2.0/24,198.51.100.0/24,203.0.113.0/24: reserved for documentation. This course uses them for made-up public addresses.
#The overlap trap
Two networks that need to talk (a VPC and your office, two VPCs, or a VPC and a partner’s network) must use ranges that do not overlap. The reason is routing: when a packet’s destination matches the VPC’s own range, the VPC delivers it inside the VPC, full stop.
Why two networks that talk must not share addresses
A VPC is connected to the office over a VPN. Watch where the VPC router sends a packet for 10.0.5.20.
Scenario
VPC 10.0.0.0/16 and the office also uses 10.0.0.0/16.
VPC route table
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 10.0.0.0/16 (office) | vgw ✕ (cannot be added) |
The app server wants the office database at 10.0.5.20. The packet first goes to the VPC router, which checks its route table.
What to take from the three scenarios:
- Same range on both sides: the office is unreachable. A route to it cannot even be added.
- Different ranges: routing works in both directions.
- Partial overlap: the most specific route wins (longest prefix match: a
/16beats a/8). Some office addresses work and some silently fail.
How teams avoid this in practice:
- Keep an address plan. Give each environment and Region its own block, for example
10.0.0.0/16for production in one Region,10.1.0.0/16for staging, and leave the office its existing range. - Avoid the common defaults.
192.168.0.0/24and192.168.1.0/24are on half the home routers in the world, which matters as soon as staff connect through a VPN.172.31.0.0/16is the default VPC everywhere. - Use a tool at scale. AWS VPC IPAM (IP Address Manager) hands out non-overlapping ranges to teams and accounts automatically.
#0.0.0.0/0 and /32 in AWS rules
Two CIDR blocks appear everywhere in AWS:
0.0.0.0/0means “any IPv4 address”. In a route table it is the default route: “for anything not matched by a more specific route, go here” (usually the internet gateway or a NAT gateway). In a firewall rule it means “from anywhere” or “to anywhere”./32means “exactly this one address”. You use it to allow just your own IP to SSH into a server, for example203.0.113.25/32, or to block one attacker’s address.
#Try it yourself
You do not need an AWS account to practise. Python’s standard library does all of this:
python3 - <<'EOF'
import ipaddress as ip
vpc = ip.ip_network("10.0.0.0/16")
print(vpc.num_addresses, vpc[0], vpc[-1]) # 65536 10.0.0.0 10.0.255.255
print(vpc.netmask) # 255.255.0.0
print(vpc.is_private) # True
# split into /24 subnets and show the first three
for s in list(vpc.subnets(new_prefix=24))[:3]:
print(s, "usable on AWS:", s.num_addresses - 5)
# which /20 holds 10.0.37.9?
print(ip.ip_network("10.0.37.9/20", strict=False)) # 10.0.32.0/20
# do two ranges overlap?
print(ip.ip_network("10.0.0.0/16").overlaps(ip.ip_network("10.0.0.0/8"))) # True
EOF
With an AWS account and the CLI configured, list the ranges your VPCs already use:
aws ec2 describe-vpcs --query "Vpcs[].{id:VpcId,cidr:CidrBlock,default:IsDefault}" --output table
#Summary
Key takeaways
- An IPv4 address is 32 bits, written as four octets of 0–255. Bit values in an octet are 128, 64, 32, 16, 8, 4, 2, 1.
- CIDR = base address / prefix length. The prefix bits are fixed; the rest are free. Addresses = 2^(32 − prefix). Bigger prefix, smaller block.
- A subnet mask is the same information:
/24=255.255.255.0,/28=255.255.255.240. For split octets, block size = 256 − mask value. - Splitting a
/ainto/bgives 2^(b − a) subnets, each starting on a multiple of its own size. AWS keeps 5 addresses per subnet, so usable = total − 5. - RFC 1918 private ranges:
10.0.0.0/8,172.16.0.0/12(default VPC172.31.0.0/16),192.168.0.0/16. Public addresses are globally unique; private ones reach the internet through NAT. - A VPC range must be
/16to/28, and its primary range can never be changed. Plan it so it never overlaps a network it will connect to; the longest prefix match makes overlaps fail silently. 0.0.0.0/0= anywhere (the default route);/32= one address. Never open admin ports to0.0.0.0/0.
Answer on your own first, then read the explanation, even when you got it right.
Self-checkQuestion
You need a subnet on AWS for 29 instances. What is the smallest subnet that works?
Self-checkQuestion
A VPC uses 10.0.0.0/16 and is connected by VPN to an office that uses 10.0.0.0/16. Servers in the VPC cannot reach office servers. Why?
Self-checkQuestion
Which of these is not an RFC 1918 private range?
Self-checkQuestion
Which decimal value is the octet 11100000?
Self-checkQuestion
How many IPv4 addresses does a /26 block contain?
Self-checkQuestion
A legacy firewall shows a network with the mask 255.255.255.192. What prefix length is that?
Self-checkQuestion
Which two statements about CIDR prefix length are true?
Choose 2
Self-checkQuestion
A training slide says “IPv4 allows about 3.7 billion addresses.” Which statement is accurate?
Self-checkQuestion
Before CIDR, an organisation that needed 2,000 addresses had to take a whole Class B network (65,536 addresses). With CIDR, what is the smallest single block that holds 2,000 addresses? (Ignore AWS’s reserved addresses here.)
Self-checkQuestion
An on-premises network engineer describes the office network as “10.50.0.0, mask 255.255.252.0”. Which CIDR block do you put in the VPC route table for it?
Self-checkQuestion
Which /21 block contains the address 172.20.77.10?
Self-checkQuestion
You split the VPC range 10.0.0.0/16 into equal /20 subnets. How many subnets do you get, and how big is each?
Self-checkQuestion
Which of these is a valid /26 subnet?
Self-checkQuestion
How many addresses can you assign to instances in a /28 subnet on AWS?
Self-checkQuestion
An office network uses 192.168.10.0/24. Which two of these ranges overlap it?
Choose 2
Self-checkQuestion
Which range does not overlap 10.1.0.0/16?
Self-checkQuestion
Which two CIDR blocks can be the primary IPv4 range of a new VPC?
Choose 2
Self-checkQuestion
A solutions architect is choosing a range for a new VPC. Which one will AWS refuse?
Self-checkQuestion
A team lead says: “A VPC can only use 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16.” What is the accurate position?
Self-checkQuestion
Which two statements about public and private IPv4 addresses are true?
Choose 2
Self-checkQuestion
A subnet uses 10.0.2.0/24. Which two of these addresses can you never assign to an instance?
Choose 2
Self-checkQuestion
A company needs a single subnet that can hold 500 EC2 instances. What is the smallest subnet that works?
Self-checkQuestion
An Auto Scaling group can run up to 60 instances in one subnet, and the subnet also needs about 10 addresses for other network interfaces such as load balancer nodes. What is the smallest subnet that fits?
Self-checkQuestion
A VPC uses 10.0.0.0/22. You need four equal subnets, two in each of two Availability Zones. Which set is correct?
Self-checkQuestion
An administrator must allow SSH to a server only from the administrator’s own workstation, whose public address is 203.0.113.25. What source should the firewall rule use?
Self-checkQuestion
A route table has an entry with destination 0.0.0.0/0 and an internet gateway as the target. What does this entry do?
Self-checkQuestion
A route table contains: 10.0.0.0/16 → local, 10.8.0.0/16 → a VPN gateway, 10.8.5.0/24 → a VPC peering connection, 0.0.0.0/0 → an internet gateway. Where does a packet to 10.8.5.20 go?
Self-checkQuestion
A VPC uses 10.0.0.0/16. The office, connected over VPN, is reached through a route for 10.0.0.0/8. VPC servers can reach the office host 10.7.0.9 but not the office host 10.0.200.15. What is the cause?
Self-checkQuestion
A solutions architect tries to peer VPC A (10.0.0.0/16) with VPC B (10.0.128.0/20) in the same Region. The peering connection cannot be created. What is the most likely reason?
Self-checkQuestion
A company’s data centre uses 10.0.0.0/16 and 10.1.0.0/16. Staff connect over VPN from home networks that commonly use 192.168.0.0/24 and 192.168.1.0/24. A new production VPC will be connected to the data centre over Direct Connect. Which VPC range is the best choice?
Self-checkQuestion
A VPC was created with 10.0.0.0/24 and has run out of addresses. The team wants to keep the VPC and the resources already in it. What should a solutions architect do?