Ubuntu wlan0 random MAC address changer

Run this shell script with (sudo) root permissions to change your wlan0 MAC address to a randomly generated one. If you want to change your eth0 address, just replace all wlan0′s with eth0′s.


Direct download: randomac.sh

Please post your suggestions and improvements below!

TODO:
* accept interface as parameter
* accept custom (not random) mac as parameter

#!/bin/bash --
#
# Random MAC adress changer
# http://www.360percents.com
#
#

if [ "$UID" -eq "0" ]
then
	oldmac=`ifconfig -a | grep HWaddr | grep wlan0 | awk '{print $NF}'`
	echo "Old mac adress: $oldmac"
	newmac=`echo $RANDOM$RANDOM | md5sum | sed -r 's/(..)/\1:/g; s/^(.{17}).*$/\1/;'`
	sudo ifconfig wlan0 down
	sudo /etc/init.d/networking stop > /dev/null
	sudo ifconfig wlan0 hw ether $newmac
	sudo /etc/init.d/networking start > /dev/null
	sudo ifconfig wlan0 up
	sudo dhclient
	echo "New mac adress: $newmac"
else
  echo "You need to be root to run this script. run: sudo $0"
fi

2 thoughts on “Ubuntu wlan0 random MAC address changer

  1. This is great idea. I changed all wlan0 to eth0

    Had to renew the lease otherwise network unreachable:

    sudo dhclient

    I want to run this as a startup programme, or via cron. Is there any way to not run it as root?

    • Under Debian based systems (eg Ubuntu), you can use “macchanger” (in the repos), which has “random” (with and without changing vendor) and “custom” options. It would be best to put this in the initramfs to be run before the network comes up, otherwise you’re leaking your information. Not exactly sure how to do this, but if someone knows, speak up!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>