Generating a random MAC address 2011-08-25

# enum -r -s ':' -f '%02x' 0 .. 6x .. 255
f3:1c:51:29:9a:ed
  • 0 .. 6x .. 255 — produce values between 0 and 255 (inclusively), 6 of them
  • -r — produce random values
  • -s ':' — insert a colon between values (instead of a newline)
  • -f '%02x' — formats values as two-digit hex values

enum is available in at least Debian, Fedora and Gentoo (package app-misc/enum).

Creative Commons License
The Generating a random MAC address by Sebastian Pipping, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

9 Comments
jkt August 25th, 2011

Your code is wrong as it fails to use the private space of MAC addresses.

ago August 25th, 2011

I’m using:

devil ago # cat /home/ago/bash/rand.sh
#!/bin/bash
#Agostino Sarubbo
#Numeri e lettere random

Lettere=”a b c d e f”

Numeri=”0 1 2 3 4 5 6 7 8 9″

lettera=($Lettere)
numero=($Numeri)

rand_lett=${#lettera[*]}
rand_num=${#numero[*]}
echo -n “00″:
echo -n “${numero[$((RANDOM%rand_num))]}”
echo -n “${lettera[$((RANDOM%rand_lett))]}”:
echo -n “${lettera[$((RANDOM%rand_lett))]}”
echo -n “${lettera[$((RANDOM%rand_lett))]}”:
echo -n “${numero[$((RANDOM%rand_num))]}”
echo -n “${lettera[$((RANDOM%rand_lett))]}”:
echo -n “${numero[$((RANDOM%rand_num))]}”
echo -n “${numero[$((RANDOM%rand_num))]}”:
echo -n “${lettera[$((RANDOM%rand_lett))]}”
echo -ne “${numero[$((RANDOM%rand_num))]}\n”

and in a NM init.d script:

ebegin “Starting NetworkManager”
for i in eth0 wlan0
do
ifconfig $i down;
ifconfig $i hw ether $( /home/ago/bash/rand.sh )
ifconfig $i up
done

mihau August 26th, 2011

what is a point to have random MAC? or it’s just fun?!

Paul Tobias August 26th, 2011

For XEN virtual machines we generate the MAC based on the hostname:
echo “$VMHOSTNAME.`dnsdomainname`” | md5sum | sed ‘s/^\(..\)\(..\)\(..\).*$/00:16:3e:\1:\2:\3/’
For KVM virtual machines it’s random:
MAC=”52:54:00$(hexdump -e ‘/1 “:%02x”‘ -n 3 /dev/urandom)”

Elias Pipping August 26th, 2011

If all you’re after is six numbers between 0 and 255, joined by colons and formatted as hex, then you could also use

for i in {1..6}; do
printf -vout %02x $((RANDOM/128));
if [[ $i -ne 6 ]]; then
echo -n $out:;
else
echo $out;
fi;
done

in bash.

[...] Generating a random MAC address [...]

Stephen Trier August 27th, 2011

MAC addresses with an odd first byte land in the multicast range, which can cause problems with bridging and with some network protocol implementations which are smart enough to check. Aside from the question of globally unique vs. locally administered addresses, time spent debugging obscure problems with a batch of off-brand Ethernet cards that had odd first bytes of their MAC addresses (no doubt someone wanted to avoid paying IEEE for a legitimate assignment) taught me to watch out for this.

hardly September 1st, 2011

macchanger -r
But I guess this is more an example of something else than a duplication of effort.

Mart Raudsepp September 5th, 2011

You should make sure to set the second least significant bit of the most significant byte of the address. That would signify a locally administered MAC address and hence can’t collide with burned in addresses on the network ever unless different random local addresses get to be the same.
E.g 06:…

Leave a Reply