DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

NetworkingLesson 2 of 12
Your progress · 0%
On this page
  1. An address and a prefix
  2. Plan subnets
  3. Private, public and reserved
  4. IPv6 in brief
↖ Course roadmap
Lesson 2 · IP addressing and subnets

IP addressing and subnets: reading CIDR

Read any CIDR block at a glance, plan a VPC into subnets that will not collide later, and know which addresses are private, public or reserved, with IPv6 in brief.

25 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 4 complete
Lab status

Verified lab: every number below comes from Python's ipaddress module and ip on a Linux host. The output shown is real.
Cloud lab: none needed.

You'll write CIDR blocks for the rest of your career: VPCs, subnets, security group rules, Kubernetes Pod ranges, Docker networks. Reading them should be instant.

An address and a prefix

An IPv4 address is 32 bits, written as four numbers from 0 to 255. The /number after it says how many of those bits, from the left, name the network. The rest number the hosts inside it.

$ python3 -c "import ipaddress as i; n=i.ip_network('10.0.0.0/16'); print(n, n.num_addresses, n.netmask, n[0], n[-1])"
10.0.0.0/16 65536 255.255.0.0 10.0.0.0 10.0.255.255

Your machine from lesson 1, eth0 172.29.94.205/20, sits in the network 172.29.80.0/20: 4,096 addresses from 172.29.80.0 to 172.29.95.255. Anything outside that range goes to the gateway. Note that the first address (the network itself) and the last (broadcast) aren't usable for hosts, and AWS reserves five per subnet.

Plan subnets

A real network is one big block split into smaller ones. The common pattern: one /16 per VPC, one /24 per subnet, split by availability zone and by public or private.

$ python3 -c "import ipaddress as i; s=list(i.ip_network('10.0.0.0/16').subnets(new_prefix=24)); print(len(s), s[0], s[1], s[-1])"
256 10.0.0.0/24 10.0.1.0/24 10.0.255.0/24
$ python3 -c "import ipaddress as i; print(i.ip_address('10.0.3.7') in i.ip_network('10.0.0.0/22'), i.ip_address('10.0.4.7') in i.ip_network('10.0.0.0/22'))"
True False

A /16 holds 256 /24 subnets. A /22 is four /24s glued together (10.0.0.0 to 10.0.3.255), so 10.0.3.7 is inside and 10.0.4.7 isn't. You don't need to do binary in your head: ipaddress or cidr.xyz will check any plan.

Plan before you build

CIDR ranges are painful to change later, and networks you ever want to connect (VPC peering, a VPN to the office, Kubernetes Pods reaching a database) must not overlap. Give each environment its own range, such as 10.10.0.0/16 for dev and 10.20.0.0/16 for prod.

Private, public and reserved

Three ranges are set aside for private networks and are never routed on the internet. Everything else is public.

$ python3 -c "import ipaddress as i; [print(a, i.ip_address(a).is_private) for a in ('10.1.2.3','172.20.0.5','192.168.1.10','8.8.8.8','100.64.0.1')]"
10.1.2.3 True
172.20.0.5 True
192.168.1.10 True
8.8.8.8 False
100.64.0.1 False

Your laptop, your Docker containers and your VPC servers all use private addresses. When they reach the internet, a router swaps the private source address for a public one (NAT, lesson 7). Ask curl -4 https://ifconfig.me and you'll see the public address the world sees, not your 172.x or 192.168.x one. 169.254.169.254 is one link-local address you'll meet often: it's where cloud servers fetch their metadata and credentials.

IPv6 in brief

IPv4 has about 4.3 billion addresses, which ran out years ago; NAT is how the internet copes. IPv6 uses 128 bits, written in hexadecimal groups, with :: standing for a run of zeros. Your machine already has one:

$ ip -6 addr show eth0 | grep inet6
    inet6 fe80::215:5dff:fedd:4d64/64 scope link proto kernel_ll
ip -6 addr show eth0 | grep inet6
  • |pipe: send output to the next command
  • grepfind lines that match a pattern

fe80::/10 is link-local: every interface makes one itself, and it only works on the local link. A global IPv6 address (starting with 2 or 3) is routable everywhere with no NAT. AWS, Kubernetes and most CDNs support IPv6, and AWS now charges for every public IPv4 address, which is one reason teams are adopting it. The CIDR rules are the same: a VPC gets a /56, a subnet a /64.

⌘
Mini mission

Plan a two-environment network

Pick non-overlapping /16 ranges for dev and prod. Split each into two public and two private /24 subnets across two zones, leaving gaps for growth. Prove with Python that none of your eight subnets overlap and that each sits inside its VPC.

Input: Python 3Output: ~/subnet-plan.txt
Reveal one safe solution
$ python3 - <<'EOF' > ~/subnet-plan.txt
import ipaddress as i, itertools
plan = {'dev': '10.10.0.0/16', 'prod': '10.20.0.0/16'}
subnets = {f'{env}-{tier}-{z}': f'{plan[env][:-6]}{n}.0/24'
           for env in plan for (tier, base) in (('public', 1), ('private', 11)) for z, n in (('a', base), ('b', base + 1))}
for name, cidr in subnets.items():
    print(name, cidr, i.ip_network(cidr).subnet_of(i.ip_network(plan[name.split('-')[0]])))
print('overlaps:', [p for p in itertools.combinations(subnets.values(), 2) if i.ip_network(p[0]).overlaps(i.ip_network(p[1]))])
EOF
$ cat ~/subnet-plan.txt
python3 - <<'EOF' > ~/subnet-plan.txt
  • -the previous directory
  • >write output to a file, replacing it
cat ~/subnet-plan.txt
  • catprint a file

Every line should end in True, and the overlaps list should be empty.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

How many addresses does a /24 contain?

Go deeper

Official reading for this lesson

  • DocsPython ipaddress modulePython Docs ↗
  • DocsAddress allocation for private internets (RFC 1918)IETF ↗
  • DocsVPC CIDR blocksAWS Docs ↗
  • Toolcidr.xyz, an interactive CIDR visualisercidr.xyz ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousHow networks work: layers, packets and the next hopNext lesson →TCP, UDP and ports: refused is not timed out