Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
This question is hard, actually, if you don't specify your OS, e.g. FreeBSD's ifconfigs netmask line is: inet netmask 0xffffff00 broadcast, so while the answers can be easily adjusted, there's probably no one-size-fits-all (using Bash/ifconfig only). – sr_ Nov 8 '11 at 14:46
up vote 7 down vote accepted

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.

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

share|improve this answer
/sbin/ifconfig eth0 | awk '/Mask:/{ print $4;} '


share|improve this answer
You are missing | cut -c6- in order to also remove the "Mask:" – michelemarcon Nov 8 '11 at 14:52

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


# 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" "."
        printf "%d" "0x${1:$i:2}"

printf "\n"
share|improve this answer

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 "" on my machine.

share|improve this answer
/sbin/ifconfig eth0 | grep Mask | cut -d":" -f4
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.