18

QUESTION:

How might I be able to specifically change the Mac Address of the enp3s0 and wlp2s0 interfaces through the /etc/network/interfaces file? What code would I have to include inside? I have been trying for some time now without success sadly enough.


ELABORATING:

So I found this great article online explaining how to change a Mac Address permanently through the /etc/network/interfaces file on my Ubuntu.

In the article, it says:

On Debian, Ubuntu, and similar systems, place the following in the appropriate section of /etc/network/interfaces (within an iface stanza, e.g., right after the gateway line) so that the MAC address is set when the network device is started:

hwaddress ether 02:01:02:03:04:08

Source: https://en.wikibooks.org/wiki/Changing_Your_MAC_Address/Linux

Now when I use the following code:

cat /etc/network/interfaces

I get the following output

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

And when I do ifconfig on my ubuntu, I get back 3 different interfaces:

  • enp3s0

  • lo

  • wlp2s0

I would like to change the mac address of all of my interfaces (enp3s0, wlp2s0) (lo is loopback so no need there), but I am unfamiliar with the commands in the /etc/network/interfaces file. I have been looking at tutorials online though I can't seem to get stuff right, and my computer even started acting very strangely a few times afterwards.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Webeng
  • 423

3 Answers3

25

Use the hwaddress ether inside your interface configuration block. Example:

auto enp3s0
iface enp3s0 inet static
    address 192.0.2.7
    netmask 255.255.255.0
    gateway 192.0.2.254
    hwaddress ether 00:11:22:33:44:55

or, if dhcp:

allow-hotplug enp3s0
iface enp3s0 inet dhcp
    hwaddress ether 00:11:22:33:44:55

A detail that i have missed: The hwaddress configuration item needs to be after the gateway stanza, if you are setting a static ip address.

Related stuff: Good detailed explanation of /etc/network/interfaces syntax?

However, if you are having problemas while changing mac through network/interfaces you could do it through udev

udev method - Create the file etc/udev/rules.d/75-mac-spoof.rules with the following content:

ACTION=="add", SUBSYSTEM=="net", ATTR{address}=="XX:XX:XX:XX:XX:XX", RUN+="/usr/bin/ip link set dev %k address YY:YY:YY:YY:YY:YY"

You could also do it using systemd units as explained here: Changing mac using systemd units. But at the end of the day, they are also only wrappers for executing ip link set and macchanger.

  • thanks for the post nwildner! I have 2 questions about the code above, would I replace eth0 with enp3s0 in this case? Also for wireless, do I just replace that same portion with wlps20? – Webeng May 22 '17 at 20:58
  • I tried out your code, and when I replaced it with enp3s0, it did in fact work! however when I then appended the same thing but with wlp2s0 replacing enp3s0 and changing the Mac Address to 00:11:22:33:44:56, I got the following error when doing sudo /etc/init.d/networking restart: [....] Restarting networking (via systemctl): networking.serviceJob for networking.service failed because the control process exited with error code. See "systemctl status networking.service" and "journalctl -xe" for details. – Webeng May 22 '17 at 21:19
  • @Webeng. Thats right. It is just a matter of changing the interface name to the one you have on your system, and restart the networking service. Do you have the journalctl -xe output to see what is happening? –  May 22 '17 at 23:11
  • The very first suggestion fits into my need, it allows me to have a fixed local IP address and a permanent MAC address. The second option didn't work in my case despite the presence of a DHCP in my residential router, probably because something wrong with udev but I didn't investigate. Actually, I don't see the interest of leaving the DHCP attribute the local IP address to a machine permanently connected to Internet, using "static" can make sense even when the DHCP works. – gouessej Oct 26 '19 at 10:49
3

To permanently change your MAC address, append the following line to your /etc/network/interfaces:

pre-up ifconfig <interface> hw ether xx:xx:xx:yy:yy:yy

NetworkManager will reset your MAC address during Wi-Fi scanning. To disable this, you may edit your /etc/NetworkManager/NetworkManager.conf as follows:

[main]
plugins=ifupdown,keyfile

[ifupdown] managed=false

[device] wifi.scan-rand-mac-address=no

[keyfile]

Then reboot.

There is some useful information about MAC address randomization during Wi-Fi scanning on the ArchWiki:

Configuring MAC Address Randomization

As of version 1.4.0, NetworkManager supports two types MAC Address Randomization: randomization during scanning, and stable randomization. Both modes can be configured by modifying /etc/NetworkManager/NetworkManager.conf

Randomization during Wi-Fi scanning is enabled by default starting on version 1.2.0, and it can be disabled by adding the following lines to /etc/NetworkManager/NetworkManager.conf:

[device] wifi.scan-rand-mac-address=no

GAD3R
  • 66,769
  • Hi GAD3R thank you for the response! I tried including your previous code in the /etc/network/interfaces file. The result was I did infact have the mac address changed, however only when I wasn't connected to a connection. Whenever I connected to a wifi, it reverted back to the permanent Mac Address. When I disconnected from the wifi connection, it would go back to the new Mac Address xx:xx:xx:yy:yy:yy but when I connect again, it used the permanent again. – Webeng May 22 '17 at 21:06
  • @Webeng Please reboot then test it – GAD3R May 22 '17 at 21:37
  • 1
    Rebooted and same result. The Mac Addresses are changed as long as I'm not connected to a wifi connection. When I connect to one, the permanent Mac Address appears again when running ifconfig – Webeng May 22 '17 at 21:48
-4

The MAC address is registered in its wifi device, and it cannot be changing to another MAC address usually.
So if you change MAC address, then you have to use another wifi device inevitably.
As you mentioned in the comment that re-appearing the same MAC address after
rebooting, the OS reads out MAC address from wifi device simply.
This is normal behavior.

By the way, in the virtual environment, you can freely setup MAC address.

  • 2
    This is how it was planned a long time ago. My experience is that embedded devices that use u-boot (eg. all android phones) always have set the MAC from the operating system (or generate random at boot) and thus it's not fixed. Linux too has got ways to set the MAC address, so I believe the plan to bind MAC to hardware is totally obsolete today. – JPT Nov 20 '19 at 11:48
  • This answer is really wrong, but the last sentence of your comment is as well. After all, MAC addresses should be unique world-wide. With hardware-bound MAC addresses, it is easy to achieve that. With randomly generated MAC addresses, you can't achieve it reliably because MAC addresses only have 48 bits. At least, you would need tricky algorithms. For that reason, HW-bound MAC addresses are not dead in any way. Instead, OSes use them as default MAC addresses in their network stacks (but allow to overwrite them if necessary). – Binarus Oct 01 '22 at 09:14