Recently I copied a script from user deltaray to generate random local MAC addresses for virtual machines. I like this script because it's the easiest and most effective bash script for generating a MAC address.

However, all available private MAC addresses have four different OUI octets available for use, and my question is:

How to randomize the first set of octets for private MAC addresses and never have to worry about colliding with another VM?


Where the "x" in x2, x6, xA and xE are randomized.

I did modify deltaray's script for a static private MAC address, but I would like to be thorough and have the best solution, and not just a single solution.

My mods:

mac=$( for i in {1..10} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' )
echo 02$mac

Thank you

Please see the answer to my question below.

  • You're already randomizing 5 bytes, right? 2^40 is a lot of addressess. The odds of having a collision in that space are astronomically low, even if you're spinning up millions of new VM instances per second for decades. Is getting to 2^47 really worth additional effort?
    – Spiff
    Jan 29, 2017 at 2:36
  • I understand your point, and I get the math. The issue is about creating the best possible solution and forgetting about the need to monitor, administer or maintain records. Creating the new MAC takes milliseconds, while administering is far too time consuming and unnecessary.
    – ina2n
    Jan 30, 2017 at 23:18

2 Answers 2


Is there a reason the VM mac addresses need to be random (pseudo random actually)? Seems like you're asking the question of how to get a guaranteed maxium period in a sequence of numbers, before overlap. A simple increment would be the most obvious, but CRC's with maximum period for a allowed min/max range exist. but I don't think you would have a problem with sequential, so CRC would be unnecessary.

With maximum period, you just have to worry about whether a VM still exists, when you wrap. If that can happen (due to the lifetime of your VMs), then you have to keep a history of running VMs, and pick another choice until you don't hit on the history. Assuming you have a wider range available, than possibly still running VMs, then you wouldn't get overlap. It's unlikely though that you need a history (if you use a sequence with maximum period, like simple increment) ..I'm guessing about the lifetime of your VMs, and number, relative to the space you have available.

Assuming there are no other mac addrs you need to worry about colliding with, other than the ones you create yourself with this algorithm. Which may not be true.

  • Thank you Kevin. This is about maximizing possible MACs so there's not need to administer the VMs in your methodology. The last thing I have time for is trying to figure out which MAC addresses are assigned where and the time they expire.
    – ina2n
    Jan 30, 2017 at 16:33

I figured it out.

# This will generate every possible local MAC address available.  Works on any system that can run the bash shell.
local=$( echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; echo -n ${localoctet:$(( $RANDOM % 4 )):1} )
mac=$( for i in {1..10} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' )
echo $local$mac

This will generate all possible local MAC addresses.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .