0

Is there an easy way to view MAC address by hardware device? I know you could sling together a whole bunch of Bash logic with sed to do this but I figure there has to be an easier way to get this info. Ex: You have a server with a whole bunch of network cards and you want to see MAC address by device? I can figure out how to manually correlate them or I can just pull the info from IPMI but Linux always seems to have a fast way to do things - I just don't know what it is in this case. What I'm looking for is something like:

<Some command>

NIC 1 - Intel x710
<its MACs>
NIC 2 - MLX5
<its MACs>
NIC 3 - Broadcom Whatever
<its MACs>

Something like that. The important thing is determining the model of the network card: Mellanox MLX5, Intel x710, etc

3 Answers 3

1

ip link shows exactly this. Mine looks roughly like

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 4000 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether aa:aa:bb:bb:cc:dd brd ff:ff:ff:ff:ff:ff
3: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:01:02:03:04:05 brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
    link/ether 10:0F:0E:0D:0C:0B brd ff:ff:ff:ff:ff:ff

There's no "not much shell logic" way of mapping Linux network interfaces to "device names" (like your "NIC 1 - Intel x710"), especially in the context of things like multi-port SFP+ cards like the x710; these are designed to offer virtual functions, so the mapping of "one Linux network device belongs to one hardware device" simply works in neither direction.

2
  • Sincere apologies, I failed to make the example I wrote clear. The model of the card is the important piece I know how to match the MAC to just the OS' device name Dec 16, 2021 at 21:42
  • as said, since there's not really a 1:1 mapping from OS device names to hardware devices, there's no simpler way to do this than to take a few guesses and detours through PCI device listings, as described in Grant's answer. Generally, a OS network device might not map to any hardware device, or to a group of different hardware devices, and especially, there can be many OS network devices that map to the same hardware device. Dec 17, 2021 at 12:41
1

I'll leave this open in case someone comes up with something cleaner but what I ended up doing was combining lspci and ethtool:

[root@gputest ~]# lspci -v | grep Ethernet
04:00.0 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 Gigabit Ethernet PCIe
04:00.1 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 Gigabit Ethernet PCIe
31:00.0 Ethernet controller: Intel Corporation Ethernet Controller E810-XXV for SFP (rev 02)
        Subsystem: Intel Corporation Ethernet 25G 2P E810-XXV OCP
31:00.1 Ethernet controller: Intel Corporation Ethernet Controller E810-XXV for SFP (rev 02)
        Subsystem: Intel Corporation Ethernet 25G 2P E810-XXV OCP
98:00.0 Ethernet controller: Mellanox Technologies MT2892 Family [ConnectX-6 Dx]
98:00.1 Ethernet controller: Mellanox Technologies MT2892 Family [ConnectX-6 Dx]
b1:00.0 Ethernet controller: Mellanox Technologies MT28800 Family [ConnectX-5 Ex]
b1:00.1 Ethernet controller: Mellanox Technologies MT28800 Family [ConnectX-5 Ex]
[root@gputest ~]# ethtool -i eno8303
driver: tg3
version: 4.18.0-348.2.1.el8_5.x86_64
firmware-version: FFV21.81.3 bc 5720-v1.39
expansion-rom-version:
bus-info: 0000:04:00.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@gputest ~]# ethtool -i eno8403
driver: tg3
version: 4.18.0-348.2.1.el8_5.x86_64
firmware-version: FFV21.81.3 bc 5720-v1.39
expansion-rom-version:
bus-info: 0000:04:00.1
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@gputest ~]# ethtool -i eno12399
driver: ice
version: 4.18.0-348.2.1.el8_5.x86_64
firmware-version: 3.00 0x80008943 20.5.13
expansion-rom-version:
bus-info: 0000:31:00.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: yes

By checking the domain/bus/function numbers from lspci you can compare those with the output of ethtool and determine which device names are associated with what model card and from there determine the MAC address.

0

You can get all MAC addresses of your network cards with:

$ cat /sys/class/net/*/address
c8:5b:76:9e:65:9a
22:60:04:08:f0:37
00:00:00:00:00:00

34:f3:9a:01:e6:22

So you could easily extend that to:

$ ( cd /sys/class/net/ && for i in *; do printf '%s: %s\n' "$i" $(cat "$i"/address); done )
enp0s31f6: c8:5b:76:9e:65:9a
ipv6leakintrf0: 22:60:04:08:f0:37
lo: 00:00:00:00:00:00
tun0: 
wlp3s0: 34:f3:9a:01:e6:22

But really, parsing the output of the ip command given by Marcus Müller is your best bet:

$ ip link | awk '$1~/^[0-9]*:/{printf "%s ", $2} /^ /{print $2}'
lo: 00:00:00:00:00:00
enp0s31f6: c8:5b:76:9e:65:9a
wlp3s0: 34:f3:9a:01:e6:22
ipv6leakintrf0: 22:60:04:08:f0:37
tun0: 

And, if you want that as a single command, just make it into an alias. Add this line to your shell's initialization file (~/.bashrc if using bash):

alias getMac="ip link | awk '\$1~/^[0-9]*:/{printf \"%s \", \$2} /^ /{print \$2}'"

Then, open a new terminal and:

$ getMac 
lo: 00:00:00:00:00:00
enp0s31f6: c8:5b:76:9e:65:9a
wlp3s0: 34:f3:9a:01:e6:22
ipv6leakintrf0: 22:60:04:08:f0:37
tun0: 
1
  • Sincere apologies, I failed to make the example I wrote clear. The model of the card is the important piece I know how to match the MAC to just the OS' device name Dec 16, 2021 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .