I use a lot of scripts that involve the Mac addresses of my network cards. Is there a file I can read or a command that I can use to JUST get the Mac address? I don't want any headers or anything, just 1 line of output that has the Mac address. I would like to set this as an environment variable on login. I use Arch Linux x64

  • Whatever command you normally use to see the MAC address, pipe its output through awk (or perl, or ...) and pull out just the bit you want.
    – alexis
    Jan 31, 2013 at 19:29
  • Be aware that the MAC address is not a static unchangable value it used to be. Pretty much everywhere it is user configurable/changable.
    – mdpc
    Feb 1, 2013 at 3:32

2 Answers 2

 VARIABLE="$(cat /sys/class/net/$IFACE/address)"

It's hard to do it simpler.

Well, actually:

 read -r MACADDRESS < /sys/class/net/$iFACE/address

is even simpler, and starts no additional process.

  • VARIABLE=`cat /sys/class/net/$IFACE/address` also work.
    – haccks
    Aug 18, 2016 at 7:40
  • I knew there had to be a more graceful way than using sed on ip link show dev $iface. Well done. Nov 6, 2018 at 4:55

This works for me. Can probably be optimized a bit:

/sbin/ifconfig eth0 | grep -o 'HWaddr  *[0-9A-F:]*' | sed -s 's/HWaddr *//'

I don't think you can simplify it much from this. I did an strace on the ifconfig, and it seems you pull the MAC addr with an ioctl() on a socket, not from a /proc file.

  • 4
    ifconfig is deprecated on Linux, in favor of ip, which is as easily parsable: ip link show dev eth0 | sed -nre 's@.*link\/ether (\S+).*@\1@p'
    – BatchyX
    Jan 31, 2013 at 19:47
  • Have you figured out a way to do this without regular expressions?
    – Ben
    Aug 11, 2014 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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