Modifying MAC Address of Network Card under linux

Preface

The mac address of a network card has been written in the factory, sometimes in the erasable eeprom, but there must be considerable expertise or professional equipment.

For some mac addresses that are bound and do not want to change after replacing the device, it is necessary to modify the mac address.

Temporary modification (restart invalidation):

Method 1:
ifconfig eth0 down
ifconfig eth0 hw ether xx:xx:xx:xx
ifconfig eth0 up

Method 2:

With the ip command, you can view the MAC address:

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

The following string of links/ethers uses: the segmented 6-byte hexadecimal number is the MAC address of the network card, which is f0:de:f1:ad:1d:f0.

MAC addresses can also be modified using the ip command, but root privileges are required:

1. Disabling Network Cards

sudo ip link set dev ${interface} down

2. Modifying MAC Address

Some network operators may refuse to assign IP addresses to incorrect MACs, so it is recommended that the first three bytes be prefixed with real MAC addresses, and the last three bytes can be changed randomly.

sudo ip link set dev ${interface} address ${new_mac}

Let's check the MAC address of the network card again.

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

You will find that the network card has been changed to a new MAC address.

3. Finally restart the network card

sudo ip link set dev ${interface} up

Permanent modification:

Modifying eeprom values through the ethtool tool tool

See

Ethtool-e network card name to see the value in eeprom

modify

Etool-E Network Card Name magic 0xVenID and DevID 8-byte hexadecimal integer offset (e.g. 0*0000) value (e.g. 0xAA)

magic value:

~# lspci  -s 02:05.0 -nn

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

The above [8086:100f]: 100F is VenID, 8086 is DevID, and magic is 0x100f8086.

Or observe the value at eeprom 0 *0010 address to get:

~# ethtool -e eth1 | grep 0x0010 | awk '{print "0x"$13$12$15$14}'
0x100f8086

The offset value:

[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 

Give an example:

Command to modify eth0 MAC address:

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

 

Script:

#!/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 '{print "0x"$13$12$15$14}')
ethtool -E $1 magic $dev offset 0xAA value 0xBB

0xAA Namely eeprom position
0xBB That's the new value.

 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

The script is:

  #!/bin/bash

  echo "setmac to $1"
  numbers=(`echo $1 | tr ':' ' '`)
  # 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]}

The setMAC script is called by px2init during Initial File System Configuration.

Keywords: Mac network sudo

Added by Sarahlmorrow on Thu, 29 Aug 2019 12:43:15 +0300