In Ubuntu, with command ifconfig -a, I obtain all the information about my ethernet/wifi interfaces.

But I need to obtain as output only the MAC address, like:


How can I obtain this?

8 Answers 8


You can access the address file for each device on the /sys virtual filesystem. The MAC address should be in /sys/class/net/<device-name>/address:

$ cat /sys/class/net/enp1s0/address

For all devices:

$ cat /sys/class/net/*/address
  • 18
    +1 for not using a tool that's been deprecated for a decade :)
    – hobbs
    May 26, 2015 at 21:06
  • @hobbs Wait, which tool is that?
    – detly
    May 26, 2015 at 22:33
  • @detly ifconfig
    – hobbs
    May 26, 2015 at 22:33
  • 3
    @hobbs Woah. I was not aware of that. It does not print the deprecation warning on my machine.
    – detly
    May 26, 2015 at 22:35
  • 2
    @detly ifconfig's been deprecated for years now, and ip has also been around for years.
    – muru
    May 27, 2015 at 2:43

The easiest way would be to use grep with PCRE:

$ ifconfig -a | grep -Po 'HWaddr \K.*$'
  • grep -P will enable us to use perl compatible Regex

  • grep -o will only take the matched portion of the line

  • We have matched HWaddr before our desired match (MAC addresses) and then discard HWaddr by \K to print only the MAC addresses.

@Helio has mentioned an important point, this is highly dependent on your language i.e. locale settings. To overcome that you can use the C locale (uses ASCII character set) for this command only:

$ LANG=C ifconfig -a | grep -Po 'HWaddr \K.*$'
  • 2
    You may use LANG=C ifconfig -a instead of ifconfig -a for non-english installations, can be problems with grep. (i.e. In my system, HWaddr is direcciónHW)
    – 0x2b3bfa0
    May 26, 2015 at 11:32
  • @Helio what if you type: ifconfig -a | grep -i hw-*?
    – JoKeR
    May 26, 2015 at 15:06
  • 1
    @JoKeR just curious - is that -* supposed to mean something?
    – muru
    May 26, 2015 at 15:29
  • 2
    I have no HWaddr in my ifconfig output!
    – Owl
    Jun 21, 2019 at 9:58
  • 3
    On ubuntu 20.04, I found that HWaddr was swapped out with "ether" So I had to do ifconfig -a | grep -o 'ether [[:xdigit:]:]*' instead. Jun 4, 2020 at 18:25

Here are a few ways:

  1. grep. There are various regular expressions that will pick these up. Here, I am looking for 5 repetitions of 2 letters or numbers followed by a colon, then any two characters. The -i makes the match case insensitive and the -o makes grep print only the matched portion. -E enables extended regular expressions. The same regex also works with PCREs (-P).

    ifconfig -a | grep -ioE '([a-z0-9]{2}:){5}..'
  2. sed. The -n suppresses normal output and the -r enables extended regular expressions. Using the same regex as above, this script will attempt to replace everything on the line with the part of it that matches the regex. If the substitution was successful, the resulting line is printed (because of the p at the end of the substitution).

    ifconfig -a | sed -rn 's/.*(([a-z0-9]{2}:){5}..).*/\1/p'
  3. awk. If the line starts with a word character ([a-zA-Z0-9_]), and has 5 fields, print the last one.

    ifconfig -a | awk '/^\w/&&NF==5{print $NF}'
  4. Perl, where, as usual, there are more than one ways to do it. This one is the same logic as the awk above. The -a tells perl to split each input line into the @F array.

    ifconfig -a | perl -lane 'if(/^\w/&&$#F==4){print $F[$#F]}'

    Alternatively, you can use the regex from the previous approaches:

    ifconfig -a | perl -lne '/(([a-z0-9]{2}:){5}..)/ && print $1'
  5. Coreutils.

    LANG_ALL=C ifconfig -a | grep 'HWadd' | tr -s ' ' '\t' | cut -f 5

As some have commented, ifconfig is deprecated in favor of the ip command. So combining the various solutions and comments, I'd use:

$ LANG=C ip link show | awk '/link\/ether/ {print $2}'

ifconfig -a | grep HWaddr | awk '{print $5}'

If your system output is non-English in this command, then it makes sense to run it this way.

LANG=C ifconfig -a | grep HWaddr | awk '{print $5}'

This is applicable to all solutions.

  • 5
    Instead of using grep separately you could filter with awk directly like this: ifconfig -a | awk '/HWaddr/ { print $5 }'
    – krt
    May 26, 2015 at 11:21
  • Yes, there are really lots of ways to get this done.
    – Pilot6
    May 26, 2015 at 11:27
  • 2
    You may use LANG=C ifconfig -a instead of ifconfig -a for non-english installations, can be problems with grep. (i.e. In my system, HWaddr is direcciónHW)
    – 0x2b3bfa0
    May 26, 2015 at 11:31
  • Good notice. But in my Russian installation it is still HWaddr.
    – Pilot6
    May 26, 2015 at 11:33
  • Your Russian installation may be, but my Spanish installation doesn't. ;-) It can be a huge problem. Please, put @Helio at the begin of the comment. If not, I won't be notified.
    – 0x2b3bfa0
    May 26, 2015 at 11:36

If you want a layer of abstraction away from the /sys/class/net interface you could use the nmcli command. For example, the GENERAL field of the dev[ice] object contains the interface type and hardware address (MAC):

GENERAL.DEVICE:                         enp0s3
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:A7:C4:90

GENERAL.DEVICE:                         enp0s9
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:8E:46:AB

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00

You can get the output in a more parsable form using the -g | --get-values option:

$ nmcli -g GENERAL.TYPE,GENERAL.HWADDR -e no dev show



So for example in a script you could do

$ nmcli -g GENERAL.TYPE,GENERAL.HWADDR -e no dev show | awk -v RS= '$1 ~ "ethernet" {print $2}'

There are several ways for this :

$ /sys/class/net//address

this is the default location where u can find out the mac of any device.

$ cat /sys/class/net/*/address is the easiest way to find out mac of hardware parts in ur ubuntu PC.

$ ifconfig -a will also provide the same details with some extra data. or you can use the grep to filter out the mac alone like : $ ifconfig -a | grep HWaddr | awk '{print $5}'

other commands :

$ ifconfig eth0

$ ip link show

$ ip -o link show eth0

Here in this blog it is explaining the same and several other ways to get this.


The below command is working for my ubuntu PC.

ifconfig | grep ether

ether c8:f7:50:fd:14:c8 txqueuelen 1000  (Ethernet)
  • This doesn't output "only" the Mac address, you need to use awk as well. Oct 3, 2022 at 6:05

You must log in to answer this question.

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