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.
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.
I use
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.
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!
These variants work as well.
longer:
or shorter:
The load consumption of both variants is very similar according to quick measuring with time.
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:
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:
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
comes with OS X and BSDs but not with most Linux distributions. Injot
-w
changes the format,-s
changes the separator, and-r
generates random numbers.od
is in POSIX buthexdump
is not.OS X's
od
(/usr/bin/od
below) uses a different output format than GNUod
: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'sod
.Here's another one, based on wombie's answer:
I use:
You could just add a $RANDOM after $FQDN and this would give you random mac addresses every time you run it. This is especially helpful for poeple who want to create backup vms using snapshots or clones of vms.
Python one-liner: