Skip to content
$ lynuxpro
← Linux

LESSON 13 OF 26 · INTERMEDIATE · 21 MIN

Networking fundamentals: IP, subnets and CIDR

In one minute

  • The basics of how computers find each other: IP addresses (a machine's address), subnets (a block of addresses), CIDR (a short way to write a block, like 10.0.1.0/24) and ports (which program on the machine).
  • Every DevOps task ends up as a network question. Why can't the app reach the database? Which subnet should this server go in? Why does the Kubernetes network clash with the office VPN?
  • Data is sent in small packets. Each packet carries the address of the machine it is going to (the IP) and the program it is for (the port). Routers pass packets along until they arrive.
  • 👉 Designing a cloud network, writing firewall or security group rules, and fixing "can't connect" problems.

Think of it like: posting a letter to a big apartment building. The IP is the building's street address. The port is the flat number. A subnet is one street, with all the buildings on it.

The layers, in practice

Networking is built in layers. Each layer does one job and relies on the layer below it.

When something breaks, knowing the layer tells you which tool to use and where to look.

👉 Troubleshooting. Start at the bottom and work up.

LayerWhat it handlesExamplesTools to check it
7 ApplicationThe actual conversationHTTP, DNS, SSH, gRPCcurl, dig
4 TransportPorts and reliable deliveryTCP, UDPss, nc
3 NetworkIP addresses and finding the way (routing)IPv4, IPv6, ICMPip route, ping, traceroute
2 Data linkDelivery on the local network by hardware (MAC) addressEthernet, ARPip neigh
1 PhysicalCables, Wi-Fi, virtual network cardsip link

Check from the bottom up: Is the network card up? Does it have an IP? Is there a route? Is the port open? Does the app answer?

Remember it: You do not need all seven OSI layers. Know 3 = IP, 4 = port, 7 = app.

IPv4 addresses

An IPv4 address is four numbers from 0 to 255, separated by dots. Under the hood it is 32 bits.

10.0.1.2500001010.00000000.00000001.00011001
OUTPUTterminalutf-8 · 2L

Each number is 8 bits, called an octet. That makes about 4.3 billion addresses in total. That is not enough for the whole world, which is why private ranges and NAT exist.

Private ranges

Three ranges are reserved for private networks. They are never used on the public internet.

👉 Every cloud network (VPC), home network and Kubernetes cluster uses them.

RangeCIDRSizeTypical use
10.0.0.0 – 10.255.255.25510.0.0.0/816.7 millionAWS VPCs, company networks
172.16.0.0 – 172.31.255.255172.16.0.0/121 millionDocker's default networks
192.168.0.0 – 192.168.255.255192.168.0.0/1665,536Home routers

Remember it: Private starts with 10, 172.16–31 or 192.168.

Other special addresses:

AddressMeaning
127.0.0.1Loopback: this machine itself (localhost)
0.0.0.0"Every network card", when a server listens on it
169.254.169.254The cloud's metadata service on AWS, Azure and GCP
100.64.0.0/10Used by some internet providers and some Kubernetes networks

Subnets and CIDR

A subnet is a block of addresses that sit together. CIDR writes it as address/number.

The number after the slash says how many bits are fixed (the network part). The bits left over are for machines (hosts).

10.0.1.0/24network part: first 24 bits (10.0.1)host part:    last 8 bits   (0 – 255)
OUTPUTterminalutf-8 · 3L

The formula: number of addresses = 2 to the power of (32 − the number).

CIDRNetmaskAddressesCommon use
/32255.255.255.2551One single machine in a firewall rule
/28255.255.255.24016Smallest AWS subnet
/24255.255.255.0256A typical subnet
/20255.255.240.04,096A large subnet (Kubernetes nodes and pods)
/16255.255.0.065,536A whole VPC
/8255.0.0.016.7 millionThe entire 10.x range
/00.0.0.0Everything"Anywhere" in security groups

Remember it: Smaller number = bigger network. The slash number is how much is locked. Lock less, get more. A /16 is 256 times bigger than a /24.

In every subnet, the first address names the network and the last is the broadcast address. So a /24 has 254 usable addresses. AWS keeps 5 for itself (the first four and the last one), leaving 251.

Worked example: is this IP in that subnet?

Is 10.0.1.200 inside 10.0.1.0/25?

  • /25 gives 2⁷ = 128 addresses: 10.0.1.0 to 10.0.1.127.
  • 200 is bigger than 127, so no. It belongs to the next block, 10.0.1.128/25.

You will do this in your head when reading security group rules and route tables.

Worked example: splitting a VPC

You get 10.20.0.0/16 for a new VPC. You need public and private subnets in three availability zones. Splitting it into /20 blocks gives 16 subnets of 4,096 addresses each:

SubnetCIDRRange
public-a10.20.0.0/2010.20.0.0 – 10.20.15.255
public-b10.20.16.0/2010.20.16.0 – 10.20.31.255
public-c10.20.32.0/2010.20.32.0 – 10.20.47.255
private-a10.20.48.0/2010.20.48.0 – 10.20.63.255
private-b10.20.64.0/2010.20.64.0 – 10.20.79.255
private-c10.20.80.0/2010.20.80.0 – 10.20.95.255

Notice that each block starts 16 higher in the third number (0, 16, 32...). That leaves room for ten more subnets later. You will write this exact layout in Terraform in the AWS course.

Do not let ranges overlap

Networks that need to connect (VPC peering, VPN, Transit Gateway) must not overlap. If two teams both pick 10.0.0.0/16, they can never be connected without renumbering everything. Keep one company-wide IP plan.

Seeing your addresses: ip addr

👉 "What is this machine's IP, and which network is it on?"

ip addrip -br addr
SHELLbashutf-8 · 2L
lo        UNKNOWN  127.0.0.1/8eth0      UP       10.0.1.25/24docker0   DOWN     172.17.0.1/16
OUTPUTterminalutf-8 · 3L

This machine is 10.0.1.25 in a /24, so its local network is 10.0.1.0 to 10.0.1.255.

Ports and protocols

The IP finds the machine. A port (a number from 0 to 65535) finds the program on that machine.

PortService
22SSH
53DNS (UDP and TCP)
80 / 443HTTP / HTTPS
3306 / 5432MySQL / PostgreSQL
6379Redis
6443Kubernetes API server
8080Common app port
9090 / 3000Prometheus / Grafana
30000–32767Kubernetes NodePort range

TCP or UDP? These are the two ways to send data:

TCPUDP
ConnectionSays hello first (a 3-step handshake: SYN, SYN-ACK, ACK)Just sends
DeliveryReliable, in order, re-sends lost packetsBest effort, no re-sending
Used byWebsites, SSH, databasesDNS lookups, metrics, video calls

Think of it like: TCP is a phone call (you connect first and confirm you heard). UDP is shouting across a room (fast, but nobody checks).

Ports below 1024 need root to listen on. That is why apps often listen on 8080, with a proxy on port 80 in front.

NAT in one paragraph

NAT (Network Address Translation) lets private machines reach the internet.

Private addresses cannot travel on the internet. So a NAT device swaps the private address for a public one on the way out, and swaps it back on the way in.

Where you meet it: In AWS, servers in private subnets reach the internet through a NAT gateway. Servers in public subnets use an internet gateway and their own public IP. Docker uses NAT too: containers on 172.17.0.0/16 reach out through the host's address.

IPv6 essentials

IPv6 addresses are 128 bits, written in hex: 2001:db8:12::25/64. There are so many that NAT is rarely needed. The loopback is ::1. Cloud providers and Kubernetes increasingly support IPv4 and IPv6 together ("dual-stack"). Learn to recognise the format, even if you mostly use IPv4.

Real-world scenario: the app cannot reach the database

The app on 10.20.48.15 times out when connecting to the database on 10.20.64.30:5432. Check from the bottom up:

ip -br addrip routeping -c 2 10.0.2.10ss -tn
SHELLbashutf-8 · 4L
  1. The network card is up, with the expected IP.
  2. Both addresses are inside the VPC's 10.20.0.0/16, so the VPC's local route covers them.
  3. ping may be blocked by security groups, so test the real port (next lesson, with nc or curl).
  4. The cause: the database security group allows 10.20.48.0/24, but the app subnet is 10.20.48.0/20. Servers above .48.255 are left out. Fixing the CIDR fixes the outage.

Common mistakes

  • Getting the size backwards, thinking /28 is bigger than /24.
  • Opening 0.0.0.0/0 (the whole internet) on a database port "just for now".
  • Overlapping ranges between VPCs, Docker networks and VPNs.
  • Forgetting reserved addresses and running out of IPs in small subnets. This happens a lot with EKS, where every pod takes a VPC IP.

Practice

  1. How many addresses are in a /26? A /22?
  2. Is 172.20.5.9 a private address? Which range?
  3. Is 10.0.1.130 inside 10.0.1.0/25?
  4. Split 10.50.0.0/16 into four equal subnets and write their CIDRs.
  5. Run ip -br addr and say which network the machine is on.

Cheat sheet

FactValue
Addresses in a /n2 to the power of (32 − n)
/24256 addresses, mask 255.255.255.0
/1665,536 addresses
Private ranges10/8, 172.16/12, 192.168/16
Loopback127.0.0.1 and ::1
Show IPsip -br addr

Key takeaways

  • An IP address finds the machine. A port finds the program on it.
  • A subnet is a block of addresses, written like 10.0.1.0/24.
  • The smaller the number after the slash, the bigger the network.
  • Private networks use 10.x, 172.16–31.x or 192.168.x.
  • TCP is careful and reliable. UDP is fast and does not check.