Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAC address using shell script

Currently all the solution mentioned for getting the MAC address always use eth0. But what if instead of eth0 my interfaces start with eth1. Also on OS X the interface names are different.
Also the interface eth0 may be present but is unused. i.e. not active, it doesn't have an IP.

So is there a way I could get the MAC address for the first available interface that is Active.(i.e. it has an inet address, I even don't want one having inet6).

For E.g

eth0      Link encap:Ethernet  HWaddr <some addr>           inet6 addr: <some addr> Scope:Link           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:123           RX packets:123 errors:123 dropped:123 overruns:123 frame:123           TX packets:123 errors:123 dropped:123 overruns:123 carrier:123           collisions:123 txqueuelen:123            RX bytes:123 (123 MB)  TX bytes:123 (123 KB)           Interrupt:123 Memory:00000000-00000000  eth1      Link encap:Ethernet  HWaddr <some addr>           inet addr:<some addr>  Bcast:<some addr>  Mask:<some addr>           inet6 addr: <some addr> Scope:Link           UP BROADCAST RUNNING MULTICAST  MTU:123 Metric:123           RX packets:123 errors:123 dropped:123 overruns:123 frame:123           TX packets:123 errors:123 dropped:123 overruns:123 carrier:123           collisions:123 txqueuelen:123            RX bytes:123 (123 MB)  TX bytes:123 (123 KB)           Interrupt:123 Memory:00000000-00000000 

NOTE : I have changed the values of the output.

So in this case I want the HWaddr for eth1 and not eth0. How do I find it ? Also it should work on all the Linux flavours.

like image 704
Pratham Avatar asked May 23 '14 11:05

Pratham


People also ask

How do I find MAC address in shell?

On a Linux machine Open a terminal window. Type ifconfig at the command prompt. Your MAC address will be displayed beside the label HWaddr.

How do I find my MAC address on Linux?

UNIX or Linux devicesOpen a terminal. Type ifconfig -a and press Enter. -> HWaddr or ether or lladdr is the device's MAC address.

How do I find my MAC address in putty?

Type ipconfig /all and press Enter. This will display your network configuration. Look for the MAC address listed as Physical Address.


2 Answers

You can do as follows

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

Also you can get MAC for all interface as follows

cat /sys/class/net/*/address 

For particular interface like for eth0

cat /sys/class/net/eth0/address 
like image 82
Jayesh Bhoi Avatar answered Oct 02 '22 10:10

Jayesh Bhoi


The best Linux-specific solution is to use sysfs:

$ IFACE=eth0 $ read MAC </sys/class/net/$IFACE/address $ echo $IFACE $MAC eth0 00:ab:cd:12:34:56 

This method is extremely clean compared to the others and spawns no additional processes since read is a builtin command for POSIX shells, including non-BASH shells. However, if you need portability to OS X, then you'll have to use ifconfig and sed methods, since OS X does not have a virtual filesystem interface like sysfs.

like image 24
Dave Avatar answered Oct 02 '22 10:10

Dave