basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    IPv4, masks, and CIDR: 32 bits, subnets, and private ranges

    An IPv4 address is 32 bits split by a subnet mask into a network ID and a host ID. Learn /24 vs. 255.255.255.0, network and broadcast addresses, mental subnet math, /30, /31, and /32, plus RFC 1918 private blocks, CGNAT 100.64.0.0/10, and 127.0.0.0/8.

    New hereIPv4, netmasks and CIDR

    What you’ll get

    Inspect an interface’s IPv4 address, prefix length, broadcast address, and the subnets the kernel adds to the routing table automatically:

    ip -4 addr show dev eth0
    # 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    #     inet 10.0.0.11/24 brd 10.0.0.255 scope global eth0
    
    ip -4 route
    # default via 10.0.0.1 dev eth0 proto static
    # 10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.11
    # 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1

    A single inet line holds everything the kernel needs to know the boundaries of the local network:

    ip -4 addr show dev eth0 · Pick a segment
    Without /24, 10.0.0.11 is incomplete: the mask tells the kernel which addresses to resolve locally via ARP and which to send to the gateway.

    You can compute any subnet’s boundaries directly on a server without extra packages using Python’s built-in ipaddress module:

    python3 -c 'import ipaddress; n=ipaddress.ip_network("10.0.10.42/26", strict=False); print(n, n.netmask, n.broadcast_address, n.num_addresses)'
    # 10.0.10.0/26 255.255.255.192 10.0.10.63 64

    32 bits and the mask: why /24 and 255.255.255.0 mean the same thing

    To the kernel and routers, an IPv4 address is not four separate numbers — it is one continuous string of 32 bits. The dots between four 8-bit octets exist only for human readability.

    To decide whether 10.0.0.20 sits on the same LAN (reachable via ARP from MAC, switch, ARP, and VLAN) or needs a router, every address comes paired with a subnet mask. That mask is also 32 bits: contiguous ones on the left (the network part) and zeros on the right (the host part).

    • Dotted-decimal notation writes four octets: 255.255.255.0 is 24 ones followed by 8 zeros.
    • CIDR notation (RFC 4632) writes the count of ones after a slash: /24.

    The kernel applies a bitwise AND between the IP and the mask — every bit under a zero becomes zero, yielding the network address. If your network address matches the destination’s, the peer is local.

    Enter any IPv4/prefix below or pick a preset to watch the network boundary slide across octet bits and update the mask, network ID, broadcast address, and usable host count:

    32 address bits · blue — network part, grey — host partMask: 255.255.255.192 (/26)
    192octet 1
    11000000
    168octet 2
    10101000
    10octet 3
    00001010
    42octet 4
    00101010
    Network address192.168.10.0/26
    Hosts192.168.10.1 – 192.168.10.62
    Broadcast192.168.10.63
    Addresses / hosts64 / 62

    In 192.168.10.42/26, the boundary lands inside the fourth octet after its second bit: three full octets (24 bits) plus the top two bits of the fourth (128 + 64 = 192, giving mask 255.255.255.192). In 42 (00101010), the top two bits are 00, so the subnet starts at .0, ends with broadcast .63 (00111111), and leaves .1–.62 for usable hosts.

    Calculating subnets in your head: block size, /31, and /32

    You do not need to memorize dozens of masks — three rules cover everything:

    • Total addresses in a subnet — 2^(32 − prefix). For /24 that is 2^8 = 256, for /26 it is 2^6 = 64, and for /30 it is 2^2 = 4.
    • Usable host addresses — two fewer: the first address is the network ID (host bits all 0), and the last is broadcast (host bits all 1).
    • Block size in the boundary octet equals 256 − mask octet. Subnets always start on multiples of that step. For /26 (255.255.255.192), the step is 256 − 192 = 64, splitting the fourth octet into four clean blocks: 0–63, 64–127, 128–191, and 192–255.

    The prefixes you run into most often:

    Prefix Mask Total addresses Usable hosts Common use
    /8 255.0.0.0 16,777,216 16,777,214 10.0.0.0/8 private range, 127.0.0.0/8 loopback
    /16 255.255.0.0 65,536 65,534 Default Docker bridge (172.17.0.0/16), 192.168.0.0/16
    /20 255.255.240.0 4,096 4,094 Docker pools after the 15th network (192.168.0.0/20 = sixteen /24s)
    /24 255.255.255.0 256 254 Standard LAN, VLAN, office, home router
    /25 255.255.255.128 128 126 Half a /24: 0–127 and 128–255
    /26 255.255.255.192 64 62 Quarter of a /24: step 64 (0, 64, 128, 192)
    /28 255.255.255.240 16 14 Small hosted public block, DMZ
    /30 255.255.255.252 4 2 Classic router-to-router link (2 spent on network + broadcast)
    /31 255.255.255.254 2 2 Point-to-point link without broadcast (RFC 3021)
    /32 255.255.255.255 1 1 Single host: firewall rules, lo, WireGuard AllowedIPs

    Three edge prefixes work differently from the rest:

    • /31 (RFC 3021). A direct cable between two routers needs no broadcast address: both .0 and .1 serve as normal host addresses. Linux supports /31 out of the box — ip -4 addr omits brd, and 10.0.99.0/31 pings 10.0.99.1/31 normally.
    • /32. All 32 bits are fixed, narrowing the range to a single IP. Use it to match one server in nftables (203.0.113.10/32), a single peer in WireGuard AllowedIPs, or an extra loopback address on lo.
    • /0 (0.0.0.0/0). Zero fixed bits — matches every IPv4 address. Routing tables use it for the default route (default).
    Are these two addresses in the same subnet?
    Host A: 10.0.10.62/26
    Host B: 10.0.10.65/26
    Are these two addresses in the same subnet?
    Answer:
    10.0.10.62/26: subnet 10.0.10.0/26 (broadcast .63) · 10.0.10.65/26: subnet 10.0.10.64/26 (broadcast .127)

    A /26 mask has a block size of 64: the first subnet spans 10.0.10.0–10.0.10.63 (.62 is the last usable host, .63 is broadcast), and the second spans 10.0.10.64–10.0.10.127 (.64 is the network ID, .65 is the first host). Without a router, these hosts cannot talk.

    What breaks when masks don’t match

    A wrong mask on one server often wastes hours because networking breaks halfway. If Host A has 10.0.10.10/24 and Host B on the same segment has 10.0.10.200/25, their routing tables disagree:

    # Host A (10.0.10.10/24) treats all of 10.0.10.0/24 as local:
    ip route get 10.0.10.200
    # 10.0.10.200 dev eth0 src 10.0.10.10
    
    # Host B (10.0.10.200/25) only treats 10.0.10.128–10.0.10.255 as local:
    ip route get 10.0.10.10
    # RTNETLINK answers: Network is unreachable

    Host A sends ARP who-has 10.0.10.200, while Host B sees 10.0.10.10 as outside its /25: without a gateway it cannot reply at all, and with one it tries to route replies through the gateway. On Host A, ping 10.0.10.200 suffers 100% loss and ip neigh shows 10.0.10.200 INCOMPLETE, even though hosts in 10.0.10.130–10.0.10.254 reach Host B with no trouble.

    Private and reserved IPv4 ranges

    Not every IPv4 address routes on the public internet. Standards reserve specific blocks for private networks, carrier NAT, and special roles:

    Range Address bounds Standard Purpose
    10.0.0.0/8 10.0.0.0 – 10.255.255.255 RFC 1918 Private networks: offices, clouds, VPNs, labs
    172.16.0.0/12 172.16.0.0 – 172.31.255.255 RFC 1918 Private networks; Docker draws bridges 172.17.0.0/16–172.31.0.0/16 here
    192.168.0.0/16 192.168.0.0 – 192.168.255.255 RFC 1918 Home and small-office LANs (192.168.1.0/24, 192.168.88.0/24)
    100.64.0.0/10 100.64.0.0 – 100.127.255.255 RFC 6598 ISP CGNAT (and overlay VPNs like Tailscale): no direct public IPv4 behind it
    127.0.0.0/8 127.0.0.0 – 127.255.255.255 RFC 1122 Loopback: the entire /8 stays inside the host (127.0.0.1, 127.0.0.53 for systemd-resolved)
    169.254.0.0/16 169.254.0.0 – 169.254.255.255 RFC 3927 Link-local (APIPA): assigned when DHCP fails; 169.254.169.254 serves cloud VM metadata
    224.0.0.0/4 224.0.0.0 – 239.255.255.255 RFC 5771 Multicast: group delivery (mDNS 224.0.0.251, OSPF 224.0.0.5)
    192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 Three /24 blocks RFC 5737 Documentation addresses for configs and examples

    A common trap in 172.16.0.0/12: only second octets from 16 through 31 are private (the top 12 bits 10101100.0001). Both 172.32.0.1 and 172.15.0.1 are real public internet addresses. Another frequent surprise is loopback: 127.0.0.1 is not the only local address — Linux accepts the whole 127.0.0.0/8 block on lo, which is why systemd-resolved listens on 127.0.0.53.

    Which address is public on the internet rather than private?
    RFC 1918 private block: 172.16.0.0/12
    Which address is public on the internet rather than private?
    Answer:
    172.32.1.5 — public IPv4 (outside 172.16.0.0/12)

    The 172.16.0.0/12 block covers exactly sixteen /16 networks: 172.16.0.0 through 172.31.255.255. Once the second octet reaches 32, the address leaves RFC 1918 and belongs to the public internet.

    If it doesn’t work

    Two hosts on the same segment cannot talk: one pings lower addresses fine, yet addresses above .127 stick in INCOMPLETE

    Cause
    The hosts use mismatched masks — such as /24 on one and /25 on the other. The /24 host treats its peer as local and sends ARP, while the /25 host (10.0.10.128–10.0.10.255) sees 10.0.10.10 as outside its subnet.
    Fix
    Compare prefixes on both hosts with ip -4 -br addr and check routing in both directions using ip route get <address>.

    Creating a new Docker network cuts off access to the server from your office or home LAN

    Cause
    The container subnet overlapped the client’s range (for example Docker allocated 192.168.0.0/20, swallowing 192.168.10.0/24). The kernel decided the client lives on bridge br-… and sent replies there.
    Fix
    Inspect routes with ip -4 route, configure custom default-address-pools in /etc/docker/daemon.json, and recreate the conflicting network (Docker subnet clashes with home network).

    The router’s WAN port has an address in 100.64.0.0/10 (or 10.0.0.0/8), and inbound port forwarding does not work

    Cause
    Your ISP puts the line behind CGNAT (RFC 6598): the router has no public IPv4 and shares an external address with other subscribers.
    Fix
    Compare your WAN address against what an external check returns (curl -4 ifconfig.me). Direct inbound access requires a public IPv4 from your ISP, IPv6, or a tunnel (WireGuard, Cloudflare Tunnel).

    An interface shows 169.254.x.x/16 and has no network access

    Cause
    No DHCP server answered, so the OS assigned a link-local address (APIPA, RFC 3927). Routers never forward 169.254.0.0/16 traffic.
    Fix
    Check the cable, switch port VLAN, and DHCP server; for static setups, configure the IP, prefix, and gateway manually.

    An interface config or DHCP pool rejects .0 or .255 in a /24 network

    Cause
    In a standard subnet, the first address is reserved for the network ID (all host bits zero) and the last for broadcast (all host bits one).
    Fix
    Assign hosts from the usable range (.1–.254 in /24). Only /31 point-to-point links and /32 single-host routes are exceptions.