Skip to Content

How to Find Subnet Mask in Linux

A subnet, or subnetwork, is a logical subdivision of an IP network. It’s essentially a smaller network within a larger network, created by dividing the network address space into smaller segments. This division is achieved through a process called subnetting.

By dividing the network into smaller segments, subnet allows for more efficient management of network resources, such as IP addresses and bandwidth.

Think of an apartment building as a network. Each floor of the building represents a subnet. Each apartment on a floor is a host. Subnetting is like dividing the floors into smaller segments, with each segment having its own set of apartments. This makes it easier to manage the building and provide services to individual apartments.

Getting Subnet Information on Linux

To get subnet information on a Linux system, you can use several command-line tools that provide details about your network configuration.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

These tools include details about IP addresses, subnet masks, and other relevant data.

Using the ip Command

  1. Open Terminal: Access your terminal on your Linux system.
  2. Run the ip Command: Type ip addr show in the terminal. This command displays information about all network interfaces, including their IP addresses and subnet masks.
  3. Identify Subnet Information: Look for the inet line under your active network interface (e.g., eth0, wlan0) to see the IP address and subnet mask. For example, 192.168.1.2/24 indicates a subnet mask of 255.255.255.0.

Using the ifconfig Command

  1. Install ifconfig if Necessary: On some newer distributions, ifconfig is deprecated. Install it via your distribution’s package manager if needed.
  2. Run ifconfig: Type ifconfig in the terminal to list network interfaces and their details, including the subnet mask.

Using the nmcli Command (for NetworkManager)

  1. Run nmcli: Type nmcli in the terminal for detailed network configurations, including subnet masks.

Example

After running ip addr show, you might see output like this for an Ethernet interface:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever

Here, 192.168.1.2/24 indicates an IP address of 192.168.1.2 with a subnet mask of 255.255.255.0 (or /24 in CIDR notation).

These tools are fundamental for network administration in Linux and provide valuable information about your network configuration, including subnet details.

Converting CIDR to Subnet Mask in Linux

To convert CIDR (Classless Inter-Domain Routing) notation to a subnet mask in Linux, you can use simple calculations or command-line tools. Here are two methods to do this:

Method 1: Using the ipcalc Tool

  1. Install ipcalc: Install ipcalc using your distribution’s package manager. For example, on Ubuntu or Debian:
    sudo apt-get install ipcalc
  2. Run ipcalc with CIDR Notation: Use ipcalc with your CIDR notation. For example:
    ipcalc 192.168.1.0/24
  3. Check Output: ipcalc will display various network information, including the Netmask which is your subnet mask.

Method 2: Manual Conversion

  1. Understand CIDR: The number after the slash (/) in CIDR represents the bits used for the network portion of the address. For example, /24 means the first 24 bits are for the network.
  2. Calculate Subnet Mask: Convert the CIDR value into a subnet mask. For /24, it’s 255.255.255.0.
  3. Use a Table for Quick Reference: Refer to a quick table for common CIDR to subnet mask conversions.
    • /32 = 255.255.255.255
    • /31 = 255.255.255.254
    • /30 = 255.255.255.252
    • /29 = 255.255.255.248
    • /28 = 255.255.255.240
    • /27 = 255.255.255.224
    • /26 = 255.255.255.192
    • /25 = 255.255.255.128
    • /24 = 255.255.255.0

Example

Using ipcalc:

$ ipcalc 192.168.1.0/24
Address:   192.168.1.0
Netmask:   255.255.255.0 = 24
...

Manual Calculation:

For a CIDR of /24, the subnet mask is 255.255.255.0.

These methods provide quick ways to convert CIDR notation to a subnet mask in Linux, through a command-line utility or simple binary-to-decimal conversion logic.

3 Linux commands to list network interfaces

how to configure network with nmcli command in Linux

Linux Network Configuration Tools &#8211; Configuring IP Addresses and Routing

Top 3 ways to restart Network Interfaces in Linux