Skip to main content

Web servers and networking · Beginner · 16 min

Understand IP addresses, subnets and CIDR notation

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · ipcalc 0.51 · Python 3.14

You will work out network, broadcast and usable host addresses for any IPv4 prefix by hand, check your answers with ipcalc, and finish with a VPC subnet plan that a small Python script checks for overlaps.

On this page

Every time you open a cloud console, write a firewall rule or read a server's network settings, you meet strings such as 10.0.0.0/16 or 192.168.1.10/24. That is CIDR notation: an IPv4 address plus a number saying how much of the address names the network. Once you can read it, you can tell at a glance which machines share a subnet, how many servers fit in it, and exactly what a firewall rule lets in.

In this tutorial you work a handful of examples by hand, then check each one with ipcalc, a small calculator in Ubuntu's archive. You finish by planning the subnets for a cloud network (a VPC) and checking the plan with a short Python script, so a mistake shows up before you build anything. Everything runs locally; nothing touches your network settings.

Step 1 · Install ipcalc and see an address as 32 bits

Install ipcalc and Python 3. On Ubuntu 26.04 ipcalc comes from the universe component, which is enabled by default.

Bash
sudo apt-get update
sudo apt-get install -y ipcalc python3

An IPv4 address is a 32-bit number. The dotted form, 192.168.1.10, writes it as four 8-bit pieces (octets), each between 0 and 255. Convert one octet by hand with the place values 128 64 32 16 8 4 2 1:

  • 192 is 128 + 64, so 11000000
  • 168 is 128 + 32 + 8, so 10101000
  • 1 is 00000001
  • 10 is 8 + 2, so 00001010

Now ask ipcalc for the same address:

Bash
ipcalc 192.168.1.10/24

The Address: line shows the dotted form on the left and the 32 bits on the right, something like 11000000.10101000.00000001. 00001010. The gap ipcalc leaves in the bits is where the /24 cuts the address.

Check it worked

Compare the four groups of bits on the Address: line with your hand conversion. They should match digit for digit. If they do not, recheck the place values: each octet must add up to its decimal number.

Step 2 · Read the prefix, the mask and the three key addresses

The number after the slash is the prefix length: how many bits, counted from the left, belong to the network. In 192.168.1.10/24 the first 24 bits (192.168.1) name the network and the last 8 bits (10) name one host on it.

The address 192.168.1.10 drawn as four octets of bits. The first three octets, 24 bits, are marked as the network part and the last octet as the host part. Below, the mask 255.255.255.0 is ones under the network part and zeros under the host part. Three boxes show the network address 192.168.1.0, the broadcast address 192.168.1.255 and the 254 usable hosts from .1 to .254
A /24 prefix keeps 24 bits for the network and leaves 8 for hosts

From the prefix you get everything else:

  • Netmask: the prefix written as an address, ones for network bits and zeros for host bits. /24 is 255.255.255.0. Older tools and some config files still ask for this form.
  • Network address: keep the network bits, set every host bit to 0. Here 192.168.1.0. It names the subnet itself; you do not give it to a machine.
  • Broadcast address: set every host bit to 1. Here 192.168.1.255. A packet sent to it goes to every host on the subnet.
  • Usable hosts: everything in between, 192.168.1.1 to 192.168.1.254. With h host bits, a subnet holds 2^h addresses and 2^h − 2 hosts: 2^8 − 2 = 254.

Check all four with ipcalc. The -b option drops the binary columns so the answers are easier to read:

Bash
ipcalc -b 192.168.1.10/24

Look at the Netmask:, Network:, Broadcast: and Hosts/Net: lines. The HostMin: and HostMax: lines are the first and last usable hosts.

Check it worked

Pick out the lines with grep. Each command prints one line; if one prints nothing, that answer differs from the one worked above.

Bash
ipcalc -b 192.168.1.10/24 | grep -E 'Netmask: +255\.255\.255\.0'
ipcalc -b 192.168.1.10/24 | grep -E 'Network: +192\.168\.1\.0/24'
ipcalc -b 192.168.1.10/24 | grep -E 'Broadcast: +192\.168\.1\.255'
ipcalc -b 192.168.1.10/24 | grep -E 'Hosts/Net: +254'

Step 3 · Work a /16 and a /28 by hand

A /24 is easy because the cut falls between octets. Two more examples cover the other cases.

A /16: a cut on an octet boundary

Take 172.20.5.77/16. The first two octets are the network, the last two are hosts.

  • Network: keep 172.20, zero the rest, so 172.20.0.0.
  • Broadcast: 172.20.255.255.
  • Hosts: 16 host bits, so 2^16 − 2 = 65,534.

A /28: a cut inside an octet

Take 10.0.3.45/28. The first three octets are network bits; the cut is 4 bits into the last octet. A quick way to work it out without writing bits:

  1. The last octet has 4 host bits, so each subnet is 2^4 = 16 addresses wide. (The mask's last octet is 256 − 16 = 240, so the mask is 255.255.255.240.)
  2. Subnets start at multiples of 16 in that octet: 0, 16, 32, 48 and so on. 45 falls in the block that starts at 32.
  3. Network 10.0.3.32, broadcast 10.0.3.47 (the next block starts at 48), usable hosts 10.0.3.33 to 10.0.3.46, which is 14.

Write your answers down, then let the shell confirm the arithmetic:

Bash
echo "/16 hosts: $(( 2**16 - 2 ))"
echo "/28 block size: $(( 2**(32-28) )), hosts: $(( 2**(32-28) - 2 ))"
echo "45 falls in the block starting at $(( 45 / 16 * 16 ))"

It prints 65534, a block size of 16 with 14 hosts, and 32.

Check it worked

Run ipcalc on both and compare with your notes:

Bash
ipcalc -b 172.20.5.77/16 | grep -E 'Network: +172\.20\.0\.0/16'
ipcalc -b 172.20.5.77/16 | grep -E 'Broadcast: +172\.20\.255\.255'
ipcalc -b 10.0.3.45/28 | grep -E 'Netmask: +255\.255\.255\.240'
ipcalc -b 10.0.3.45/28 | grep -E 'Network: +10\.0\.3\.32/28'
ipcalc -b 10.0.3.45/28 | grep -E 'Broadcast: +10\.0\.3\.47'
ipcalc -b 10.0.3.45/28 | grep -E 'Hosts/Net: +14'

Six lines print. A shorter prefix means a bigger subnet: each step down (from /28 to /27) doubles the size.

Step 4 · Tell private, shared and special ranges apart

Some ranges never appear on the public internet. You use them inside your own networks, and cloud providers build VPCs from them.

  • 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16: the private ranges from RFC 1918. Home routers, office networks and VPCs use these.
  • 100.64.0.0/10: shared address space from RFC 6598, set aside for carrier-grade NAT, where an internet provider puts many customers behind a small pool of public addresses. Many mobile operators use carrier-grade NAT, which is one reason a phone on mobile data cannot accept incoming connections. Do not use this range for your own VPC.
  • 127.0.0.0/8: loopback, the machine talking to itself.
  • 169.254.0.0/16: link-local, the address a machine gives itself when it gets none from DHCP. Clouds also put their metadata service at 169.254.169.254.
  • 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24: reserved for documentation and examples (RFC 5737).

The /12 trips people up: 172.16.0.0/12 runs from 172.16.0.0 to 172.31.255.255, so 172.32.0.1 is outside it. Make a working folder and write a small classifier to test addresses against these ranges:

Bash
mkdir -p ~/subnet-demo
cd ~/subnet-demo
cat > classify.py <<'PY'
import ipaddress
import sys

RANGES = [
    ("10.0.0.0/8", "private (RFC 1918)"),
    ("172.16.0.0/12", "private (RFC 1918)"),
    ("192.168.0.0/16", "private (RFC 1918)"),
    ("100.64.0.0/10", "shared, carrier-grade NAT (RFC 6598)"),
    ("127.0.0.0/8", "loopback"),
    ("169.254.0.0/16", "link-local"),
    ("192.0.2.0/24", "documentation (RFC 5737)"),
    ("198.51.100.0/24", "documentation (RFC 5737)"),
    ("203.0.113.0/24", "documentation (RFC 5737)"),
]

for arg in sys.argv[1:]:
    ip = ipaddress.ip_address(arg)
    label = "not in any listed range, so treat as public"
    for cidr, name in RANGES:
        if ip in ipaddress.ip_network(cidr):
            label = f"{name}, inside {cidr}"
            break
    print(f"{arg:16} {label}")
PY
python3 classify.py 10.4.2.1 172.31.255.255 172.32.0.1 100.72.1.5 169.254.169.254 203.0.113.9

Check it worked

The output has one line per address. 172.31.255.255 is private, inside 172.16.0.0/12; 172.32.0.1 is "not in any listed range"; 100.72.1.5 is shared carrier-grade NAT space.

Bash
python3 classify.py 172.32.0.1 | grep 'not in any listed range'
python3 classify.py 100.72.1.5 | grep 'inside 100.64.0.0/10'

Step 5 · Account for the addresses a cloud keeps

A cloud provider keeps some addresses in every subnet for its own use. On AWS, the documented rule is that the first four addresses and the last address of each subnet cannot be assigned. In 10.20.0.0/24 those are .0 (network), .1 (the VPC router), .2 (DNS), .3 (reserved for future use) and .255 (broadcast). AWS subnets must be between /16 and /28.

So the usable count on AWS is 2^h − 5, not 2^h − 2:

Bash
for p in 28 24 20; do echo "/$p: $(( 2**(32-p) )) addresses, $(( 2**(32-p) - 5 )) usable on AWS"; done

A /28 gives 11 usable addresses, a /24 gives 251 and a /20 gives 4,091. That matters when you size a subnet for a cluster whose nodes and pods each take an address. Other clouds keep a different number; check your provider's subnet documentation before you size anything tightly.

Check it worked

The loop prints three lines. Confirm the /28 line by hand: 2^4 = 16 addresses, minus 5, is 11.

Bash
for p in 28; do echo "/$p: $(( 2**(32-p) - 5 )) usable"; done | grep '/28: 11 usable'

Step 6 · Split a /16 into a VPC subnet plan

A common layout gives a VPC one /16 and splits it into public, application and database subnets, one of each per availability zone, so losing one zone does not take everything down. Leave gaps: you cannot resize most cloud subnets later, and spare space is how you add a third zone or a new tier.

Here is a plan for 10.20.0.0/16 across two zones:

  • public-a 10.20.0.0/24 and public-b 10.20.1.0/24: load balancers and a bastion need few addresses.
  • app-a 10.20.16.0/20 and app-b 10.20.32.0/20: application servers or Kubernetes nodes need room to grow.
  • data-a 10.20.48.0/24 and data-b 10.20.49.0/24: databases and caches.
A bar showing the first 64 blocks of 256 addresses in 10.20.0.0/16 to scale. Two thin public subnets sit at 10.20.0 and 10.20.1, then spare space up to 10.20.16, then app-a from 10.20.16 and app-b from 10.20.32, each a /20, then two thin data subnets at 10.20.48 and 10.20.49 and more spare space. A table lists each subnet's CIDR, 256 or 4,096 addresses, and 251 or 4,091 usable on AWS, with 9,216 of 65,536 addresses allocated
The plan uses 9,216 of 65,536 addresses and leaves room to grow

Each subnet must start on a multiple of its own size. A /20 is 16 blocks of 256 wide, so it can start at 10.20.0.0, 10.20.16.0, 10.20.32.0 and so on, never at 10.20.8.0.

Write the plan to a file. The first line names the VPC; each line after it is a subnet name and its CIDR:

Bash
cd ~/subnet-demo
cat > plan.txt <<'EOF'
vpc      10.20.0.0/16
public-a 10.20.0.0/24
public-b 10.20.1.0/24
app-a    10.20.16.0/20
app-b    10.20.32.0/20
data-a   10.20.48.0/24
data-b   10.20.49.0/24
EOF

Now write a checker. For each subnet, Python's ipaddress module refuses a CIDR whose host bits are set (such as 10.20.16.5/20), and the script checks the subnet sits inside the VPC and overlaps no earlier subnet:

Bash
cd ~/subnet-demo
cat > check_plan.py <<'PY'
import ipaddress
import sys

RESERVED = 5  # AWS keeps the first four and the last address of every subnet

rows = [line.split() for line in open(sys.argv[1]) if line.strip()]
vpc = ipaddress.ip_network(rows[0][1])
seen = []
problems = 0

for name, cidr in rows[1:]:
    try:
        net = ipaddress.ip_network(cidr)
    except ValueError as err:
        print(f"BAD  {name}: {err}")
        problems += 1
        continue
    if not net.subnet_of(vpc):
        print(f"BAD  {name}: {net} is outside the VPC {vpc}")
        problems += 1
        continue
    clash = [other_name for other_name, other in seen if net.overlaps(other)]
    if clash:
        names = ", ".join(clash)
        print(f"BAD  {name}: {net} overlaps {names}")
        problems += 1
        continue
    seen.append((name, net))
    print(f"ok   {name:9} {str(net):15} {net.num_addresses:>5} addresses, {net.num_addresses - RESERVED:>5} usable")

used = sum(net.num_addresses for _, net in seen)
print(f"{used} of {vpc.num_addresses} addresses allocated, {problems} problems")
sys.exit(1 if problems else 0)
PY
python3 check_plan.py plan.txt

Check it worked

You see six ok lines and a summary of 9216 of 65536 addresses allocated, 0 problems. The script exits with status 0, so you can run it in a CI job before anyone applies the plan.

Bash
cd ~/subnet-demo
python3 check_plan.py plan.txt | grep '9216 of 65536 addresses allocated, 0 problems'

Step 7 · Catch the three common planning mistakes

Make a broken copy of the plan with three typical errors: a subnet outside the VPC, a CIDR with host bits set, and two subnets that overlap.

Bash
cd ~/subnet-demo
cat > plan-broken.txt <<'EOF'
vpc      10.20.0.0/16
public-a 10.20.0.0/24
public-c 10.21.0.0/24
app-a    10.20.16.0/20
app-b    10.20.24.0/21
data-a   10.20.48.5/24
EOF
python3 check_plan.py plan-broken.txt > broken-result.txt || echo "The checker found problems (exit status $?)"
cat broken-result.txt

Read each BAD line:

  • public-c is in 10.21, a different /16, so it is outside the VPC.
  • app-b at 10.20.24.0/21 covers 10.20.24.0 to 10.20.31.255, which is inside app-a (10.20.16.0 to 10.20.31.255).
  • data-a says 10.20.48.5/24. That names a host, not a network; the network is 10.20.48.0/24.

Check it worked

The summary says 3 problems, and each mistake has its own line:

Bash
cd ~/subnet-demo
grep -c '^BAD' broken-result.txt
grep 'public-c: 10.21.0.0/24 is outside the VPC' broken-result.txt
grep 'app-b: 10.20.24.0/21 overlaps app-a' broken-result.txt
grep 'data-a: 10.20.48.5/24 has host bits set' broken-result.txt

The first command prints 3. Fix plan-broken.txt until check_plan.py reports 0 problems; that is the habit to keep for any real plan.

Step 8 · Read CIDR in firewall rules

Firewalls, security groups and allow-lists all take CIDR. Three cases cover most rules you will read:

  • /32 is one address. 203.0.113.9/32 means only that machine.
  • 0.0.0.0/0 is every IPv4 address. A rule allowing SSH from 0.0.0.0/0 lets the whole internet try to log in.
  • Anything between is a range. With the plan above, a database rule allowing 10.20.16.0/20 admits every server in app-a and nothing else.

On an Ubuntu server with ufw, the rules for the database subnet might read like this. They are here to read, not to run on this machine:

Bash
sudo ufw allow from 10.20.16.0/20 to any port 5432 proto tcp
sudo ufw allow from 10.20.32.0/20 to any port 5432 proto tcp
sudo ufw deny 5432/tcp

ufw checks rules in order, so the two app subnets reach PostgreSQL on port 5432 and everyone else is refused. Setting up ufw itself is covered in the ufw tutorial.

Before you write a rule, test which addresses it would match:

Bash
cd ~/subnet-demo
cat > match.py <<'PY'
import ipaddress
import sys

rule = ipaddress.ip_network(sys.argv[1])
for arg in sys.argv[2:]:
    verdict = "matches" if ipaddress.ip_address(arg) in rule else "does not match"
    print(f"{arg:15} {verdict} {rule}")
PY
python3 match.py 10.20.16.0/20 10.20.20.7 10.20.31.200 10.20.40.7 10.20.48.10

Check it worked

10.20.20.7 and 10.20.31.200 match; 10.20.40.7 (in app-b) and 10.20.48.10 (in data-a) do not.

Bash
cd ~/subnet-demo
python3 match.py 10.20.16.0/20 10.20.31.200 | grep -E '10\.20\.31\.200 +matches'
python3 match.py 10.20.16.0/20 10.20.40.7 | grep 'does not match'

When you are finished, remove the practice folder with rm -r ~/subnet-demo.

Next steps

  • See the addresses, prefixes and routes your own server uses: /tutorials/see-addresses-and-routes-with-the-ip-command
  • Learn how IPv6 addresses and prefixes differ from IPv4: /tutorials/ipv6-basics-for-server-admins
  • Follow one request from the browser to your server, address by address: /tutorials/how-a-request-reaches-your-server
  • Put CIDR rules to work on a server: /tutorials/set-up-ufw-firewall-ubuntu-26-04

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.