Lesson 2 of 14 · 10 min · How networks work
IP addressing, subnets and CIDR
What you will learn
- How an IPv4 address and a subnet mask or CIDR prefix divide a network
- How to calculate network address, broadcast address, number of hosts and ranges
- Private address ranges, loopback, link-local and public addresses
- What a default gateway and NAT do
- How to split a block into subnets, which you will do in every cloud VPC
In simple words
An IP address is like a postal address for a computer. Part of it names the street (the network) and part names the house (the machine). A subnet is simply one street. This lesson shows how to read an address, work out how many houses fit on a street, and divide a big area into streets.
Words to know
- IP address: the number that identifies a machine on a network, such as
192.168.1.10. - Subnet: a smaller network cut out of a bigger one.
- CIDR: the short way to write a network's size, such as
/24(256 addresses). - Gateway: the router a machine sends traffic to when the destination is outside its own network.
- NAT: letting many private addresses share one public address, as a home Wi-Fi router does.
An IPv4 address
An IPv4 address is a 32-bit number, written as four 8-bit parts (octets) in decimal: 192.168.1.10. In binary: 11000000.10101000.00000001.00001010. Every address has two parts: a network part that says which network it is on, and a host part that identifies the machine inside it. How many bits belong to each is given by the subnet mask or, more compactly, the prefix length in CIDR notation:
192.168.1.10/24
/24 means the first 24 bits are the network part, so the mask is 255.255.255.0 (24 ones, then 8 zeros). The remaining 8 bits (32 − 24) number the hosts.
Calculations you should be able to do
Given a prefix /n, there are 32 − n host bits:
- Total addresses = 2^(32−n).
- The first address is the network address (all host bits 0) and the last is the broadcast address (all host bits 1). They are not given to hosts in traditional IPv4 subnets, so usable hosts = 2^(32−n) − 2. (Cloud providers reserve a few more; AWS keeps 5 addresses per subnet.)
Checked with Python's ipaddress module:
| CIDR | Mask | Total addresses | Usable hosts | First host | Last host | Broadcast |
|---|---|---|---|---|---|---|
192.168.1.0/24 |
255.255.255.0 | 256 | 254 | 192.168.1.1 | 192.168.1.254 | 192.168.1.255 |
192.168.10.0/26 |
255.255.255.192 | 64 | 62 | 192.168.10.1 | 192.168.10.62 | 192.168.10.63 |
10.0.5.77/20 (the address is inside 10.0.0.0/20) |
255.255.240.0 | 4096 | 4094 | 10.0.0.1 | 10.0.15.254 | 10.0.15.255 |
Handy sizes to remember: /32 is one address, /30 has 4 (2 usable, used on point-to-point links), /28 has 16, /24 has 256, /20 has 4096, /16 has 65,536, /8 has about 16.7 million. Each bit you add to the prefix halves the network.
Are two addresses in the same subnet? Apply the mask to both; if the network parts are equal, they are. 192.168.1.130 is inside 192.168.1.128/25 (range .128 to .255), but not in 192.168.1.0/25 (range .0 to .127). Two machines in the same subnet talk directly; for anything else, a machine sends the packet to its default gateway.
Tools
ipcalc 192.168.1.0/24 # package ipcalc or sipcalc
python3 -c "import ipaddress as i; n=i.ip_network('10.0.0.0/20'); print(n.netmask, n.num_addresses, n.broadcast_address)"
ip -br addr # your own addresses with their prefix
Special and private ranges
| Range | Purpose |
|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 |
Private addresses (RFC 1918). Not routed on the public internet; used inside homes, companies and cloud networks. 172.16.0.0/12 covers 172.16.0.0 to 172.31.255.255 only |
127.0.0.0/8 |
Loopback: the machine itself (127.0.0.1, localhost) |
169.254.0.0/16 |
Link-local: self-assigned when DHCP fails; clouds also use 169.254.169.254 for instance metadata |
100.64.0.0/10 |
Carrier-grade NAT space used by some ISPs; also used by tools such as Tailscale |
0.0.0.0 |
"any address" when binding a server; 0.0.0.0/0 means "all destinations", i.e. the default route |
| everything else | Public, globally unique, assigned by regional registries (in India, APNIC) |
Python confirms: 10.4.5.6 is private, 172.32.0.1 is not (just outside the /12), and 100.64.0.1 is not RFC 1918 private.
Default gateway and routing
A host's routing table tells it where to send packets (ip route):
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
The second line says: addresses in 172.17.0.0/16 are on the local link, send directly. The default line says: for everything else, hand the packet to the gateway 172.17.0.1. When several routes match, the most specific one (longest prefix) wins, and default (0.0.0.0/0) is the least specific.
NAT: many private hosts, one public address
Private addresses cannot be used on the internet, and IPv4 addresses are scarce. NAT (Network Address Translation) on your router, or a NAT gateway in the cloud, rewrites the source address of outgoing packets to a public address and remembers the mapping (including the source port) so replies can be sent back to the right private host. This is why:
- many devices at home share one public IP,
- servers in a private cloud subnet can download updates through a NAT gateway but cannot be reached from the internet,
- you cannot connect to a laptop behind NAT from outside unless a port is forwarded.
DHCP
Hosts normally do not have addresses typed in; a DHCP server hands out an address, mask, gateway and DNS server for a lease period. Servers usually get fixed addresses or reservations so that others can rely on them.
IPv6 in short
IPv6 uses 128-bit addresses written in hexadecimal, such as 2001:db8::1, which removes the shortage and the need for NAT. ::1 is loopback and fe80::/10 is link-local. Prefixes work the same way (/64 is a typical subnet). Many clouds support both, and ip addr shows both. Know that it exists and how to read an address; most day-to-day DevOps work still uses IPv4 inside networks.
Planning subnets in the cloud
A cloud network (AWS VPC, Azure VNet, Google VPC) is a private block that you divide into subnets, usually one or more per availability zone, and separate public and private tiers. Example plan with 10.0.0.0/16 (65,536 addresses). Splitting it into four /18 blocks gives 10.0.0.0/18, 10.0.64.0/18, 10.0.128.0/18 and 10.0.192.0/18. More typically you use /24 per subnet:
| Subnet | CIDR | Purpose |
|---|---|---|
| public-a | 10.0.1.0/24 |
load balancers, NAT gateway (zone a) |
| public-b | 10.0.2.0/24 |
same, zone b |
| private-a | 10.0.11.0/24 |
application servers |
| private-b | 10.0.12.0/24 |
application servers |
| data-a / data-b | 10.0.21.0/24, 10.0.22.0/24 |
databases |
Rules that save pain later:
- Subnets in one network must not overlap.
- Do not overlap with other networks you will connect (another VPC, an office network, a VPN). Overlapping ranges cannot be routed between. Choose distinct blocks per environment (
10.1.0.0/16for dev,10.2.0.0/16for prod). - Leave room to grow: it is painful to renumber a network. A
/16per VPC and/24per subnet is a common start. - Remember reserved addresses, so a
/28(16 addresses) in AWS gives only 11 usable.
Common mistakes
- Subtracting 2 everywhere. Clouds reserve more (AWS: 5),
/31and/32are special cases. - Overlapping CIDR blocks between networks that later need to be connected.
- Using
172.x.x.xassuming it is all private (only 172.16 to 172.31). - Confusing the mask direction:
/24is 255.255.255.0 and small prefix numbers mean bigger networks. - Forgetting that Docker's default bridge uses
172.17.0.0/16and can clash with company ranges.
Try it yourself
- For
10.20.30.40/22, find the network address, broadcast address, mask, and number of usable hosts. Check withipcalcor Python. - Is
192.168.5.200in192.168.5.128/26? Show the calculation. - Split
10.10.0.0/16into four equal subnets and list their ranges. - Run
ip routeon your machine and explain every line; find which interface and gateway reach the internet.
Check yourself
How many usable host addresses are there in a /26?
2^(32−26) = 64 addresses, minus the network and broadcast addresses = 62.
What does /24 mean, and what is its mask?
The first 24 bits are the network part. The mask is 255.255.255.0. Hosts vary in the last 8 bits, giving 256 addresses.
What are the three private IPv4 ranges?
10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
Why can a server in a private subnet reach the internet through a NAT gateway but not be reached from it?
NAT rewrites the source of outgoing connections and keeps a table for replies. There is no table entry for new inbound connections and the private address is not routable, so outsiders cannot start a connection to it.
Interview questions
Split 192.168.1.0/24 into four subnets. What are they?
Borrow 2 bits: /26 blocks of 64 addresses: 192.168.1.0/26, 192.168.1.64/26, 192.168.1.128/26 and 192.168.1.192/26.
What is the difference between a public and a private subnet in a cloud VPC?
A public subnet has a route to an internet gateway (and its instances can have public IPs); a private subnet does not, and reaches the internet, if at all, through a NAT gateway.
What does the route 0.0.0.0/0 via 10.0.0.1 mean?
The default route: any destination not matched by a more specific route is sent to the gateway 10.0.0.1.
Summary
- An IPv4 address is 32 bits split by the prefix into network and host parts;
/nleaves32 − nhost bits, so a network has 2^(32−n) addresses. - Private ranges are 10/8, 172.16/12 and 192.168/16; loopback is 127/8; link-local is 169.254/16.
- Hosts send non-local traffic to the default gateway; the most specific route wins; NAT lets private hosts share public addresses.
- Plan cloud subnets without overlaps and with room to grow.
Going deeper
- RFC 1918: Address Allocation for Private Internets: the private ranges.
- RFC 4632: Classless Inter-domain Routing (CIDR): CIDR notation and aggregation.
- AWS: VPC CIDR blocks and subnet sizing: sizing rules and the 5 reserved addresses.
- Python ipaddress module: do subnet maths in code.
See how much of this you can explain
A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.