Everyone Can Learn Program
中文 Start learning
Core · NetworksLesson 1 of 6Free to read

IP addresses and subnets · Computer Networks

How does one computer find another among billions?

Course contents

Why you need it

Every device on a network has an IP address, and every cloud server, firewall rule and Docker network is configured with addresses and ranges like 10.0.0.0/16. If you can read these, you can tell at a glance whether two machines can talk directly, and why a rule does or doesn't match.

The intuition

An IPv4 address is 32 bits, written as four numbers from 0 to 255: 192.168.1.20.

It splits into two parts, like a street and a house number:

  • the network part (the street): which network it's on
  • the host part (the house number): which device on that network

/24 says how many leading bits are the network: here the first 24 bits (192.168.1), leaving 8 bits, so 2⁸ = 256 addresses. Two devices in the same network talk directly; otherwise packets go through a router (the default gateway).

Some ranges are private (10.x, 172.16–31.x, 192.168.x): used inside homes, offices and clouds, never reachable directly from the internet.

Definition

CIDR prefix

The /n after an address: the first n bits name the network. /24 = 256 addresses, /16 = 65,536, /32 = exactly one address.

Default gateway

The router a device sends packets to when the destination is outside its own network.

Examples

Addresses are just numbers

Try it yourself

Python's ipaddress module does the bit work for you.

import ipaddress

net = ipaddress.ip_network("192.168.1.0/24")
print(net.num_addresses)
print(ipaddress.ip_address("192.168.1.20") in net)
print(ipaddress.ip_address("192.168.2.20") in net)
print(ipaddress.ip_address("10.0.0.5").is_private)
Step by step
  1. L432 − 24 = 8 host bits: 2⁸.256
  2. L5Same first 24 bits: same network.True
  3. L6Third number differs: another network, needs a router.False
  4. L710.x is a private range.True

Doing it by hand

Try it yourself

A /24 mask keeps the first three numbers. Compare them to see if two addresses share a network.

def to_bits(ip):
    return "".join(format(int(part), "08b") for part in ip.split("."))

print(to_bits("192.168.1.20"))
a, b = to_bits("192.168.1.20"), to_bits("192.168.1.99")
print(a[:24] == b[:24])
Step by step
  1. L2Each number becomes 8 binary digits.
  2. L432 bits: 192 is 11000000, 168 is 10101000, and so on.11000000101010000000000100010100
  3. L6Same first 24 bits: same /24 network.True

Common mistakes

0.0.0.0/0 in a firewall

/0 means zero network bits: every address on the internet. Allowing SSH from 0.0.0.0/0 opens it to the world.

Bigger number, smaller network

/28 is smaller than /24: more network bits leave fewer host bits (16 addresses versus 256).

Quick quiz

1. What does this print?

import ipaddress
print(ipaddress.ip_network("10.0.0.0/28").num_addresses)
  1. A
    16
  2. B
    28
  3. C
    256
Show the answer

Answer: A

32 − 28 = 4 host bits: 2⁴ = 16.

2. Your laptop is 192.168.1.20/24. Which address can it reach without a router?

  1. A192.168.1.200
  2. B192.168.10.2
  3. C8.8.8.8
Show the answer

Answer: A

Only 192.168.1.x shares the first 24 bits.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictRead the ranges

    Type exactly what this program prints.

  2. ModifyCompare networks, not strings

    same_network(a, b) should say whether two IPv4 addresses are in the same /24. It compares the first 10 characters, which is wrong for 10.0.0.1 and 10.0.0.200 and many others. Compare the first three numbers instead.

  3. WriteWhich rule matches?

    Write route(ip, table) where table is a list of (network, name) pairs like ("10.0.0.0/8", "office"). Return the name of the matching network with the longest prefix (the most specific one), or "internet" if none matches. Use the ipaddress module.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Terms in this lesson

TermChineseDefinition
IP addressIP 地址A number that identifies a device on a network, such as 192.168.1.20.
Subnet子网A range of addresses sharing the same network prefix, such as 10.0.1.0/24.
Router路由器A device that forwards packets between networks.