How to randomize the WiFi MAC address on a Macbook
Sometimes it can be useful to change the MAC address of a laptop’s WiFi interface to a new, random one. (For example when testing a WiFi system or to extend the free usage time on a public hotspot).
On macOS, the MAC address of an interface can get changed via ifconfig
:
ifconfig <interface> ether <new mac address>
We can incorporate this into a bash shell alias, so that running the command newmac
in a Terminal is all we need to do to quickly give the WiFi interface a new, random MAC address.
1.) Register the shell alias
Assuming that our WiFi interface is en0
(the default on all Macbooks that I’ve seen so far), under our regular user, we run:
cat >>$HOME/.bash_profile <<EOF
alias newmac="sudo /sbin/ifconfig en0 ether \\\`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'\\\`"
EOF
2.) Optional: Suppress sudo password prompt
In a new Terminal session it should now be possible to run our newly created alias command newmac
. There is one annoyance though: It prompts for the user’s admin password. We can suppress that by setting a rule in the sudoers config.
To set up the exception, we elevate our shell with sudo su
and run as root:
cat >/etc/sudoers.d/newmac <<EOF
Cmnd_Alias CMD_NEWMAC = /sbin/ifconfig en0 ether *
ALL ALL=NOPASSWD: CMD_NEWMAC
EOF
3.) Usage/Testing
Let’s test if it’s working. In a new Terminal session:
(Be aware that running newmac
will shortly disconnect any active WiFi connection).
$ ifconfig en0 | grep -i ether
ether f6:21:56:35:69:c3
$ newmac
$ ifconfig en0 | grep -i ether
ether 14:2e:a0:eb:de:30
Restore the hardware MAC address
The soft-changed MAC address is only active until the next reboot.