Change a MAC address in Linux with ip link, nmcli and macchanger
|

How to Change a MAC Address in Linux (ip, NetworkManager and macchanger)

Changing a network interface’s MAC address on Linux takes three commands with the ip tool, and the change is temporary: it normally lasts until a reboot, and something else can reset it sooner. If you want it to survive reboots, NetworkManager can set it per connection, and macchanger is a handy helper for random addresses. Older guides use ifconfig, which belongs to the deprecated net-tools package and is no longer installed by default on many distributions; ip from iproute2 is the current tool. I ran the ip commands below with iproute2 7.0.0 on a throwaway virtual interface, so the output is real. The NetworkManager and macchanger parts follow their documentation.

Use this only on machines and networks you own or are allowed to configure. Changing a MAC address is legitimate for testing, for lab work with virtual machines and for privacy on public Wi-Fi, but MAC filtering is easy to bypass and should not be relied on for access control, and you should not use a spoofed address to get onto a network you have not been given access to.

Before you start

  • Changing the MAC drops the network link briefly. If you are connected over SSH through the same interface, you will be cut off, so use the console or another interface.
  • A new MAC can break things that depend on the old one: DHCP reservations, MAC-based access lists and licences tied to a network card. Two devices with the same MAC on one network cause intermittent connectivity problems, so never reuse an address that is in use.
  • Write down the current address first so you can go back (ip -br link).

What a MAC address is

A MAC address identifies a network interface on its local network segment: six bytes, written as six hex pairs such as 02:11:22:33:44:55. Every Ethernet frame carries a source and a destination MAC. Manufacturers burn a unique “permanent” address into the hardware, but the operating system can present a different one. Two bits in the first byte matter when you choose your own address:

  • The lowest bit set to 1 marks a multicast address, so a normal interface address must have an even first byte.
  • The next bit set to 1 marks the address as locally administered, which is what you want for an address you made up. In practice, start with 02, 06, 0a or 0e.

Method 1: change it with ip (temporary)

First find the interface name. ip -br link prints a one-line summary of each interface:

ip -br link

Take the interface down, set the address, bring it back up, then check. Doing it in this order is the safe one for real network cards:

sudo ip link set dev eth0 down
sudo ip link set dev eth0 address 02:11:22:33:44:55
sudo ip link set dev eth0 up
ip -br link show eth0

Replace eth0 with your interface (enp3s0, wlan0 and so on). This is the output from the test interface, first while it was down before the change, then after setting the address and bringing it up:

dummy0           DOWN           c6:3f:1f:52:a1:4a <BROADCAST,NOARP>
dummy0           UNKNOWN        02:11:22:33:44:55 <BROADCAST,NOARP,UP,LOWER_UP>

(A virtual dummy interface shows the state UNKNOWN when up; a real card normally shows UP.)

On the virtual test interface the change also worked while the interface was up. Real network cards depend on the driver, and many refuse the change while the link is up, which is why the safe order is down, change, up.

The kernel, and most drivers, refuse addresses that are not valid for an interface. In my test, a multicast address (odd first byte, such as 01:00:5e:00:00:01) and the all-zero address were both rejected:

RTNETLINK answers: Address not available

An ordinary globally administered unicast address such as 00:11:22:33:44:55 was accepted. Choosing a locally administered address (first byte 02, 06, 0a or 0e) is a recommendation, not a kernel rule: it avoids clashing with a real manufacturer’s address range.

The change lasts until a reboot, or until the driver reloads or NetworkManager reapplies its own setting.

Method 2: make it permanent with NetworkManager

On most desktop distributions NetworkManager controls the interfaces (many servers use netplan or systemd-networkd instead, in which case set the address in that tool’s configuration), and it can clone a MAC address per connection. List your connections first:

nmcli connection show

Then set the cloned address on the one you use and reconnect. For a wired connection:

nmcli connection modify "Wired connection 1" ethernet.cloned-mac-address 02:11:22:33:44:55
nmcli connection up "Wired connection 1"

For Wi-Fi the property is wifi.cloned-mac-address. According to the NetworkManager documentation, the property also accepts special values instead of an address:

Value Meaning
preserve Leave the MAC address untouched when the connection activates (the default if nothing else is set)
permanent Use the hardware’s permanent address
random Generate a new random address each time the connection is made
stable Generate a hashed address that stays the same for this connection on this machine
stable-ssid (Wi-Fi only) Same idea, derived from the network name

For privacy on public Wi-Fi, random or stable is usually a better choice than a hand-picked address:

nmcli connection modify "CafeWiFi" wifi.cloned-mac-address random

To go back to the default behaviour, clear the property so the connection inherits NetworkManager’s defaults, then reconnect:

nmcli connection modify "Wired connection 1" ethernet.cloned-mac-address ""
nmcli connection up "Wired connection 1"

Set it to permanent instead if you want to force the hardware address. Setting preserve only leaves whatever address the interface has at that moment.

Method 3: macchanger

macchanger is a small utility that sets or randomises the address. On Debian and Ubuntu:

sudo apt install macchanger

The installer may ask whether to change the address automatically each time an interface starts; choosing No is the safe default. Take the interface down before using it, as with ip. These options come from the program’s manual page:

Command What it does
sudo macchanger -s eth0 Show the current and permanent address
sudo macchanger -r eth0 Set a fully random address
sudo macchanger -m 02:11:22:33:44:55 eth0 Set a specific address
sudo macchanger -p eth0 Reset to the original hardware address

Check that it worked

Besides ip -br link, you can read the address straight from the kernel:

cat /sys/class/net/eth0/address

If a DHCP server gives your machine an address based on its MAC, a new MAC normally means a new lease, so the IP address may change.

Common questions

How do I restore the original address?

Reboot (for the temporary methods), run sudo macchanger -p eth0, or set the NetworkManager property to permanent and reconnect.

Why did my change disappear?

Something reapplied the old setting: a reboot, a driver reload, or NetworkManager activating the connection with its own value. Use the NetworkManager method if the interface is managed by it.

Does it work for virtual machines?

Yes, and VM hypervisors usually let you set the virtual NIC’s MAC in their settings, which is cleaner than changing it inside the guest.

Sources

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *