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.
oh, if you want only the mac ether mac address, you can use that:
(work on macintosh)
ifconfig
-- get all infogrep
-- keep the line with addresstr
-- clean allcut
-- remove the "ether" to have only the addressOn a modern GNU/Linux system you can see the available network interfaces listing the content of
/sys/class/net/
, for example:You can check if an interface is
up
looking atoperstate
in the device directory. For example, here's how you can see ifenp0s25
is up:You can then get the MAC address of that interface with:
For example, here's a simple bash script that prints MAC addresses for active interfaces:
And here's its output on a system with an ethernet and a wifi interface:
For details see the Kernel documentation
Remember also that from 2015 most GNU/Linux distributions switched to
systemd
, and don't useethX
interface naming scheme any more - now they use a more robust naming convention based on the hardware topology, see:The best Linux-specific solution is to use sysfs:
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 useifconfig
andsed
methods, since OS X does not have a virtual filesystem interface like sysfs.return: eth0 (my online interface)
return: ec:a8:6b:bd:55:05 (macaddress of the eth0, my online interface)
Terminal image
You can do as follows
Also you can get MAC for all interface as follows
For particular interface like for eth0
None of the above worked for me because my devices are in a balance-rr bond. Querying either would say the same MAC address with
ip l l
,ifconfig
, or/sys/class/net/${device}/address
, so one of them is correct, and one is unknown.But this works if you haven't renamed the device (any tips on what I missed?):
And this works even if you rename it (eg.
ip l set name x0 dev p4p1
):or my ugly script that makes it more parsable (untested driver/os/whatever compatibility):