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.
Observe that the interface name and the MAC address are the first and last fields on a line with no leading whitespace.
If one of the indented lines contains
inet addr:
the latest interface name and MAC address should be printed.Note that multiple interfaces could meet your criteria. Then, the script will print multiple lines. (You can add
; exit
just before the final closing brace if you always only want to print the first match.)Here's an alternative answer in case the ones listed above don't work for you. You can use the following solution(s) as well, which was found here:
OR
OR
All three of these will show your MAC address(es) next to
link/ether
. I stumbled on this because I had just done a fresh install of Debian 9.5 from a USB stick without internet access, so I could only do a very minimal install, and receivedwhen I tried some of the above solutions. I figured somebody else may come across this problem as well. Hope it helps.
I know that is a little bit dated, but with basic commands, we can take the mac address of an interface:
Have a nice day!
Get MAC adress for eth0:
Example:
Simply run:
OR
These two example commands will grep all lines with "ether" string and cut the mac address (that we need) following the number spaces (specified in the -f option) of the grepped portion.
Tested on different Linux flavors