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.

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
  • 17,371
Charles Green
  • 21,859
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
  • 30,621