Output only MAC address on Ubuntu
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:
ab:cd:ef:12:34:56
57:89:12:34:ac:23
12:34:56:ab:cd:ef
How can I obtain this?
Solution 1:
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
34:17:eb:5d:88:7c
For all devices:
$ cat /sys/class/net/*/address
34:17:eb:5d:88:7c
00:00:00:00:00:00
64:5a:04:69:50:45
Solution 2:
The easiest way would be to use grep
with PCRE:
$ ifconfig -a | grep -Po 'HWaddr \K.*$'
74:d4:35:84:34:13
grep -P
will enable us to useperl
compatible Regexgrep -o
will only take the matched portion of the lineWe have matched
HWaddr
before our desired match (MAC addresses) and then discardHWaddr
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.*$'
74:d4:35:84:34:13