How to generate a unique MAC address

For some of my LXC containers, I'm allocating a reserved private IP and need to generate a reasonably unique MAC address that's calculated from the container's hostname. As is LXC's default, I'm using the OUI that belongs to Xensource, Inc (00:16:3e).

Here's a Bash function I'm using:

GetMAC()
{
  if [ -n "$1" ]; then
    OID="00:16:3e"
    RAND=$(echo $1 | md5sum | sed 's/\(..\)\(..\)\(..\).*/\1:\2:\3/')
    echo "$OID:$RAND"
  else
    echo "ERROR: please supply hostname to create MAC address from, e.g.:"
    echo "       $FUNCNAME myhost"
  fi
}

Code in action:

$ GetMAC myhost
00:16:3e:e0:04:92

Useful references:

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).