How to change MAC address permanently on Linux

This article explains how to permanently change/spoof the MAC address of a network interface. For this we will use Macchanger, A utility for processing MAC addresses, we will create a systemd unit for it to automatically start it at startup.

Macchanger is the tool that will be used to change the MAC address in this article. It has a service asking if you want to change the MAC every time the computer starts, but this does not work on my Ubuntu 18.04. Since any changes made by Macchanger are reset when the system is rebooted, this article contains instructions on creating a systemd unit to automatically run Macchanger when the Linux computer starts, and change the MAC address each time. To use this guide, your Linux system needs to use systemd obviously.1. Install Macchanger
Macchanger should be located in the repository of major Linux distributions. To install it in Debian/Ubuntu/Linux Mint, use:

sudo apt install macchanger

2. Find out the network interface for which you want to change the MAC address
For this, you can use the following command:

ifconfig -a

If the system you are using no longer works, you can run the following command:

ip link show

Your network interface should now display as follows:

  • For ifocnfig -a:
$ ifconfig -a
enp10s0: flags=4163  mtu 1500
        inet 192.168.1.211  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe60::cc24:29cf:2c1:1c5a  prefixlen 64  scopeid 0x20
        ether 1d:21:da:ab:1d:71  txqueuelen 1000  (Ethernet)
        RX packets 164861  bytes 215658240 (215.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45118  bytes 8577639 (8.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 265  bytes 20133 (20.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 265  bytes 20133 (20.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4098  mtu 1500
        ether 1e:14:57:1c:66:11  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • For ip link show:
$ ip link show
1: lo:  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: enp10s0:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 1d:21:da:ab:1d:71 brd ff:ff:ff:ff:ff:ff
3: wlp3s0:  mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 1e:14:57:1c:66:11 brd ff:ff:ff:ff:ff:ff

Here, the wired network interface is enp10s0 The wireless network interface is wlp3s0 (Previously, eth0, 1 etc. for the wired interface, and wlan0, 1st class wireless). Make a note of the network interface 3 whose MAC address is to be spoofed. Check if Macchanger is actually working on your system
Before creating a systemd unit to automatically change the MAC address on every reboot, check if Macchanger can actually change your MAC address (I have seen cases where it does not work for some reason). To temporarily change your MAC address (after the system restarts, the changes will be restored), please run Macchanger as follows:

sudo macchanger -r NETWORK-INTERFACE

NETWORK-INTERFACE Is the network interface whose MAC you want to change, as listed in step 2 of this article (for example, enp10s0, wlp3s0).
If Macchanger is valid, then -r The option should change the MAC to a random MAC address, and the command should output the original MAC address and the new MAC address 4. Create a systemd unit to automatically run Macchanger every time the system starts (so the MAC address changes every time the system starts)
We will create /etc/systemd/system/[email protected] Systemd unit file and open it as root using a text editor:

sudo touch /etc/systemd/system/[email protected]
gedit admin:///etc/systemd/system/[email protected]

If you have not installed Gedit, please replace it with another text editor installed on your system in the above command.
Paste the following into [email protected] file:

[Unit]
Description=changes mac for %I
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
ExecStart=/usr/bin/macchanger -r %I
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

System unit use macchanger -r Change the MAC. -r Set a completely random MAC address. You can change -r to -e Change the MAC but keep the original NIC vendor bytes, -a Set up random supplier MACs of the same type, and so on. You can view all available options by running the following command:

macchanger --help

You can also set a custom non-random MAC address. For this, please change ExecStart=/usr/bin/macchanger -r %I Go to something like this:

ExecStart=/usr/bin/macchanger --mac=XX:XX:XX:XX:XX:XX %I

Replace XX:XX:XX:XX:XX:XX Use the new MAC address 5. Enable Macchanger systemd service
Now it’s time to enable the systemd service so that it starts at boot time. For this, use:

sudo systemctl enable [email protected]enp10s0.service

Replace enp10s0 Use the network address from step 2.
You can use the same command to enable the function of changing the MAC addresses of multiple network interfaces. 6. Restart the system
Your network interface should now have a new MAC address. For how to check the old (original) and new MAC addresses, see below.

How to check the original and new MAC addresses

You can use Macchanger to find out the original MAC and the new MAC address by running the following command:

macchanger -s NETWORK-INTERFACE

Where NETWORK-INTERFACE Is the network interface you found by using the command in step 2.
example:

$ macchanger -s enp10s0
Current MAC:   1d:21:da:ab:1d:71 (unknown)
Permanent MAC: 72:ab:3d:89:88:88 (Intel Corporate)

How to use systemd to disable changing MAC on every reboot

To undo the changes, first disable the system’s MAC changer service:

sudo systemctl disable [email protected]enp10s0.service

Replace enp10s0 Use the network address from step 2 (from the initial setup instructions). Do this for each network interface for which the service was previously enabled.
Now, you can delete the MAC changer systemd service file:

sudo rm /etc/systemd/system/[email protected]

Source

Sidebar