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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aman Jain
  • 10,927
  • 15
  • 50
  • 63

19 Answers19

118

You can do a cat under /sys/class/

cat /sys/class/net/*/address

Specifically for eth0

cat /sys/class/net/eth0/address
Michalis
  • 1,508
  • 1
  • 10
  • 10
  • 1
    This gives all addresses, not just for eth0, for example. – MickeyfAgain_BeforeExitOfSO May 08 '14 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/" – NULL pointer Jul 09 '15 at 09:23
  • 1
    To store in a variable: `MacAdd=\`cat /sys/class/net/eth0/address\``. – haccks Aug 18 '16 at 07: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).

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
  • 1
    What? This will match regardless of placement of the MAC address whereas the other solutions will not. – Robert Gamble Oct 29 '08 at 06:04
  • It does have the advantage of also working on OSX since that ifconfig use 'ether' instead of 'HWaddr' – albertb Oct 29 '08 at 06: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. – Robert Gamble Oct 29 '08 at 06:10
  • I don't understand the downvote, my original thought was that it's over-engineered for the job but it works and is better on slightly different output (I'd want to put it in a script so I don't have to remember the regex). – paxdiablo Oct 29 '08 at 06:51
  • 3
    That 'grep -o' is a handy addition to my knowledge as well. – paxdiablo Oct 29 '08 at 06:53
  • Are there any UNICES that don't output leading zeros for the MAC sections (necessitating "{1,2}" instead of "{2}")? – paxdiablo Oct 29 '08 at 06:57
  • This is the best way I've seen. That's because this command will be useful not only with ifconfig but with any other source of MAC as could be extracting the MACS of Xen DomU with xm network-list the MACS of the ARP caché, etc – Álvaro Mar 12 '12 at 09:04
  • 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 '13 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
Jerub
  • 41,746
  • 15
  • 73
  • 90
  • 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 '08 at 14:30
10

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

ifconfig eth0 | awk '/HWaddr/ {print $5}'
albertb
  • 2,874
  • 1
  • 20
  • 16
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

xebeche
  • 905
  • 7
  • 20
3

For Ubuntu/Debian

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

For Rhat or CentOs try

ip add | grep link/ether | awk '{print $2}'
nPcomp
  • 8,637
  • 2
  • 54
  • 49
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
manafire
  • 5,984
  • 4
  • 43
  • 53
1

On Ubuntu 14.04 in terminal

ifconfig | grep HW
Fernando_Jr
  • 430
  • 5
  • 10
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`
kenorb
  • 155,785
  • 88
  • 678
  • 743
phoxis
  • 60,131
  • 14
  • 81
  • 117
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.
Sergio Rodriguez
  • 8,258
  • 3
  • 18
  • 25
0

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

ifconfig p2p0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CRGreen
  • 3,406
  • 1
  • 14
  • 24
0
ifconfig | grep -i hwaddr | cut -d ' ' -f11
whoan
  • 8,143
  • 4
  • 39
  • 48
0

Nice and quick one:

ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11
kenorb
  • 155,785
  • 88
  • 678
  • 743
Hugh
  • 193
  • 1
  • 1
  • 11
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.

Dogukan
  • 111
  • 2
0

ifconfig en0 | grep ether - for wired mac address

ifconfig en1 | grep ether - for wireless mac address

Sateesh Pasala
  • 772
  • 8
  • 15
0

this worked for me

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

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

Jason
  • 2,493
  • 2
  • 27
  • 27
Adel Skn
  • 1
  • 4
-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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amar
  • 509
  • 3
  • 8
  • 17
-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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131