IP addresses and subnetting: calculating on Linux
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
10.20.0.15/24:/24— the first 24 bits of the address are the network, mask255.255.255.0.- Number of hosts = 2^(32 − prefix) − 2:
/24→ 254,/26→ 62,/30→ 2. - The network address (first) and broadcast (last) are not given to hosts.
- Private ranges:
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16— not routed on the internet. ipcalc 10.20.0.15/26shows the network, mask, first/last host and broadcast;ipcalc 10.20.0.0/24 -s 50 50suggests a split.
CIDR notation
An IPv4 address is 32 bits and has two parts: the network and the host. The mask says where they split. In CIDR notation the mask is written after a slash as a number of bits:
| CIDR | Mask | Addresses | Hosts | Where |
|---|---|---|---|---|
/8 | 255.0.0.0 | 16 777 216 | 16 777 214 | A large private network (10.0.0.0/8) |
/16 | 255.255.0.0 | 65 536 | 65 534 | Campus |
/24 | 255.255.255.0 | 256 | 254 | A typical office/server VLAN |
/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 | A small DMZ |
/30 | 255.255.255.252 | 4 | 2 | A link between two routers |
/32 | 255.255.255.255 | 1 | 1 | A single host (loopback, route) |
Rule: hosts = 2^(32 − prefix) − 2. Two addresses are subtracted: the network address (all host bits 0) and the broadcast (all host bits 1).
Calculating by hand: 10.20.0.15/26
/26— in the last octet 2 bits belong to the network and 6 to the host. Block size 2^6 = 64.- Blocks in the last octet: 0, 64, 128, 192.
- 15 is in the first block (0–63).
- Network:
10.20.0.0, broadcast:10.20.0.63, hosts:10.20.0.1–10.20.0.62.
Quick method: block size = 256 − the mask's last octet. /26 → 256 − 192 = 64.
Checking with ipcalc
ali@srv-web01:~$ sudo apt install -y ipcalc ali@srv-web01:~$ ipcalc -b 10.20.0.15/26 Address: 10.20.0.15 Netmask: 255.255.255.192 = 26 Wildcard: 0.0.0.63 => Network: 10.20.0.0/26 HostMin: 10.20.0.1 HostMax: 10.20.0.62 Broadcast: 10.20.0.63 Hosts/Net: 62 Class A, Private Internet
Wildcard is the inverse mask used in Cisco ACLs — a familiar number for network admins.
Splitting a network
The 10.20.0.0/24 network must be split into three parts: servers (50 hosts), staff (100 hosts), management (10 hosts). We allocate from largest to smallest:
ali@srv-web01:~$ ipcalc -b 10.20.0.0/24 -s 100 50 10 | grep -E "^(Network|HostMin|HostMax)" Network: 10.20.0.0/25 HostMin: 10.20.0.1 HostMax: 10.20.0.126 Network: 10.20.0.128/26 HostMin: 10.20.0.129 HostMax: 10.20.0.190 Network: 10.20.0.192/28 HostMin: 10.20.0.193 HostMax: 10.20.0.206
| Segment | Needed | Subnet | Hosts |
|---|---|---|---|
| Staff | 100 | 10.20.0.0/25 | .1 – .126 |
| Servers | 50 | 10.20.0.128/26 | .129 – .190 |
| Management | 10 | 10.20.0.192/28 | .193 – .206 |
This method is called VLSM: each segment gets a size that fits its needs, and no addresses are wasted.
Special ranges
| Range | What it is |
|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | Private (RFC 1918) — reach the internet through NAT |
127.0.0.0/8 | Loopback — the server itself |
169.254.0.0/16 | Link-local — an automatic address when DHCP does not answer |
100.64.0.0/10 | Provider NAT (CGNAT) |
0.0.0.0/0 | "All addresses" — the default route |
If you see a 169.254.x.x address on a server, it means the DHCP server did not answer.
Small checks on Linux
ali@srv-web01:~$ ip -4 -br a show ens18 ens18 UP 10.20.0.15/24 ali@srv-web01:~$ ip route get 10.20.0.20 10.20.0.20 dev ens18 src 10.20.0.15 uid 1000 cache ali@srv-web01:~$ ip route get 8.8.8.8 8.8.8.8 via 10.20.0.1 dev ens18 src 10.20.0.15 uid 1000 cache
ip route get says: 10.20.0.20 is on the same network — it goes directly; 8.8.8.8 is on another network — it goes via the gateway 10.20.0.1. If the mask is wrong (for example /32), even neighbouring servers are treated as "another network" — a typical configuration mistake.
In the Linux simulator, check which network addresses fall into with ip -br a and ip route get.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1How many host addresses does a /26 network have?
2Which network does 10.20.0.100/26 belong to?
3What is the /28 mask?
4You see the address 169.254.12.7 on a server. What does it mean?
5What does ip route get 10.20.0.20 show?
Comments
Sign in to comment.