63

What is the best way to extract the MAC address from ifconfig's output?

Sample output:

bash-3.00# ifconfig eth0        
eth0      Link encap:Ethernet  HWaddr 1F:2E:19:10:3B:52    
          inet addr:127.0.0.66  Bcast:127.255.255.255  Mask:255.0.0.0    
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          ....
          ....

Should I use cut, AWK or anything else, and what are the merits and demerits of one method over the other.

2
  • Which one is mac address?
    – Nabin
    Sep 16, 2014 at 11:36
  • @Nabin: the "HWaddr" field. Nov 22, 2014 at 16:33

19 Answers 19

118

You can do a cat under /sys/class/

cat /sys/class/net/*/address

Specifically for eth0

cat /sys/class/net/eth0/address
3
  • 1
    This gives all addresses, not just for eth0, for example. May 8, 2014 at 15:56
  • Iin openwrt cat /sys/class/net/eth0/address works a treat: "cat /sys/class/net/eth0/address" gives "c2:ee:1e:08:e6:39" Note eth0 links you to the hardware specific location:"cd /sys/class/net/eth0/" changes the cwd to "/sys/devices/platform/ag71xx.0/net/eth0/" Jul 9, 2015 at 9:23
  • 1
    To store in a variable: MacAdd=`cat /sys/class/net/eth0/address`.
    – haccks
    Aug 18, 2016 at 7:43
74

I would use:

ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

The -o will cause grep to only print the part of the line that matches the expression. [[:xdigit:]]{1,2} will match 1 or 2 hexidecimal digits (Solaris doesn't output leading zeros).

8
  • 1
    What? This will match regardless of placement of the MAC address whereas the other solutions will not. Oct 29, 2008 at 6:04
  • It does have the advantage of also working on OSX since that ifconfig use 'ether' instead of 'HWaddr'
    – albertb
    Oct 29, 2008 at 6:07
  • That was my intention, I have worked with plenty of different Unixes and this is the only solution that will work on all of them. Oct 29, 2008 at 6:10
  • 3
    That 'grep -o' is a handy addition to my knowledge as well.
    – paxdiablo
    Oct 29, 2008 at 6:53
  • 1
    I have used ifconfig eth0 | grep -Eo '([[:xdigit:]]{1,2}[:-]){5}[[:xdigit:]]{1,2}' | head -n1 to compensate for devices that contain '-' instead of ':' and/or report more than 6 bytes.
    – Darcara
    Sep 29, 2013 at 20:09
25

I like using /sbin/ip for these kind of tasks, because it is far easier to parse:

$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff

You can trivially get the mac address from this output with awk:

$ ip link show eth0 | awk '/ether/ {print $2}'
00:0c:29:30:21:48

If you want to put a little more effort in, and parse more data out, I recommend using the -online argument to the ip command, which will let you treat every line as a new device:

$ ip -o link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue \    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\    link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\    link/ether 00:0c:29:30:21:52 brd ff:ff:ff:ff:ff:ff
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 100\    link/[65534] 
5: sit0: <NOARP> mtu 1480 qdisc noop \    link/sit 0.0.0.0 brd 0.0.0.0
1
  • Not all distros ship /sbin/ip, which belongs to the iproute2 package. But this is the best solution: ip is much nicer than ifconfig!
    – ephemient
    Oct 29, 2008 at 14:30
10

Not sure whether there really are any advantages, but you can simply use awk:

ifconfig eth0 | awk '/HWaddr/ {print $5}'
1
  • Use ip addr instead of ifconfig. ip addr | grep -C1 "link/ether" should do.
    – nPcomp
    Dec 29, 2016 at 19:12
4

Since the OP's example refers to Bash, here's a way to extract fields such as HWaddr without the use of additional tools:

x=$(ifconfig eth0) && x=${x#*HWaddr } && echo ${x%% *}

In the 1st step this assigns the ouput of ifconfig to x. The 2nd step removes everything before "HWaddr ". In the final step everything after " " (the space behind the MAC) is removed.

Reference: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

3

For Ubuntu/Debian

ifconfig | grep HW | awk '{print $5}'

For Rhat or CentOs try

ip add | grep link/ether | awk '{print $2}'
0
2

I prefer the method described here (with slight modification): http://www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac.html

ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d " " -f2

Which you can then alias to a short 'myip' command for future use:

echo "alias myip=\"ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d ' ' -f2\"" >> ~/.bash_profile
1

On Ubuntu 14.04 in terminal

ifconfig | grep HW
1

How about this one:

ifconfig eth0 | grep -Eo ..\(\:..\){5}

or more specifically

ifconfig eth0 | grep -Eo [:0-9A-F:]{2}\(\:[:0-9A-F:]{2}\){5}

and also a simple one

ifconfig eth0 | head -n1 | tr -s ' ' | cut -d' ' -f5`
1
ifconfig en1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' 
  • Replace "en1" with the name of the nic card "eth0" or remove altogether "en1" - easy and useful solution.
0

Note: on OS X eth0 may not work. Use p2p0:

ifconfig p2p0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
1
  • interface on my mac server is en0 , so p2p0 doesn't work always Sep 7, 2012 at 9:43
0

This works for me on Mac OS X:

ifconfig en0 | grep -Eo ..\(\:..\){5}

So does:

ifconfig en0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Both are variations of examples above.

0

Nice and quick one:

ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11
0
ifconfig | grep -i hwaddr | cut -d ' ' -f11
0

I needed to get MAC address of the active adapter, so ended up using this command.

ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Hope it helps.

0

ifconfig en0 | grep ether - for wired mac address

ifconfig en1 | grep ether - for wireless mac address

0

this worked for me

ifconfig eth0 | grep -o -E ..:..:..:..:..:..

instead of eth0 you can write the interface you need it's mac address

-1

Output of ifconfig:

$ifconfig

eth0      Link encap:Ethernet  HWaddr 00:1b:fc:72:84:12
      inet addr:172.16.1.13  Bcast:172.16.1.255  Mask:255.255.255.0
      inet6 addr: fe80::21b:fcff:fe72:8412/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:638661 errors:0 dropped:20 overruns:0 frame:0
      TX packets:93858 errors:0 dropped:0 overruns:0 carrier:2
      collisions:0 txqueuelen:1000
      RX bytes:101655955 (101.6 MB)  TX bytes:42802760 (42.8 MB)
      Memory:dffc0000-e0000000

lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:3796 errors:0 dropped:0 overruns:0 frame:0
      TX packets:3796 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:517624 (517.6 KB)  TX bytes:517624 (517.6 KB)

The best way to extract MAC address is:

ifconfig | sed '1,1!d' | sed 's/.*HWaddr //' | sed 's/\ .*//' | sed -e 's/:/-/g' > mac_address
-1

Use:

ifconfig eth0 | grep HWaddr

or

ifconfig eth0 |grep HWaddr

This will pull just the MAC address and nothing else.

You can change your MAC address to whatever you want:

ifconfig eth0 down,
ifconfig eth0 hw ether (new MAC address),
ifconfig eth0 up
1
  • HW, HWaddr is not an option for some Linux/Unix systems, and it will show nothing.
    – amrx
    May 5, 2016 at 6:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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