Random MAC address

I just thought I had need to blog about that, a script to generate a random MAC address. It is based on $RANDOM from any shell and works reliably.

#! /bin/sh
# Generates a random mac address
echo $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17



  1. very nice 1 liner
    Master Foo would be pleased

  2. # echo $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17
    cut: [-cf] list: values may not include zero

    # echo $RANDOM | openssl md5
    (stdin)= 4b4f98e306dd3797ca1a05c8349b8005

    Gives (stdin) at the begining...but:

    # echo $RANDOM | md5 | sed 's/\(..\)/\1:/g' | cut -c1-17

    ...gives the result. This is on NetBSD though. Thnx for the tip, nice oneliner. :)

  3. Reading the comments... and reading the blog entry, I was also wondering for the cut command. This has changed since some time in the GNU coreutils, 0 prints an error now.

  4. As on my router ther is no md5 command,
    I did:

    RANDOM=$(md5sum /proc/uptime | sed 's/\(..\)/\1:/g' | cut -c1-17)

    regards, wwwolf

  5. No need for cut anyway...

    sed -r 's/(..)/\1:/g; s/^(.{14}).*$/\1/;'

    Also, I found /dev/random or /dev/urandom to be a more appropriate/useful source of random hex:

    MAC=00:`head -c5 /dev/urandom|hexdump -e '"%x"'| sed -r 's/(..)/\1:/g; s/:$//;'`

  6. Very nice use of hexdump, however to be sure to always have two hexa values %02x must be used for the format.

  7. the '-r' flag for sed cannot be used on MacOSX (10.6), use '-E' instead

  8. "cho $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17"

    Hey so I am starting to learn how to script and I was looking into mac generators..

    I find this crazy line of code that looks like a little dude with his arms in the air
    " /\(..\)/\ "
    And thru some witch craft this little dude spits out a mac address......
    I have no idea how it works but it does..
    This is an awesome piece of art Mike, nice job.

  9. " /\(..\)/\1 "
    / starts the expression
    \ escapes the (
    ( begins a sort of "clipboard"
    . finds any character
    . finds any character
    \ escapes the )
    ) closes the clipboard
    / ends the search, begins the replacement
    \ espaces the 1
    1 pastes the clipboard