Random MAC Address Generator

Nov 10, 2010 - 21 Comments

Terminal in OS X Do you want to generate a random MAC address? That’s no problem with this neat openssl tip sent in by one of our readers, and you can run the command once or multiple times to generate a randomized MAC address instantly on each execution. This command will work to randomize MAC addresses in in Mac OS X, Linux, and just about anything else with openssl and sed installed.

How to Generate a Random MAC Address from the Command Line

To get started, jump to your terminal or command line window, and paste the following syntax into the command line to generate a randomized MAC address:

openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

Be sure that command syntax is pasted onto a single line. The advantage to this trick is that it’s fairly simple, short, sweet, and doesn’t require any third party utilities or scripts, it works in OS X and Linux as is.

The hexadecimal output will be the generated MAC address, and will look something like this: 07:e0:17:8f:11:2f

If you want to generate a new address, just hit the UP arrow on the keyboard, then hit RETURN again, this will re-execute the same command in just about any modern shell. Unless you know what you’re doing, you probably won’t want to edit the command syntax itself otherwise you could wind up generating inaccurate or incomplete hexadecimal that may not match a potential MAC address. Keep it simple, stick with the following syntax if you’re not sure:

openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

Set Up an Alias for Randomizing MAC Addresses

If you plan on using this often, consider setting up an alias in your .bash_profile or .profile so that you don’t have to type out the entire command string, simply placing an alias can be done like so:

alias randommacaddy="openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'"

This is pretty helpful if you need to create a new MAC address for something like your router or cable modem. Of course you can also spoof your MAC address rather easily in Mac OS X if you want to use one of the generated addresses for the purpose of changing one too.

This command has been tested to work in Linux and Mac OS X, with virtually every version. The only requirement is that the command line has openssl and sed.

Terminal in OS X

Thanks to Akili for sending in this excellent little trick, if you know of any other ways to quickly generate a randomized MAC address, just let us know in the comments!

.

Related articles:

Posted by: David Mendez in Command Line

21 Comments

» Comments RSS Feed

  1. Renatolkl says:

    This syntax is not working.

    I am using macOS Sierra 10.12.5 and get following error:
    openssl rand -hex 6 | sed ‘s/\(..\)/\1:/g; s/.$//’
    unable to write ‘random state’
    d9:60:b2:46:f5:ad

    What could the problem be?

  2. justin says:

    I can see that it does generate a mac address with openssl rand -hex 6 | sed ‘s/\(..\)/\1:/g; s/.$//’

    But it doesn’t add it as the mac address. I would verify and nothing actually changes. Reason I’m asking is that I would love to have a script run to change the mac address each time i reboot. Thanks for the help!

  3. Nick says:

    HI there,
    I have a virtual machine (VMware) installed on my Macbook Pro . the MAC address on OS X appears to be a digit different at the end when compared to the MAC address on the VM.

    Would this be classed as a ‘unique’ MAC address so the likes of Amazon can’t link my machine if I was to use the VM to log in ?

    Thanks guys !

    • tgi says:

      Sites like Amazon can not detect a MAC address unless you give them access to something on the local machine, through Java, an application, Flash, or something similar. If you are concerned about it, spoof the MAC address in the VM.

      Typically things will track through cookies, browser fingerprints, history, IP address, etc. IP and cookie are the two primary methods of linking a computer to a user.

  4. Bob Joe says:

    Doesn’t work. I’m getting error
    -bash: syntax error near unexpected token `(‘

    • MAC says:

      Works fine, enter the syntax properly. I just ran it in OS X EL Capitan and Ubuntu Linux to confirm.

      openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

      Generates a random MAC address no problem, no syntax errors.

      • Gaiax says:

        how you guy would edit the syntax if you’d want to generate all posibles mac address from a vendor with prefix added on the synthax and store them into a txt file ?

  5. x says:

    The one from Holger is correct:
    printf ‘%02x:’ $(( 0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)) ; od /dev/urandom -N5 -t x1 -An | cut -c 2- | sed ‘s/ /:/g’

  6. Hidayat says:

    I am change mn my mac address but my computer not conectinge in conecting other wi-fi.in conctinge other computer this wi-fi.
    in my password is carickd.

  7. Hidayat says:

    How to change mac address in nokia n8?

  8. Holger says:

    And with bash calculation use this:

    printf ‘%02x:’ $(( 0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)); \
    od /dev/urandom -N5 -t x1 -An | cut -c 2- | sed ‘s/ /:/g’

  9. […] 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 […]

  10. waffletower says:

    The ruby snippet was broken. I don’t know ruby. I love perl though:

    perl -e ‘printf “\n%02x:”, $x=(int(rand(128))*2); for($i=0;$i<4;$i++){printf "%02x:",$x=int(rand(256));}printf "%02x", $x=int(rand(256));printf"\n\n";'

  11. […] that value returned from ifconfig to another hex value in the format of aa:bb:cc:dd:ee:ff. You can generate a random one if need […]

  12. […] note that the MAC address above is not the real one of my drives but a generated fake, for the sake of understanding the […]

  13. Ben Stoltz says:

    ruby -e ‘print (“%02x”%((rand*64).to_i*4|2))+(0..4).inject(“”){|s,x|s+”:%02x”%(rand*256).to_i} + “\n”‘

    The result is a locally administered, non-multicast MAC address.

  14. Matt says:

    You need to be careful that this does not generate multicast mac addresses, as these are technically illegal as source macs. The strict definition of a multicast mac address is one where the least significant bit of the first byte is set to 1. So if the first octet’s LSB is 1 (01, 03,05, a1, etc) you technically have a multicast mac source. See http://en.wikipedia.org/wiki/MAC_address.

    Using multicast src macs might not cause immediate connectivity problems, but certainly has implications for switches learning the mac address preventing unicast flooding, and routers allowing arp to resolve.

    Cisco, for example, will not allow mac learning if the source is non-unicast, and Cisco routers will not install arp entries for multicast macs to unicast ip addresses.

    I think you need to rethink this post taking into consideration these consequences.

  15. first says:

    I always just concoct random hex strings in my head, this is a bit less brain work so thanks

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