I recently had an issue changing mac address on my MacBook Air. Here’s how to change mac address to a random one keeping first three digits (otherwise it won’t work).
tl;dr: $ networksetup -getmacaddress en0 | awk '//{"openssl rand -hex 3" | getline mac; sub("(:..){3}$", mac, $3); print $3}' | sed 's/://g; s/\(..\)/:\1/g; s/^://' | xargs sudo ifconfig en0 ether
At first find name of ethernet adapter you want to change (en0
in example). Nota bene: This command always shows initial mac address to which it will be reset if you restart your computer.
$ networksetup -listallhardwareports
Hardware Port: Wi-Fi
Device: en0
Ethernet Address: aa:bb:cc:dd:ee:ff
...
Then check your current mac address. Replace en0
with ethernet adapter of your choice. On MacBook Air & Pro it’s usually en0
for wireless adapter if there is no hardware ethernet port.
$ ifconfig en0 | awk '/ether/{print $2}'
aa:bb:cc:dd:ee:ff
To set mac address run following command. Replace 11:22:33
with hex characters of your choice. Important: first three digits (aa:bb:cc
in example) have to stay the same on macOS. Seems to be a security feature and wasted about an hour of my precious time 🙈
$ sudo ifconfig en0 ether aa:bb:cc:11:22:33
To make life a little easier you can generate and set random address with following command. Replace aa:bb:cc
with first three digits of your initial mac address (see command above).
$ openssl rand -hex 3 | sed 's/\(..\)/:\1/g; s/^/aa:bb:cc/' | xargs sudo ifconfig en0 ether
Adding even more magic we can read initial mac address, substitute last three digits with random generated, and set new random mac address in one command.
$ networksetup -getmacaddress en0 | awk '//{"openssl rand -hex 3" | getline mac; sub("(:..){3}$", mac, $3); print $3}' | sed 's/://g; s/\(..\)/:\1/g; s/^://' | xargs sudo ifconfig en0 ether
Note: Mac address set by this command will be reset to initial address at restart.