Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I generate a random MAC address from the Linux command line?

I search for a solution that only requires standard tools commonly found on the Linux command line.

The MAC address will be used for a guest KVM.

share|improve this question
up vote 32 down vote accepted

I use

macaddr=$(echo $FQDN|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')

The benefit of this method, over a completely random number, is that it's possible to reliably reproduce the MAC address based on the FQDN of the machine, which I find useful sometimes. The 02 for the first octet just sets the "locally assigned" bit, which makes it obvious that it's not a vendor-provided MAC address, and guarantees that you won't collide with a real NIC's MAC address.

If you need to generate multiple MAC addresses per host, I used to concatenate the FQDN with the name of the bridge to connect the interface to; this did a good job of spreading things out for different NICs.

share|improve this answer
+1 for the reproducibility; for certain applications, that makes it a much superior method to mine. – MadHatter Aug 10 '11 at 8:18
Thanks a lot, I like the idea of having it reproducible. – Erik Sjölund Aug 10 '11 at 11:58
Plus no mac address conflicts in the unlikely event you'd randomly generate the same mac twice – Petter H Sep 25 '14 at 8:06
As alternative you can use tr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/\1:/g;s/:$//;s/^/02:/' – ALex_hha May 26 at 8:59
It's only an "alternative" in the sense that it produces a completely different end result to what my snippet does... – womble yesterday
myserver% perl -e 'for ($i=0;$i<6;$i++){@m[$i]=int(rand(256));} printf "%X:%X:%X:%X:%X:%X\n",@m;'

Ah, the ol' Swiss Army Chainsaw rides again. And by way of version 0.2, I'm unashamedly stealing womble's excellent point about the first octet being 02:

myserver% perl -e 'for ($i=0;$i<5;$i++){@m[$i]=int(rand(256));} printf "02:%X:%X:%X:%X:%X\n",@m;'
share|improve this answer
Thanks MadHatter, I tried your second variant and it worked. Very nice! – Erik Sjölund Aug 11 '11 at 12:47

The posted scripts are good, but I want to add a warning: Mind the Birthday (paradoxon)!

It comes from the fact that even if you have just 23 people, the chance is already 50% that 2 of them have birthday on the same day.

It depends on your scenario how you use it, but if you generate the MACS randomly, at approx 1 million your chance for a mac number clash is 40% at 2 million it is already 87%!

If you need just a couple this is ok, but when you maintain a server farm with hundreds of servers, each of them hosting tens of virtual machines, or if you use the macs as index in some db for bookkeeping and you need uniques be careful!

share|improve this answer
Thanks, for the warning about the Birthday paradox! In my case I will take the risk as I will generate around 20 MAC addresses. – Erik Sjölund Aug 10 '11 at 12:22
If you're running hundreds of servers each hosting tens of virtual machines all on the same broadcast domain, you've got bigger problems than MAC address collision risk. – womble Sep 25 '14 at 23:28

I know this post is old, but for future visitors, if you want a cryptographically secure pseudorandom MAC address, without being limited to 0x02 as the OUI, here is a fast mostly platform agnostic generator:

$ printf '%02x' $((0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)); od /dev/urandom -N5 -t x1 -An | sed 's/ /:/g'
share|improve this answer

Here's another one, based on wombie's answer:

macaddr=$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/')
echo $macaddr
share|improve this answer
There's no need to run urandom output through md5sum; you can just use od as per Aaron Toponce's answer. – womble Sep 25 '14 at 23:29

Here are five other options, all of which use random bits for the least significant bit of the most significant byte that indicates if the address is unicast or multicast and for the second-least significant bit of the most significant byte that indicates if the address is universally or locally administered.

jot -w%02X -s: -r 6 1 256
openssl rand -hex 6|fold -w2|paste -sd: -
od -N6 -tx1 -An /dev/random|awk '$1=$1'|tr \  :
god -N6 -tx1 -An /dev/random|cut -c2-|tr \  :
hexdump -n6 -e'/1 ":%02X"' /dev/random|cut -c2-

jot comes with OS X and BSDs but not with most Linux distributions. In jot -w changes the format, -s changes the separator, and -r generates random numbers.

od is in POSIX but hexdump is not.

OS X's od (/usr/bin/od below) uses a different output format than GNU od:

$ /usr/bin/od -N6 -tx1 -An /dev/random|tr ' ' :
$ god -N6 -tx1 -An /dev/random|tr ' ' :

In OS X's od options placed after an argument for an input file are treated as the names of input files, so the command in the answer by Aaron Toponce reads from /dev/urandom indefinitely with OS X's od.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.