Ubuntu – Output only MAC address on Ubuntu

command linenetworking

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?

Best Answer

  • 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.*$'