Linux – Change Mac Address permanently inside /etc/network/interfaces

interfacelinuxmac addressnetwork-interface

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.

Best Answer

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.

Related Question