How To Permanently Change The MAC Address On Linux

This article explains how to permanently change / spoof the MAC addresses of network interfaces. For this purpose, we'll use Macchanger, an utility for manipulating MAC addresses, for which we'll create a systemd unit to start it automatically on boot.

Macchanger tool

Macchanger, the tool that we're going to use in this article to change the MAC address, has a service that asks if you want it to change your MAC each time your computer boots, but this fails to work for me on Ubuntu 18.04. Since any change made by Macchanger resets when you reboot the system, this article includes instructions for creating a systemd unit to automatically run Macchanger when your Linux computer boots up, changing the MAC address each time.

To use this guide, your Linux system needs to use systemd, obviously.

1. Install Macchanger

Macchanger should be in the repositories for 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 you're using a system where this no longer work, you can run this command instead:

ip link show

Your network interfaces should now be displayed like this:

  • 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 and wireless network interface is wlp3s0 (previously, eth0, 1, etc. was used for wired interfaces, and wlan0, 1, and so on for wireless). Note down the network interface for which you want to spoof the MAC address.

3. Check if Macchanger actually works on your system

Before creating the systemd unity to change the MAC address automatically on each reboot, check to see if Macchanger can actually change your MAC address (I've seen cases in which it doesn't work for some reason). To temporarily change your MAC address (the change is reverted after a system reboot), run Macchanger as follows:

sudo macchanger -r NETWORK-INTERFACE

NETWORK-INTERFACE is the network interface for which you want to change the MAC, as listed by step 2 in this article (e.g. enp10s0, wlp3s0).

If Macchanger works, the -r option should change the MAC to a random MAC address, and the command should output the original and new MAC addresses.

4. Create a systemd unit to run Macchanger automatically each time the system starts (so the MAC address changes each time your system boots up)

We'll create the /etc/systemd/system/changemac@.service systemd unit file and open it as root with a text editor:

sudo touch /etc/systemd/system/changemac@.service
gedit admin:///etc/systemd/system/changemac@.service

If you don't have Gedit installed, replace it in the command above with another text editor that's installed on your system.

Paste the following in the changemac@.service 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

The systemd unit uses macchanger -r to change the MAC. -r sets a fully random MAC address. You can change -r to -e to change the MAC but preserve the original NIC vendor bytes, -a to set random vendor MAC of the same kind, and so on. You can see all the available options by running:

macchanger --help

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

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

Replacing XX:XX:XX:XX:XX:XX with the new MAC address.

5. Enable the Macchanger systemd service

It's now time to enable the systemd service so its starts on boot. For this, use:

sudo systemctl enable changemac@enp10s0.service

Replacing enp10s0 with the network address from step 2.

You can use the same command to enable changing the MAC address for multiple network interfaces.

6. Reboot the system

Your network interface(s) should now have a new MAC address. For how to check the old (original) and new MAC address, see below.

How to check the original and new MAC addresses


Macchanger can be used to find out the original MAC and new MAC address, by running this command:

macchanger -s NETWORK-INTERFACE

Where NETWORK-INTERFACE is the network interface you found out by using the commands from 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 disable changing the MAC on each reboot with systemd


To undo the changes, start by disabling the systemd MAC changer service(s):

sudo systemctl disable changemac@enp10s0.service

Replacing enp10s0 with the network address from step 2 (from the initial setup instructions). Do this for each network interface for which you previously enabled the service.

Now you can remove the MAC changer systemd service file:

sudo rm /etc/systemd/system/changemac@.service

2 comments: