
Hello everyone, I am the Intelligence Guy~
Modifying the MAC address is a common development requirement in network management and privacy protection.
1. Why Change the MAC Address?
For example, when we replace a damaged network card, we need to restore the original MAC address to maintain IP allocation, firewall policies, and device authentication; there are also virtualization management scenarios to avoid MAC conflicts caused by virtual machine cloning.
Of course, for privacy protection, changing the MAC address is often necessary, such as preventing device tracking in public WiFi scenarios to stop MAC-based behavioral monitoring (like in airports/cafes), and some ISPs impose time limits on the use of MAC addresses for free networks, you know what I mean.
Below, I will detail three methods for setting the MAC address in Linux: temporary setting, permanent setting based on systemd, and permanent setting based on SystemV.
2. Temporary Setting Method
This method is suitable for quick testing or temporary changes; the configuration will revert to the original address after a system reboot.
1. Using the<span>ip</span> Command
This is a commonly used command:
# Disable the network interface
sudo ip link set dev eth0 down
# Set new MAC address (example: 00:11:22:33:44:55)
sudo ip link set dev eth0 address 00:11:22:33:44:55
# Enable the network interface
sudo ip link set dev eth0 up
# Verify the result
ip link show eth0
2. Using the Traditional<span>ifconfig</span> Command
sudo ifconfig eth0 down
sudo ifconfig eth0 hw ether 00:11:22:33:44:55
sudo ifconfig eth0 up
ifconfig eth0
3. Permanent Setting Method (SystemV Systems)
This method is suitable for older systems using SystemV, such as CentOS 6.x and Debian 7.x.
Method 1: Modify Network Configuration File
- Edit the interface configuration:
# RHEL/CentOS
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
# Debian/Ubuntu
sudo vi /etc/network/interfaces
- Add MAC address configuration:
# RHEL Style
HWADDR=00:11:22:33:44:55
# Debian Style
hwaddress ether 00:11:22:33:44:55
- Restart the network service:
# RHEL/CentOS
sudo service network restart
# Debian/Ubuntu
sudo /etc/init.d/networking restart
Method 2: rc.local Startup Script
- Edit the startup script:
sudo vi /etc/rc.local
- Add before
<span>exit 0</span>:
ip link set dev eth0 down
ip link set dev eth0 address 00:11:22:33:44:55
ip link set dev eth0 up
- Add execution permissions:
sudo chmod +x /etc/rc.local
4. Permanent Setting Method (systemd Systems)
Currently, mainstream Linux platforms generally use systemd, suitable for modern distributions like Ubuntu 16.04+ and CentOS 7+.
Method 1: NetworkManager Configuration
# Set permanent MAC address
nmcli connection modify "Wired connection 1" 802-3-ethernet.cloned-mac-address 00:11:22:33:44:55
# Restart the network connection
nmcli connection down "Wired connection 1" && nmcli connection up "Wired connection 1"
Method 2: netplan Configuration (Ubuntu 18.04+)
- Edit the configuration file:
sudo nano /etc/netplan/01-netcfg.yaml
- Add the macaddress field:
network:
version: 2
ethernets:
eth0:
macaddress: "00:11:22:33:44:55"
dhcp4: true
- Apply the configuration:
sudo netplan apply
Method 3: systemd-networkd Configuration
- Create the configuration file:
sudo nano /etc/systemd/network/10-eth0.link
- Add the content:
[Match]
MACAddress=Original MAC Address
[Link]
MACAddress=00:11:22:33:44:55
NamePolicy=kernel database onboard slot path
- Restart the service:
sudo systemctl restart systemd-networkd
Pitfalls:
1. All modifications require root permissions.
2. Always disable the network interface before modification (for example:<span>ip link set dev eth0 down</span>).
3. MAC addresses are not arbitrary numbers; pay attention to MAC address standards.
4. After permanent modification, the network service or system needs to be restarted.
Finally
I have collected some embedded learning materials; reply with【1024】 in the public account to find the download link!
Recommended Articles Click the blue text to jump
☞ Collection | Comprehensive Programming of Linux Applications
☞ Collection | Learn Some Networking Knowledge
☞ Collection | Handwritten C Language
☞ Collection | Handwritten C++ Language
☞ Collection | Experience Sharing
☞ Collection | From Microcontrollers to Linux
☞ Collection | Power Control Technology
☞ Collection | Essential Mathematics for Embedded Systems
☞ Collection | MCU Advanced Collection
☞ Collection | Embedded C Language Advanced Collection