2

macchanger automatic script doesn’t work.

I searched here before I asked and all I found was to run macchanger in /etc/rc.local with this simple script:

ifconfig enp2s0 down
macchanger -r enp2s0
ifconfig enp2s0 up

(I just modify eth0 cuz my device name is enp2s0)

Clearly it only setup a random mac address at startup but if I reconnect my network device it always get my real mac address so it isn’t what I was looking for.

I tried to at least leave a permanent spoofed address but the mac spoof option in the ubuntu default network manager doesn’t do anything, it always sent my real address.

I can’t remember exactly where but I read somewhere that it could be systemd's fault but I'm not really sure how to handle that thing or if really is its fault.

What's the right way to get a random mac address whenever I connect my network device in Ubuntu? I'm running out of ideas.

  • Since macchanger is not working as the developer clearly intended, did you file a bug report? – user535733 Nov 14 '17 at 17:43
  • 1
    I was not aware but here says it does not work in Jaunty (9.04). So probably it doesn't work since then? – qcnrexjj Nov 14 '17 at 18:12
  • It probably doesn't work since $IFACE doesn't return anything. – qcnrexjj Nov 14 '17 at 18:38
  • @charles-green I'm using ethernet, the only options I could find concerning MAC addresses were: 802-3-ethernet.mac-address: *MY REAL MAC ADDRESS* 802-3-ethernet.cloned-mac-address: -- 802-3-ethernet.mac-address-blacklist: according to the article I tried to put stable in 802-3-ethernet.cloned-mac-address but I got an error saying that stable isn't a valid Ethernet MAC address :( – qcnrexjj Nov 14 '17 at 19:15

2 Answers2

4

Working from the Gnome blog post. These settings for for Ubuntu 17.10

Via CLI

First, obtain the name of the wired internet connection

nmcli connection show

Secondly, set the enternet connection to generate either a "stable" or "random" address

nmcli connection modify "wired_connection_name" ethernet.cloned-mac-address random

For WiFi use wifi.cloned-mac-address. On network-manager 1.2.6 you'll need to set a MAC address instead of random.

Finally, check your mac address, bring the connection down and up, and check your mac address again

ifconfig | grep Ether
nmcli connection down "wired_connection_name"
nmcli connection up "wired_connection_name"
ifconfig | grep Ether

Via GUI

the program nm-connection-manager can also be used to access this setting, bringing up a screen like the one shown below, where the drop-down box for 'Cloned MAC Address' can be changed to one of several settings.

enter image description here

Pablo Bianchi
  • 15,657
Charles Green
  • 21,339
  • This seems to be the only solution but as I mentioned, I'm getting an error when modifying ethernet.cloned-mac-address to random it always tells me that random is not a valid MAC address.

    I think it might be because the version that Ubuntu 16.04 have is the 1.2.6.

    – qcnrexjj Nov 14 '17 at 20:04
  • what version of network manager are you running? apt list network-manager – Charles Green Nov 14 '17 at 20:06
  • @hola - you can peruse the nm-settings man page, and look for the cloned-mac-address in the ethernet section to see if it's valid for your version of network-manager. It's way,way, way down in the list of possible settings. – Charles Green Nov 14 '17 at 20:17
  • Looks like I can only put a MAC address in there :/ – qcnrexjj Nov 14 '17 at 20:27
  • @hola I'd have to run up a 16.04 VM, and poke at it's connections to try this. Would take a while as I'd also have to download the iso first... – Charles Green Nov 14 '17 at 20:30
  • Looks like that's the terminal way to spoof the mac address for the Ubuntu default network manager. I just try to spoof with a random generated one using:nmcli connection modify "Conexión cableada 1" ethernet.cloned-mac-address CD:86:CB:35:C5:B0 It's here now but like I said, it doesn't work for some reason... – qcnrexjj Nov 14 '17 at 20:38
  • @Hola Perhaps it is just the newer version of nmcli that I'm running. I used nmcli connection modify "Conexión cableada 1" ethernet.cloned-mac-address random and it works quite well – Charles Green Nov 14 '17 at 20:40
  • I'm tempted to update... I can't think of a better solution and this would be the ideal. (at least for Gnome users) But recent Gnome versions have a really really weird breaklines in Spanish file names. They bother me too much. Anyway, thank you very much for your help. – qcnrexjj Nov 14 '17 at 21:33
  • @Hola Try it in a VM first, and see if they've fixed the breaklines – Charles Green Nov 14 '17 at 21:34
  • On NM 1.2.6 isn't a dropdown. Maybe this or this bugs also have something to do. This one-line works also for me: sudo sh -c 'i="wlp2s0" && macchanger $i && systemctl stop network-manager && ifconfig $i down && sleep 5 && macchanger -r $i && sleep 5 && ifconfig $i up && systemctl start && network-manager && macchanger $i'. – Pablo Bianchi Feb 10 '18 at 21:10
0

We can achieve this goal by a simple script that uses ifconfig.

Create executable script file in the directory /etc/network/if-up.d, thus the script will be executed each time when the command ifup is executed, including at system startup. Let's call the script mac-changer:

sudo touch /etc/network/if-up.d/mac-changer
sudo chmod +x /etc/network/if-up.d/mac-changer
sudo nano /etc/network/if-up.d/mac-changer
  • Copy the below script content and use in nano: Shift+Insert for paste; Ctrl+O and Enter for save; Ctrl+X for exit.

The content of the script mac-changer should be:

#!/bin/bash

change_mac(){
        # Prevent enless loop on error
        if (( COUNT >= 10 )); then exit 0; else COUNT=$(( COUNT + 1 )); echo "Attempt: $COUNT"; fi
        # Generate a new MAC address
        MAC="$(echo $RANDOM | md5sum | head -c 17 | sed -r 's/(..)./\1:/g')"
        # Change the MAC address of the target network interface
        /sbin/ifconfig "$1" hw ether "${MAC}"
        # Chech whether the MAC is successfully changed anf if is not call the function again
        if [ $? -ne 0 ]; then change_mac; fi
        # Log the change
        echo "Interface: $1 | MAC: $MAC"
}

# Call of the function; Change 'enp0s25' with the actual interface in use; Create a log file `/tmp/mac-changer-...`
change_mac 'enp0s25' > /tmp/mac-changer-enp0s25 2>&1

Notes:

  • In most cases, if you want, you could execute the script directly by sudo and the MAC address will be changed:

    sudo /etc/network/if-up.d/mac-changer
    
  • Please note some network interfaces do not support the MAC change feature.

Here is the demonstration of that, how the script works:

enter image description here

References:

pa4080
  • 29,831
  • Whenever I run:

    sudo /etc/network/if-up.d/mac-changer

    I get "Impossible to solve the host" but looks like now my address persists even when I restart the network. I'll try again on a VM.

    – qcnrexjj Nov 14 '17 at 23:53
  • Hi, @Hola, it is strange. The script shouldn't produce any messages, because everything is redirected to a log file - > /tmp/mac-changer.... Where did you get this message into the terminal or into the log file? What exactly is the message - "Impossible to solve the host" or "sudo: unable to resolve host "? – pa4080 Nov 15 '17 at 17:02