9

I want to get my netmask on Linux. It is outputted with ifconfig but I want to extract the string.

1
  • 5
    This question is hard, actually, if you don't specify your OS, e.g. FreeBSD's ifconfigs netmask line is: inet 192.168.144.120 netmask 0xffffff00 broadcast 192.168.144.255, so while the answers can be easily adjusted, there's probably no one-size-fits-all (using Bash/ifconfig only).
    – sr_
    Commented Nov 8, 2011 at 14:46

6 Answers 6

9

I should clarify that the code here works for Linux, (note comments and post about other Unices). OP asked for a Linux solution, but it would be good to change the question to "how to get netmask" in general, and have the answer combine the best way for more Unix flavors.

#!/bin/sh
ifconfig "$1" | sed -rn '2s/ .*:(.*)$/\1/p'

./script eth0

About ifconfig in Linux

Along with other traditional networking commands like netstat, arp, rarp and route, ifconfig is part of the net-tools package. Net-tools hasn't been actively developed from a really long time and there are efforts to deprecate it in favor of the newer iproute2 package. For the sake of brevity, if you want more details on this fundamental transition, here are some relevant links:

Case example: Debian Linux

Case example: Arch Linux

Man page for net-tools ifconfig (see BUGS section)

Analysis and comparison of the frameworks from a users perspective

1
  • If the interface in question has no netmask this solution returns '1'. The regex needs some serious rework to make it reliable/safe including the need for a proper anchor on the very important word 'Mask'.
    – Oly Dungey
    Commented Dec 5, 2017 at 16:35
7
/sbin/ifconfig eth0 | awk '/Mask:/{ print $4;} '

?

3
  • You are missing | cut -c6- in order to also remove the "Mask:" Commented Nov 8, 2011 at 14:52
  • 3
    Specifying the awk field separator also works to remove "Mask:": /sbin/ifconfig eth0 | awk -F: '/Mask:/{print $4}'
    – Shane
    Commented Sep 16, 2016 at 20:57
  • In Debian Stretch/net-tools 2.10-alpha I had to change to this: /sbin/ifconfig eth0 | awk '/netmask/{print $4}'
    – Shane
    Commented Dec 10, 2017 at 6:30
7

I wonder if you really only want the mask. Maybe you really want the network (or IP) with mask, to use in config files, with nmap, or whatever.

In that case, on Linux, you can also parse the output of the ip ... command.

To list all interfaces with their address and mask in CIDR notation:

ip -o -f inet addr show | awk '/scope global/ {print $2, $4}'

Or to restrict it to the default interface:

default_if=$(ip route list | awk '/^default/ {print $5}')
ip -o -f inet addr show $default_if | awk '{print $4}'

which gives me "192.168.1.61/24" on my machine.

1
  • 2
    ...it's not really necessary to match on 'scope global' or 'default' either as you can do ip -o -f inet addr show scope global and ip route list default.
    – bryn
    Commented Aug 11, 2019 at 13:22
3

if you're using an OS that outputs the mask in hex, you can do something like:

#!/usr/bin/bash

# read the mask, and strip leading 0x if it's there
hexmask=$( echo $1 | sed -e 's/^0x//' )

# loop through $hexmask in pairs
#
for (( i=0; i<${#hexmask}; i+=2 )); do
        if (( $i > 1 )); then
                # use a . to separate octets
                # but don't print a leading .
                printf "%s" "."
        fi
        printf "%d" "0x${1:$i:2}"
done

printf "\n"
0
2
/sbin/ifconfig eth0 | grep Mask | cut -d":" -f4
2
  • This is the best answer thus far as the rest rely on a fixed number of columns using awk which isn't the case in real life with ifconfig. I would suggest a few changes though: ifconfig eth0 | grep -o 'Mask:[^\s]*' | cut -d':' -f2. The -o so that grep only returns part of the line rather than the whole lot (more efficient). Match non-whitespace characters ([^\s]*) to find end of the netmask token. Single quotes around the field delimiter for safety.
    – Oly Dungey
    Commented Dec 6, 2017 at 11:20
  • on macOS I get /sbin/ifconfig | grep ask | sed -e 's/.*netmask //g' | sed -e 's/ .*//g' to output the mask in hex.
    – Tomachi
    Commented Mar 15, 2020 at 1:30
1

If you are seeking the netmask for the default network device, this command returns the default device since the default device might not be eth0

netdevice=$(ip r | grep default | awk '/default/ {print $5}')

And this command retrieves the mask:

netmask=$(ifconfig "$netdevice" | awk '/netmask/{ print $4;}')

Thanks to the Eliah Kagan and Paulo Tome that helped me to both simplify and clarify the answer. On my personal machine, the default ethernet device was "wlx504654c1a91" which was way too complicated to remember and type accurately, hence the two step process.

1
  • 1
    grep 'pattern' | awk '{ action }' can usually be reduced to awk '/pattern/ { action }' Commented Jan 1, 2020 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .