Linux Subnet Mask
In Linux networking, a subnet mask is a 32-bit value that divides an IPv4 address into network and host portions. It determines which IP addresses a host can reach directly on the local link and which traffic must be forwarded through a gateway. Modern Linux systems typically express subnet masks using CIDR notation. For example, 192.168.1.10/24 combines an IP address with a prefix length of 24, which is equivalent to the dotted-decimal mask 255.255.255.0.
The mask consists of consecutive 1 bits followed by consecutive 0 bits. The 1 bits represent the network portion, while the 0 bits represent the host portion. Devices sharing the same network prefix belong to the same subnet and can communicate directly without router assistance. When a destination lies outside the local subnet, the host sends the packet to its configured default gateway.
Binary Representation and Common Prefixes
Because both IPv4 addresses and subnet masks are 32 bits long, they map neatly to CIDR prefix lengths. The following examples show how dotted-decimal masks correspond to binary patterns and prefix lengths:
255.255.255.0is11111111.11111111.11111111.00000000, or/24.255.255.255.128is11111111.11111111.11111111.10000000, or/25.255.255.255.192is11111111.11111111.11111111.11000000, or/26.255.255.255.224is11111111.11111111.11111111.11100000, or/27.
Each additional network bit halves the size of the available host space, which is an important consideration when planning address allocation.
Common Subnet Mask Reference
| CIDR | Subnet Mask | Usable Hosts |
|---|---|---|
| /24 | 255.255.255.0 | 254 |
| /25 | 255.255.255.128 | 126 |
| /26 | 255.255.255.192 | 62 |
| /27 | 255.255.255.224 | 30 |
| /28 | 255.255.255.240 | 14 |
The number of usable hosts is calculated as 2^(32 - prefix length) - 2. The subtraction accounts for the network address and the broadcast address, neither of which can be assigned to a host interface.
Two prefix lengths deserve special attention. A /31 network provides exactly two addresses and is commonly used for point-to-point links between routers. A /32 network contains a single address and typically represents a host route rather than a usable subnet.
Viewing Subnet Mask Information
Linux provides several ways to inspect interface addressing and subnet mask information. The modern ip utility is preferred on most current distributions:
ip addr show
ip -4 addr show dev eth0
Typical output resembles the following:
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
Here, /24 indicates the subnet mask prefix length, while brd shows the broadcast address for the subnet. Older systems may still use ifconfig:
ifconfig eth0
To verify routing and confirm the default gateway, use:
ip route
The routing table displays connected subnets and the gateway used for traffic destined outside those subnets.
Temporary Configuration
The ip command allows administrators to assign addresses and masks without persisting changes across reboots. This approach is useful for temporary testing or emergency recovery:
sudo ip addr add 192.168.1.10/24 dev eth0
sudo ip link set eth0 up
sudo ip route add default via 192.168.1.1 dev eth0
If an address already exists on the interface and needs to be changed, flush the interface first:
sudo ip addr flush dev eth0
sudo ip addr add 192.168.1.10/24 dev eth0
Changes made with ip are lost after a restart, so they should not be treated as a substitute for persistent network configuration.
Permanent Configuration by Distribution
Different Linux distributions rely on different network management backends. The appropriate method depends on what is installed and running on the host.
NetworkManager
NetworkManager is widely used on desktop and server distributions. The nmcli command-line tool can modify connection profiles directly:
nmcli con mod eth0 ipv4.addresses