Linux 查看网卡的MAC地址

霸气de小男生 提交于 2021-02-11 05:39:04

如何使用命令查看网卡的MAC地址呢? 下面总结一下查看网卡MAC地址的方式。

 

1:ifconfig命令查看网卡MAC地址

 

 

/sbin/ifconfig | grep HWaddr

/sbin/ifconfig | grep ether

 

有些Linux发行版本的MAC地址字段为HWaddr,有些Linux发行版本的MAC地址字段为ether。根据实际情况选择上面命令。

 

[root@KerryDB ~]# ifconfig -a
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 858  bytes 72251 (70.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 858  bytes 72251 (70.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
p4p1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.20.57.24  netmask 255.255.255.0  broadcast 10.20.57.255
        inet6 fe80::b283:feff:fe55:32e5  prefixlen 64  scopeid 0x20<link>
        ether b0:83:fe:55:32:e5  txqueuelen 1000  (Ethernet)
        RX packets 6637181  bytes 1536293500 (1.4 GiB)
        RX errors 0  dropped 293652  overruns 0  frame 0
        TX packets 4333535  bytes 1080300983 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

如果想只提取网卡MAC地址,可以使用下面命令(用具体的网卡名替换xxx)

 

ifconfig xxx | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

 

[root@KerryDB tmp]# ifconfig p4p1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
b0:83:fe:55:32:e5
[root@KerryDB tmp]#

 

 

 

 

 

方法2:/sys/class/net/xxx/address查看

 

 

根据网卡名调整,例如这里网卡名为ens160,如果网卡名为xxx,那么应该用/sys/class/net/xxx/address

 

# more /sys/class/net/ens160/address
00:50:56:95:5c:16
 
# cat /sys/class/net/eth0/address 
00:50:56:b3:81:f8

 

 

方法3:ip命令查看网卡MAC地址

 

[root@KerryDB tmp]# ip link
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: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether b0:83:fe:55:32:e5 brd ff:ff:ff:ff:ff:ff
[root@KerryDB tmp]# ip link show
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: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether b0:83:fe:55:32:e5 brd ff:ff:ff:ff:ff:ff
[root@KerryDB tmp]# ip link show p4p1
2: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether b0:83:fe:55:32:e5 brd ff:ff:ff:ff:ff:f
[root@KerryDB tmp]# ip link show p4p1 | awk '/ether/ {print $2}';
b0:83:fe:55:32:e5

 

 

 

4:nmcli命令查看网卡MAC地址

 

 

[root@KerryDB tmp]# nmcli device show p4p1
GENERAL.DEVICE:                         p4p1
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         B0:83:FE:55:32:E5
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     p4p1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         10.20.57.24/24
IP4.GATEWAY:                            10.20.57.1
IP4.ROUTE[1]:                           dst = 10.20.57.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 10.20.57.1, mt = 100
IP6.ADDRESS[1]:                         fe80::b283:feff:fe55:32e5/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ff00::/8, nh = ::, mt = 256, table=255
IP6.ROUTE[2]:                           dst = fe80::/64, nh = ::, mt = 256
[root@KerryDB tmp]# nmcli device show p4p1 | grep -i hw
GENERAL.HWADDR:                         B0:83:FE:55:32:E5

 

注意:这里根据实际的网卡名进行调整,如果网卡名为ens160,那么上面命令需要调整为

 

# nmcli device show ens160 | grep -i hwaddr

GENERAL.HWADDR:                         00:50:56:B4:00:94

 

 

5:dmesg命令查看网卡MAC地址

 

[root@KerryDB tmp]#  dmesg | grep eth
[    0.776155] r8169 0000:02:00.0 eth0: RTL8168g/8111g at 0xffffafbe80648000, b0:83:fe:55:32:e5, XID 0c000800 IRQ 27
[    0.776158] r8169 0000:02:00.0 eth0: jumbo features [frames: 9200 bytes, tx checksumming: ko]

 

 

 

参考资料:

 

https://www.169it.com/article/14360294838474691537.html

https://stackoverflow.com/questions/245916/best-way-to-extract-mac-address-from-ifconfigs-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!