We’ve discussed this before and this approach leverages the wifi kill switch and there’s nothing here that macchanger doesn’t already do, but sometimes it’s better to have a script that “just does it” rather than a GUI (and a program that you might not want to install for whatever reason).
Before I introduce the script itself, allow me to recommend how to use it with your Librem laptop. Unfortunately, it doesn’t seem to work in crontab @reboot, so it’s kinda manual, unless you actually want to run it on a periodic basis (which might offend your router and cause problems).
-
Open a terminal and run ifconfig. Your wifi device will likely start with “w”, and your LAN connection with “l”. Probably you want to connect to a public wifi, so copy its name to the clipboard. Let’s assume that you want to change the MAC address of the device called DEVICE.
-
Arrive at the coffee shop with your laptop powered off and the wifi kill set to disable wifi (and Bluetooth, for that matter).
-
Boot up.
-
Open up a terminal and run the script:
macrandom.sh DEVICE
(This assumes that the script was marked as executable by chmod. If not, you can always prefix it with “/bin/bash” followed by a space.)
-
It will ask you for your password because it needs to run “ip” as root. Enter your password, but don’t hit Enter yet.
-
Turn on your radios via the kill switch. (I’m not sure how risky it is to have Bluetooth power back up, but by default, I don’t like it. Turn it back off, but not just yet, as you have more urgent matters to attend to.)
-
As fast as possible, so as to minimize the expression window for your hardcoded default MAC, hit Enter. If you get an error, hit the up arrow to repeat the script command and run it again. (It won’t ask for your password the second time because you’re already within the root authentication time window.) Do this again and again until the script succeeds (silently). (The retries are necessary because there’s a delay between the time that you enable the radios in hardware and the time that the devices become available.) You can verify the MAC change by doing ifconfig again.
The best method would be to this in a manner so as to ensure that the MAC is randomized before expression of the default is even possible, but I don’t know how to do that without digging into the driver layer.
In order to run the script in the first place, you need xxd, which is a hex dump utility for prettyprinting binary files. Try:
xxd --version
If that doesn’t work, then just install it in the usual way that you’d install any other package, for example:
sudo apt get install xxd
Finally, here’s the script. Note that it ensures that bits 40 and 41 of the 48-bit MAC are set to 0 because they’re special flags. The logic is awkward, but that’s business as usual in bash.
if (($# != 1)); then
echo Syntax: macrandomize.sh link-name-from-ifconfig
exit
fi
mac=""
flag=0
for i in {0..5}; do
byte=$(cat /dev/urandom|head -c1|xxd -i|tail -c3|head -c2)
if (( $i == 0 )); then
firstdigit=${byte:1:1}
bits=$(cat /dev/urandom|head -c1|xxd -b|cut -c-12|tail -c3|head -c2)
if (( $bits == "00" )); then
seconddigit="0"
elif (( $bits == "01" )); then
seconddigit="4"
elif (( $bits == "10" )); then
seconddigit="8"
else
seconddigit="c"
fi
byte="${firstdigit}${seconddigit}"
fi
mac="${mac}${byte}"
if (( $i < 5 )); then
mac="${mac}:"
fi
done
sudo ip link set dev $1 down
sudo ip link set $1 address ${mac}
sudo ip link set dev $1 up
The line containing “address” sets the actual address in the hardware. If you want to set a specific MAC address instead of a random one, you could do that in the same way.