Concepts / Addressing

Addressing

Subnet and prefix length

Also called CIDR, subnet mask, netmask, network address, /24, prefix

A block of addresses on one network, written as address/length: 10.0.1.0/24 holds 256 addresses.

Each bit of prefix halves the block
Each bit of prefix halves the block

A /24 holds 256 addresses, but the first (10.0.1.0) names the network and the last (10.0.1.255) is broadcast, so 254 are left for hosts. One more prefix bit halves the block: a /25 has 128, 126 usable; one less doubles it: a /23 has 512, 510 usable. Usable hosts are 2 to the power of the host bits, minus 2.

The prefix length counts the fixed leading bits: /24 fixes three bytes and leaves 8 bits (256 addresses, 254 usable: the first names the network, the last is broadcast). /25 halves it, /23 doubles it. Two devices talk directly only if each sees the other inside its own subnet; everything else goes to a gateway.

How subnetting works

An IPv4 address is 32 bits. The prefix length says how many of the leading bits name the network; the rest name the host on it. 10.0.1.10/24 means the first 24 bits, 10.0.1, are the network and the last 8, .10, are the host. The mask 255.255.255.0 is the same fact in another notation: 24 one-bits, then 8 zeros.

With h host bits a subnet holds 2^h addresses. Two are reserved: the first, all host bits 0, is the network address, and the last, all host bits 1, is the broadcast address. So a /24 has 256 addresses and 254 for devices. Each extra prefix bit halves the block:

Prefix Mask Addresses For devices
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/27 255.255.255.224 32 30
/28 255.255.255.240 16 14
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2

A /31 is the exception: RFC 3021 lets a point-to-point link between two routers use both of its addresses, with no network or broadcast address.

To find the network an address is in, keep the network bits and zero the host bits. For 10.0.1.200/26 the last byte keeps its top two bits: 200 is 11001000, so the network is 11000000, 192. The block size is 256 − 192 = 64, so the subnet runs from 10.0.1.192 to the broadcast 10.0.1.255. The same arithmetic, worked for any address, is what the subnet calculator does.

The prefix exists for one decision. Before sending, a machine checks whether the destination is inside its own subnet. If it is, the machine ARPs for it and sends directly. If not, the packet goes to the default gateway.

What it looks like on a Linux machine

In the subnetting lab, one school network becomes four /26 subnets. Giving schoolRouter an address in each one is what creates the route to each:

$ ip route
10.0.1.0/26 dev eth0 proto kernel scope link src 10.0.1.2
10.0.1.64/26 dev eth0 proto kernel scope link src 10.0.1.66
10.0.1.128/26 dev eth0 proto kernel scope link src 10.0.1.130
10.0.1.192/26 dev eth0 proto kernel scope link src 10.0.1.194

ipcalc does the arithmetic. For the exam server at .140:

$ ipcalc -n -b 10.0.1.140 255.255.255.192
BROADCAST=10.0.1.191
NETWORK=10.0.1.128

ip route get asks the kernel which route it would use: for an address on a connected subnet the answer has no via, for one beyond the router it names the gateway.

What goes wrong, and how you see it

A prefix too long. Give a machine /25 instead of /24. Its subnet shrinks to .0–.127, and a printer at .200 now looks remote. It sends to the router, which forwards the packet back onto the same wire and may send an ICMP redirect. It still works, one hop slower, and traceroute shows the extra hop. The printer replies directly, so the two directions take different paths. This is the quiet mistake that lasts for months.

A prefix too short. With /16, a server at 10.0.2.10 looks local. The machine ARPs for it, nobody on its wire answers, the neighbour entry goes FAILED and ping says Destination Host Unreachable from the machine itself. The router could have delivered it, but the machine never asked. The subnet masks lab sets this fault for you to fix.

Overlapping subnets. Two networks behind one router with overlapping prefixes, or a VPN that hands out a range already used at home, make some addresses reachable two ways. The longest prefix wins, which is rarely what anyone meant.

Questions people ask

How many hosts are in a /24, /27 or /30? 2^(32 − prefix) − 2: 254, 30 and 2.

What is CIDR? Classless Inter-Domain Routing (RFC 4632): writing the prefix with every address, so a network can be any size. Before it, the size was implied by the first octet: class A /8, B /16, C /24.

How do I split one network into smaller ones? Borrow host bits. A /24 gives two /25s, four /26s or eight /27s. Different sizes from one block, largest first, is VLSM, practised in the CIDR and VLSM lab.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab