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
2 Answers
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.
-
-
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 ofip
, which is as easily parsable:ip link show dev eth0 | sed -nre 's@.*link\/ether (\S+).*@\1@p'
– BatchyXJan 31, 2013 at 19:47 -
awk
(or perl, or ...) and pull out just the bit you want.