Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active May 16, 2024 03:54
Show Gist options
  • Save mbierman/03b2a962ac04963ef5bbc8354d0ed5d1 to your computer and use it in GitHub Desktop.
Save mbierman/03b2a962ac04963ef5bbc8354d0ed5d1 to your computer and use it in GitHub Desktop.
NOTE: Looks like Apple killed tis in Sonoma. There may be no viable workaround. If I find one, I will update this script. This allows you to create and assign any MAC address you like for macOS Wi-Fi or Ethernet connections. If you don’t supply one a random mac will be created for you.
#!/bin/bash
# v 1.0.2
# https://gist.github.com/mbierman/03b2a962ac04963ef5bbc8354d0ed5d1
# 2022 mbierman
sleep="${1:-5}"
regex="^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$"
getmac () {
rnd=$(openssl rand -hex 6 | sed 's/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5:\6/')
echo -e "What is the mac address want to set the mac\n\nexample: CA:49:63:9F:78:27\n\nLeave blank to get one generated for you."
read tmp
mac1=${tmp:=$rnd}
mac=$(echo $mac1 | tr '[:lower:]' '[:upper:]')
if [[ $mac =~ $regex ]] ; then
comp=true
echo -e "\n\n Using $mac...\n"
else
comp=false
echo false
fi
}
until [ "$comp" = "true" ]
do
getmac
done
getAdapter () {
echo "What is the adapter you want to set the mac for [en0,en1, etc. use 'ifconfig' if you don't know]"
read adapter
}
until [ "$adapter" ]
do
getAdapter
done
type=$(ifconfig $adapter | grep baseT)
if [ "$type" ]; then
echo "got Ethernet"
type="Ethernet"
else
echo "got Wi-Fi"
type="Wi-Fi"
fi
read -p "Is $adapter $type (y|n) ? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Exiting. Run again"
exit
fi
sudo ifconfig $adapter down && sleep $sleep && sudo ifconfig $adapter up && \
sudo ifconfig $adapter ether $mac && sudo networksetup -detectnewhardware
@phush0
Copy link

phush0 commented Sep 16, 2022

well script is working only with original adapter from Apple. If I try to change mac on the dock adapter it is not working. So it sounds like Apple fault.

@mbierman
Copy link
Author

@phush0 which one is failing? en8?

@mbierman
Copy link
Author

@FASHION399 please try the new version.

@phush0
Copy link

phush0 commented Sep 16, 2022

yep en8, I tried and Dell one on en9 with same fail result

@mrrobotbot
Copy link

still trying to get it, what's the difference between this simple action that worked couple days ago for me:

  1. disconnect from wifi network (do not turn off wifi adapter)
  2. openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
  3. sudo ifconfig en0 ether xx:xx:xx:xx:xx:xx
  4. connect back to local wlan

and this script???? o_0

@mrrobotbot
Copy link

the main difference I see is in openssl generator (syntax is a little bit different)

@acidos
Copy link

acidos commented Feb 13, 2024

This is the only script that worked for me on MacStudio Wifi / Sonoma. I will try on Macbook Pro.
Thanks a lot and please keep it alive.

@alii
Copy link

alii commented Apr 1, 2024

Thanks this is working great for en0 wi-fi!

@Brenex
Copy link

Brenex commented Apr 16, 2024

This script doesn't work for some people because when it runs "sudo ifconfig $adapter up", it auto-associates with a known SSID if a known SSID is found. The wifi adapter needs to be on and not associated for it to work. That's why the following step works, "disconnect from wifi network (do not turn off wifi adapter)".

@blueDesert
Copy link

It's not woking on MACOS sonoma 14.4.1 (23E224), MacBook Air, Apple M1.
It showed the error message: "ifconfig: ioctl (SIOCAIFADDR): Can't assign requested address"

@mbierman
Copy link
Author

This script doesn't work for some people because when it runs "sudo ifconfig $adapter up", it auto-associates with a known SSID if a known SSID is found. The wifi adapter needs to be on and not associated for it to work. That's why the following step works, "disconnect from wifi network (do not turn off wifi adapter)".

I don’t quite follow. Can you explain what you mean?

@mbierman
Copy link
Author

It's not woking on MACOS sonoma 14.4.1 (23E224), MacBook Air, Apple M1. It showed the error message: "ifconfig: ioctl (SIOCAIFADDR): Can't assign requested address"

What is the interface of your Wi-Fi adapter?

@blueDesert
Copy link

blueDesert commented May 13, 2024 via email

@blueDesert
Copy link

blueDesert commented May 13, 2024

Attached the screenshots:

img

unnamed

@mbierman
Copy link
Author

Looks like Apple killed this in Sonoma. There may be no practical workaround. If I find one, I'll update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment