Generate and Set a Random Valid MAC Address from the Command Line in OS X

May 2, 2012 - 5 Comments

Generate and set random MAC address in OS X

We’ve shown you how to generate MAC addresses randomly and then how to go about changing a MAC address in OS X Lion and OS X Mountain Lion, but why have those be two separate actions? Using the command line, you can combine the two events into a single action to generate a valid MAC and then set it immediately.

Launch the Terminal and paste the following onto a single line:

openssl rand -hex 1 | tr '[:lower:]' '[:upper:]' | xargs echo "obase=2;ibase=16;" | bc | cut -c1-6 | sed 's/$/00/' | xargs echo "obase=16;ibase=2;" | bc | sed "s/$/:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | tr '[:lower:]' '[:upper:]')/" | xargs sudo ifconfig en0 ether

There is no confirmation or output, the MAC address is set immediately which you can verify with the following command:

ifconfig en0 |grep ether

You will probably need to reconnect to the wireless router after issuing the command, and in some cases turn wi-fi on and off again.

If you intend on using this often, setting up an alias in .bash_profile would be a good idea to avoid having to cut and paste the massive block of text.

Thanks to osmium for the great tip

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

5 Comments

» Comments RSS Feed

  1. Hidayat says:

    how to change nokia n8 mac address

  2. Tiristore says:

    Can you explain me better (step by step…) hot to set up an alias and use .bash_profile? Do I have to do it with terminal?
    I’m really confused!
    Thanks

  3. Matt says:

    You should change ‘sed ‘s/$/00/” to ‘sed ‘s/$/10/” above so that the locally-administered bit is set. This is exactly what this bit is reserved for.

  4. MBH says:

    Here’s a more compact way to generate the MAC address:

    perl -e ‘printf “%02x:”x 5 .”%02x\n”, int(rand(64))*4, map {int(rand(256))}(1..5)’

    Be sure to keep the spaces around the first 5. You can add the trailing “| xargs …” from the other example to set your address.

  5. Shenbo says:

    I turned it into a bash script and tossed it into /usr/local/bin/ for easy access, couldn’t escape the characters properly to insert it into an alias. Thanks for the tip.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site