5

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: Changing Your MAC Address/Linux -WikiBooks

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.

5
  • 1
    Assuming wlp2s0 is your wifi, I think you may want to see the "Cloned MAC Address" feature inside NetworkManager to change the advertised MAC address of your wifi device - same for your Ethernet
    – Thomas Ward
    May 22, 2017 at 11:20
  • @ThomasWard thanks for the reply thomas, I actually did just that previously but found out it wasn't premanent (which is what I needed). Hence the reason why I'm looking for the previous solution. I am trying out Jos's solution now. I did however see you commented that I would be unable to connect to the internet through the GUI. Could you elaborate on how I would have to connect to the internet with the changes in place?
    – Webeng
    May 22, 2017 at 11:29
  • Painfully if wireless connection, less painfully if wired. I will elaborate in my own answer after I am at a computer - typing into the SE mobile app is evil.
    – Thomas Ward
    May 22, 2017 at 11:40
  • @ThomasWard haha alright thank you! Sadly my case is wireless. I'm still trying to get Jos's previous code to work as well.
    – Webeng
    May 22, 2017 at 11:42
  • I've posted my answer. It's a very long answer, and I apologize for that, but it's mostly complete explaining the headaches I mentioned, and links to where better information is, as I'm on a mini-time-crunch and won't have time yet today to copy in all the steps.
    – Thomas Ward
    May 22, 2017 at 12:13

3 Answers 3

5

First, the guide you linked to. It's a nice guide. Sadly, it also makes some headaches for different types of connections. And normally, /etc/network/interfaces is left alone except for the loopback interface in favor of Network Manager managing your connections.


There's a few reasons for this, mostly in that configuring wireless via /etc/network/interfaces will be painful if you switch wifi networks as you have to edit the configuration and reload it manually (and sometimes it won't load until you reboot). It's also extremely painful in that you have to hard-code connection data into wpa2-supplicant - wireless network name, password (in plain text), sometimes even the wifi band and the channel number. All of which is painful as you have to change it to switch to a different network.

I did mention that using /etc/network/interfaces will prevent Network Manager from working for handling your connections. The way Network Manager works is that it takes 'ownership' of the devices that are network devices not defined in /etc/network/interfaces and updates the configurations for each on the fly or with predefined connection profiles from older connections made in the past. If you decide to go the /etc/network/interfaces route you will have to make adjustments to how you connect to the Internet, and I give some descriptions for wifi connections this way below. And a more useful link.


Ethernet is not so bad. Ethernet can work pretty well actually with this, because you can simply set up Ethernet as such for a DHCP'd connection (dynamically assigned IP address, not a static config; note I use 'eth0' here instead of your actual device here to be more understandable):

auto eth0
iface eth0 inet dhcp
    hwaddress ether 01:23:45:67:89:0A

The only headache is if you have static IP addresses and not DHCP handled ones. But it's still pretty simple.


On wireless, however, it's not as pretty. It's a mess of steps, usually involving iwlist, /etc/network/interfaces configuration, advanced configuration of wpa2_supplicant, and otherwise. You'd ultimately end up with something looking like this when you're done, for a standard residential-grade shared-password network, but that only works for your one wireless network:

auto wlan0
iface wlan0 inet dhcp
    hwaddress ether 00:11:22:33:44:55
    wpa-ssid WIFI_NAME
    wpa-psk HASHED_PASSWORD

It gets even more complex when you need to switch networks, or use an Enterprise grade connection with an authentication backend (called WPA2-EAP or WPA2 802.1x Enterprise, or other names), of which I am not going to detail here.

You'll also note I didn't copy the steps in for how to get the hashed password. That's my bad, I'm on a time crunch to get to work, but this is all in the wiki linked in the next paragraph (from Debian, but it works).

The Debian Wiki on Wifi and How to Use It on the Command Line is where most of this information came from for the wireless section here, and having personally had to set this up on two Raspberry Pi devices manually because they're headless, I can attest that this is the most painful approach to setting up wireless.


There's a second element here: network device MAC addresses are hardcoded into the device. You can't ever 'permanently' change the MAC address that a device has. What /etc/network/interfaces, MAC Changer, and Network Manager (via the 'cloned MAC address' function) do is they temporarily change the Ethernet address advertised, but they don't change the core hardware address. You can make it work with Network Manager, but you have to configure it manually for each wifi network you use. MAC Changer, similar situation, it needs to be run at boot time. /etc/network/interfaces does this at boot time, but you lose Network Manager management of devices.

1
  • Thank you for taking the time to post! I will be reading the link you provided and attempting it myself. I previously tried Mac Changer, using the sudo ifconfig eth0 hw ether xx:xx:xx:yy:yy:yy command, as well as with the nmcli command, though the issues I was having is that I first had to try to connect to a wireless connection and then assign the cloned mac address individually. It didn't work as a "global" mac address so to speak. Out of curiosity is that the "expected" behaviour for these previous methods? Or is there a way to use them without leaking the permanent mac address?
    – Webeng
    May 22, 2017 at 22:11
3

Assuming that you don't use NetworkManager to configure your connections, I would think this will work:

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

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

iface wlp2s0
  hwaddress ether 02:01:02:03:04:09
3
  • 3
    Make a note that this will override the ability for NetworkManager to handle the wifi and ethernet connections, so they won't be able to connect anywhere anymore the "normal" GUI way.
    – Thomas Ward
    May 22, 2017 at 11:23
  • hi I noticed you changed the names of the 2 interfaces, from "enp3s0" to "enps3s0" and from "wlp2s0" to "wlps20", was this intentional or a typo?
    – Webeng
    May 22, 2017 at 11:37
  • Typos, my apologies. Fixed.
    – Jos
    May 22, 2017 at 12:38
0

This creates a systemd service that turns your network card off, changes your mac address, turns your network card on. This runs when your computer starts.

apt install macchanger -y

Create unitd service sudo vim /etc/systemd/system/[email protected] Change enp10s0 to be the name of your wireless card everywhere below. Change the mac address you want to use. Can find in ip a.

[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
ExecStartPre=/usr/bin/ip link set dev enp10s0 down
ExecStart=/usr/bin/macchanger -m 39:31:2E:60:10:6D enp10s0
ExecStartPost=/usr/bin/ip link set dev enp10s0 up
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Start it with sudo systemctl enable [email protected] Check if it's working with sudo systemctl status and sudo macchanger -s enp10s0

To debug: sudo systemctl status systemctl daemon-reload sudo systemctl restart https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .