IP addressing, CIDR and subnetting

IPv4 structure and the private ranges, CIDR notation and netmasks, how to compute a subnet by hand, gateways and broadcast addresses, and a first look at IPv6.

Four bytes, one dotted string

An IPv4 address is 32 bits written as four decimal octets. The address alone says nothing about which part is the network and which is the host — that comes from the prefix length.

192.168.1.130/24

192       .168      .1        .130
11000000  .10101000  .00000001 .10000010

/24 means the first 24 bits are the network:
network  192.168.1.0
hosts    192.168.1.1 .. 192.168.1.254
broadcast 192.168.1.255
RangePurposeNotes
10.0.0.0/8Private (RFC 1918)16.7 million addresses
172.16.0.0/12Private172.16.0.0 to 172.31.255.255
192.168.0.0/16PrivateThe home and office default
127.0.0.0/8LoopbackAddresses the local host only
169.254.0.0/16Link-localSelf-assigned when DHCP fails
100.64.0.0/10Carrier-grade NATShared by an ISP between customers
224.0.0.0/4MulticastGroup delivery, not one-to-one
0.0.0.0/0Default routeEvery address; the fallback path

Computing a subnet by hand

import ipaddress

net = ipaddress.ip_network("192.168.1.130/26", strict=False)
print(net)                 # 192.168.1.128/26
print(net.netmask)         # 255.255.255.192
print(net.network_address) # 192.168.1.128
print(net.broadcast_address)  # 192.168.1.191
print(net.num_addresses)   # 64
print(list(net.hosts())[:3])  # usable hosts, excluding network and broadcast

# is an address inside the range?
ipaddress.ip_address("192.168.1.200") in net      # False

# split a network into equal subnets
for sub in net.subnets(new_prefix=27):
    print(sub, sub.num_addresses)                 # /27 gives 32 addresses each
  • A /26 has 64 addresses: 2 to the power of (32 minus 26). Two are reserved, leaving 62 usable.
  • The netmask is just the prefix written as a dotted quad; /26 equals 255.255.255.192.
  • Subnets must align: 192.168.1.130/26 is not a valid network address, so a tool that does not reject it is being lenient.
  • Every subnet needs a gateway, which is one of its usable host addresses — conventionally the first or the last.

IPv6 in one section

2001:0db8:0000:0000:0000:ff00:0042:8329     full form
2001:db8::ff00:42:8329                     compressed (one :: run per address)
2001:db8::1/64                             a typical LAN: /64 for the host part

::1                                        loopback
fe80::/10                                  link-local, auto-configured per interface
fc00::/7                                   unique local (the private equivalent)
  • 128 bits, written as eight groups of four hex digits; leading zeros may be dropped.
  • :: may appear once and stands for one or more groups of zeros.
  • There is no broadcast address, and a host may have several addresses at once.
  • A residential /64 is standard, which makes subnetting on the host part rarely necessary.
  • A dotted-quad tail appears when an address embeds IPv4: ::ffff:192.0.2.1.
💡
An address without a prefix length is not a network. In configuration and logs, always write the prefix — 10.0.0.5/8 and 10.0.0.5/32 mean very different routability, and a missing prefix is a frequent cause of security rules matching far more than intended.

FAQ

How many usable hosts in a /30?
Four addresses, two usable. A /30 is the classic point-to-point link prefix, and a /31 is now legal for that purpose with no broadcast address.
Why does 169.254.x.x appear?
The host tried DHCP and got no answer, so it self-assigned a link-local address. It can talk to neighbours on the same segment but has no route off it.

Ports, sockets and the client-server model Routing, NAT and gateways

Last refreshed 2026-09-18.