Can linux modify the device mac address?

藏色散人
Release: 2023-03-09 10:42:59
Original
10580 people have browsed it

linux可以修改设备mac地址,其修改方法:1、通过“ifconfig eth0 down ifconfig eth0 hw ether xx:xx:xx:xx ifconfig eth0 up”指令临时修改mac地址;2、使用ip命令修改MAC地址;3、通过ethtool工具修改eeprom值即可永久修改mac地址。

Can linux modify the device mac address?

本教程操作环境:linux5.9.8系统、Dell G3电脑。

linux可以修改设备mac地址吗?

linux下修改网卡MAC地址

前言

一块网卡的mac地址在出厂时已经写入,有时候是写入可以擦写的eeprom中,但是要有相当的专业技术或专业的设备。

对于一些绑定了mac地址,更换了设备后又不想更换mac的,就需要修改mac地址了。

临时修改(重启失效):

方法一:
ifconfig eth0 down
ifconfig eth0 hw ether xx:xx:xx:xx
ifconfig eth0 up

方法二:

使用ip命令就能查看MAC地址了:

ip link show ${interface}
: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether f0:de:f1:ad:1d:f0 brd ff:ff:ff:ff:ff:ff
Copy after login

link/ether 后面那一串用 : 分割的6字节十六进制数就是网卡的MAC地址了,也就是 f0:de:f1:ad:1d:f0

使用ip命令也能修改MAC地址,但是需要root权限:

1、禁用网卡

sudo ip link set dev ${interface} down
Copy after login

2、修改MAC地址

有的网络运营商可能会拒绝为不正确的 MAC 分配 IP 地址,因此推荐前三个字节用真实的MAC地址前缀,后三个字节可以随机更改。

sudo ip link set dev ${interface} address ${new_mac}
Copy after login

我们再查看一下网卡的MAC地址

ip link show ${interface}
: enp0s25: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether f0:de:f1:ff:ff:ff brd ff:ff:ff:ff:ff:ff
Copy after login

你会发现网卡已经被改成新的MAC地址了.

3、最后重启网卡

sudo ip link set dev ${interface} up
Copy after login

永久修改:

通过ethtool工具修改eeprom值

查看

ethtool -e 网卡名 查看eeprom里面的值
Copy after login

修改

ethtool -E 网卡名 magic 0xVenID与DevID的8字节16进制整数 offset 偏移(如0×0000)value 值(如:0xAA)

magic的值:

~# lspci  -s 02:05.0 -nn

02:05.0 Ethernet controller [0200]: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) [8086:100f] (rev 01)

上面的[8086:100f]:100f为VenID,8086为DevID,magic的值为:0x100f8086
Copy after login

或者观察eeprom 0×0010地址处的值获得:

~# ethtool -e eth1 | grep 0x0010 | awk &#39;{print "0x"$13$12$15$14}&#39;
0x100f8086
Copy after login

offset的值:

[root@SG2-21 /root]#ethtool -e eth0
Offset          Values
------          ------
0x0000          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0010          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0020          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0030          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0040          00 50 c2 2f 30 13 ff ff ff ff ff ff ff ff ff ff 
0x0050          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0060          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
0x0070          ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
Copy after login

举例:

修改eth0 MAC地址的命令:

ethtool -E eth0 magic 0x100f8086 offset 0x0040 value 0x00
ethtool -E eth0 magic 0x100f8086 offset 0x0041 value 0x0a
ethtool -E eth0 magic 0x100f8086 offset 0x0042 value 0x0b
ethtool -E eth0 magic 0x100f8086 offset 0x0043 value 0x00
ethtool -E eth0 magic 0x100f8086 offset 0x0044 value 0x01
ethtool -E eth0 magic 0x100f8086 offset 0x0045 value 0x03
Copy after login

脚本:

#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 /<interface/>"
echo "       i.e. $0 eth0"
exit 1
fi

if ! ifconfig $1 > /dev/null; then
exit 1
fi

dev=$(ethtool -e $1 | grep 0x0010 | awk &#39;{print "0x"$13$12$15$14}&#39;)
ethtool -E $1 magic $dev offset 0xAA value 0xBB

0xAA 就是eeprom位置
0xBB 就是新值
Copy after login

Setting the MAC Address:For example

  [root@SG2-21 /root]#setMAC 00:50:C2:2F:30:13
  setmac to 00:50:C2:2F:30:13
Copy after login

The script is:

  #!/bin/bash

  echo "setmac to $1"
  numbers=(`echo $1 | tr &#39;:&#39; &#39; &#39;`)
  # echo ${numbers[1]}

  ethtool -E eth0 magic 0x3A8EBEEF offset 0x40 value 0x${numbers[0]}
  ethtool -E eth0 magic 0x3A8EBEEF offset 0x41 value 0x${numbers[1]}
  ethtool -E eth0 magic 0x3A8EBEEF offset 0x42 value 0x${numbers[2]}
  ethtool -E eth0 magic 0x3A8EBEEF offset 0x43 value 0x${numbers[3]}
  ethtool -E eth0 magic 0x3A8EBEEF offset 0x44 value 0x${numbers[4]}
  ethtool -E eth0 magic 0x3A8EBEEF offset 0x45 value 0x${numbers[5]}
Copy after login

相关推荐:《Linux视频教程》

The above is the detailed content of Can linux modify the device mac address?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template