how to generate a random MAC address from the Linux command linehow to install a CentOS 6.0 KVM guest on a CentOS 6.0 host server with virt-install and PXEUseful Command-line Commands on WindowsWhat's the command-line utility in Windows to do a reverse DNS look-up?Linux command line best practices and tips?How to determine the hostname from an IP address in a Windows network?QEMU-KVM Linux virtualization on the command lineWhat is the Cisco iOS (CLI) command to display a WAP's Radio MAC address?Gather mac addresses of domain computers, preferably from command lineGenerate CPU, memory, disk and network graphs from command-lineIs the MAC address of the physical host used in a virtual environment?How to get the correct MAC address of Windows Server 2008 out of multiple MAC addresses?
What happens to foam insulation board after you pour concrete slab?
Remove sudoers using script
Did thousands of women die every year due to illegal abortions before Roe v. Wade?
When conversion from Integer to Single may lose precision
Why only the fundamental frequency component is said to give useful power?
What are the words for people who cause trouble believing they know better?
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?
After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?
Does the first version of Linux developed by Linus Torvalds have a GUI?
Select items in a list that contain criteria
Secure offsite backup, even in the case of hacker root access
Why don’t airliners have temporary liveries?
Java guess the number
Turing patterns
How to make a setting relevant?
What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?
How is it possible that Gollum speaks Westron?
Incremental Ranges!
4 Layer PCB stack up
Approximate solutions to non polynomial equations
PL/SQL function to receive a number and return its binary format
What happens when the attacking player dies to damage triggers after killing the blocking creatures in the first combat step of double strike?
What risks are there when you clear your cookies instead of logging off?
What happened to all the nuclear material being smuggled after the fall of the USSR?
how to generate a random MAC address from the Linux command line
how to install a CentOS 6.0 KVM guest on a CentOS 6.0 host server with virt-install and PXEUseful Command-line Commands on WindowsWhat's the command-line utility in Windows to do a reverse DNS look-up?Linux command line best practices and tips?How to determine the hostname from an IP address in a Windows network?QEMU-KVM Linux virtualization on the command lineWhat is the Cisco iOS (CLI) command to display a WAP's Radio MAC address?Gather mac addresses of domain computers, preferably from command lineGenerate CPU, memory, disk and network graphs from command-lineIs the MAC address of the physical host used in a virtual environment?How to get the correct MAC address of Windows Server 2008 out of multiple MAC addresses?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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.
linux command-line-interface mac-address
add a comment |
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.
linux command-line-interface mac-address
add a comment |
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.
linux command-line-interface mac-address
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.
linux command-line-interface mac-address
linux command-line-interface mac-address
edited Oct 11 '12 at 16:12
voretaq7
74.8k14115199
74.8k14115199
asked Aug 10 '11 at 7:46
Erik SjölundErik Sjölund
85551324
85551324
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
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.
+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
2
As alternative you can usetr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/1:/g;s/:$//;s/^/02:/'
– ALex_hha
May 26 '16 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♦
May 30 '16 at 6:10
add a comment |
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!
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
2
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
add a comment |
myserver% perl -e 'for ($i=0;$i<6;$i++)@m[$i]=int(rand(256)); printf "%X:%X:%X:%X:%X:%Xn",@m;'
55:C2:A5:FA:17:74
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:%Xn",@m;'
02:8E:94:A3:47:26
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
add a comment |
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'
add a comment |
These variants work as well.
longer:
openssl rand -hex 6 | sed 's/(..)(..)(..)(..)(..)(..)/1:2:3:4:5:6/'
or shorter:
openssl rand -hex 6 | sed 's/(..)/1:/g; s/:$//'
The load consumption of both variants is very similar according to quick measuring with time.
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she usedfold -w2|paste -sd: -instead ofsed. Thesedsolution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.
– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins withecso 11101100 in binary...
– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
add a comment |
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
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
add a comment |
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 ' ' :
:::::::::::d9::b9::d7::da::5f::96::::::::::::::::::::::::::::::::::::::::
$ god -N6 -tx1 -An /dev/random|tr ' ' :
:f5:6d:0a:3b:39:f9
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.
add a comment |
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.
macaddr=$(echo $FQDN$RANDOM|md5sum|sed 's/^(..)(..)(..)(..)(..).*$/02:1:2:3:4:5/')
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
add a comment |
I use:
echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':'
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f299556%2fhow-to-generate-a-random-mac-address-from-the-linux-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
+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
2
As alternative you can usetr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/1:/g;s/:$//;s/^/02:/'
– ALex_hha
May 26 '16 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♦
May 30 '16 at 6:10
add a comment |
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.
+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
2
As alternative you can usetr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/1:/g;s/:$//;s/^/02:/'
– ALex_hha
May 26 '16 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♦
May 30 '16 at 6:10
add a comment |
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.
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.
answered Aug 10 '11 at 8:00
womble♦womble
86.4k18148207
86.4k18148207
+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
2
As alternative you can usetr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/1:/g;s/:$//;s/^/02:/'
– ALex_hha
May 26 '16 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♦
May 30 '16 at 6:10
add a comment |
+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
2
As alternative you can usetr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/1:/g;s/:$//;s/^/02:/'
– ALex_hha
May 26 '16 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♦
May 30 '16 at 6:10
+1 for the reproducibility; for certain applications, that makes it a much superior method to mine.
– MadHatter
Aug 10 '11 at 8:18
+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
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
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
2
2
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 '16 at 8:59
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 '16 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♦
May 30 '16 at 6:10
It's only an "alternative" in the sense that it produces a completely different end result to what my snippet does...
– womble♦
May 30 '16 at 6:10
add a comment |
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!
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
2
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
add a comment |
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!
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
2
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
add a comment |
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!
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!
answered Aug 10 '11 at 9:23
floloflolo
40249
40249
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
2
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
add a comment |
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
2
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
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
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
2
2
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
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
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
"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." That's not even remotely true. There is about a 50% chance that two of 23 people have the same birthday anniversary, not the same birthday.
– Ron Maupin
Jul 29 '16 at 21:45
add a comment |
myserver% perl -e 'for ($i=0;$i<6;$i++)@m[$i]=int(rand(256)); printf "%X:%X:%X:%X:%X:%Xn",@m;'
55:C2:A5:FA:17:74
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:%Xn",@m;'
02:8E:94:A3:47:26
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
add a comment |
myserver% perl -e 'for ($i=0;$i<6;$i++)@m[$i]=int(rand(256)); printf "%X:%X:%X:%X:%X:%Xn",@m;'
55:C2:A5:FA:17:74
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:%Xn",@m;'
02:8E:94:A3:47:26
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
add a comment |
myserver% perl -e 'for ($i=0;$i<6;$i++)@m[$i]=int(rand(256)); printf "%X:%X:%X:%X:%X:%Xn",@m;'
55:C2:A5:FA:17:74
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:%Xn",@m;'
02:8E:94:A3:47:26
myserver% perl -e 'for ($i=0;$i<6;$i++)@m[$i]=int(rand(256)); printf "%X:%X:%X:%X:%X:%Xn",@m;'
55:C2:A5:FA:17:74
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:%Xn",@m;'
02:8E:94:A3:47:26
edited Aug 10 '11 at 8:18
answered Aug 10 '11 at 8:01
MadHatterMadHatter
70.8k11147207
70.8k11147207
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
add a comment |
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
Thanks MadHatter, I tried your second variant and it worked. Very nice!
– Erik Sjölund
Aug 11 '11 at 12:47
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
answered Sep 25 '14 at 8:04
Aaron ToponceAaron Toponce
1411
1411
add a comment |
add a comment |
These variants work as well.
longer:
openssl rand -hex 6 | sed 's/(..)(..)(..)(..)(..)(..)/1:2:3:4:5:6/'
or shorter:
openssl rand -hex 6 | sed 's/(..)/1:/g; s/:$//'
The load consumption of both variants is very similar according to quick measuring with time.
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she usedfold -w2|paste -sd: -instead ofsed. Thesedsolution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.
– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins withecso 11101100 in binary...
– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
add a comment |
These variants work as well.
longer:
openssl rand -hex 6 | sed 's/(..)(..)(..)(..)(..)(..)/1:2:3:4:5:6/'
or shorter:
openssl rand -hex 6 | sed 's/(..)/1:/g; s/:$//'
The load consumption of both variants is very similar according to quick measuring with time.
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she usedfold -w2|paste -sd: -instead ofsed. Thesedsolution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.
– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins withecso 11101100 in binary...
– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
add a comment |
These variants work as well.
longer:
openssl rand -hex 6 | sed 's/(..)(..)(..)(..)(..)(..)/1:2:3:4:5:6/'
or shorter:
openssl rand -hex 6 | sed 's/(..)/1:/g; s/:$//'
The load consumption of both variants is very similar according to quick measuring with time.
These variants work as well.
longer:
openssl rand -hex 6 | sed 's/(..)(..)(..)(..)(..)(..)/1:2:3:4:5:6/'
or shorter:
openssl rand -hex 6 | sed 's/(..)/1:/g; s/:$//'
The load consumption of both variants is very similar according to quick measuring with time.
answered Sep 7 '16 at 8:40
Jaroslav KuceraJaroslav Kucera
1,280515
1,280515
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she usedfold -w2|paste -sd: -instead ofsed. Thesedsolution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.
– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins withecso 11101100 in binary...
– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
add a comment |
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she usedfold -w2|paste -sd: -instead ofsed. Thesedsolution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.
– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins withecso 11101100 in binary...
– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
Hi Anthony, I see no other variant combining openssl rand and sed here, so this is unique solution in this topic.
– Jaroslav Kucera
Sep 26 '16 at 11:27
That's true. He/she used
fold -w2|paste -sd: - instead of sed. The sed solution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.– Anthony Geoghegan
Sep 26 '16 at 13:21
That's true. He/she used
fold -w2|paste -sd: - instead of sed. The sed solution is probably easier to remember as it uses a more familiar tool – though I learned more from his/her answer.– Anthony Geoghegan
Sep 26 '16 at 13:21
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
I think the first command won't work because it does not set the first bit to be even!
– amrx
Jul 26 '18 at 21:49
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins with
ec so 11101100 in binary...– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @amrx, are you sure the first bit of MAC must be even? I have NIC in one of my servers, which begins with
ec so 11101100 in binary...– Jaroslav Kucera
Jul 27 '18 at 11:56
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
Hi @JaroslavKucera, Unicast MAC addresses must never set the 1's place bit in the first byte. That's the "group" (multicast/broadcast) bit. If you make up your own MAC address, you're supposed to set the 2's place bit (the "locally administered" bit) in the first byte, to differentiate it from a guaranteed globally unique MAC address.
– amrx
Jul 28 '18 at 21:52
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
answered Oct 30 '12 at 8:53
guckigucki
471824
471824
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
add a comment |
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
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
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
add a comment |
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 ' ' :
:::::::::::d9::b9::d7::da::5f::96::::::::::::::::::::::::::::::::::::::::
$ god -N6 -tx1 -An /dev/random|tr ' ' :
:f5:6d:0a:3b:39:f9
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.
add a comment |
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 ' ' :
:::::::::::d9::b9::d7::da::5f::96::::::::::::::::::::::::::::::::::::::::
$ god -N6 -tx1 -An /dev/random|tr ' ' :
:f5:6d:0a:3b:39:f9
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.
add a comment |
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 ' ' :
:::::::::::d9::b9::d7::da::5f::96::::::::::::::::::::::::::::::::::::::::
$ god -N6 -tx1 -An /dev/random|tr ' ' :
:f5:6d:0a:3b:39:f9
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.
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 ' ' :
:::::::::::d9::b9::d7::da::5f::96::::::::::::::::::::::::::::::::::::::::
$ god -N6 -tx1 -An /dev/random|tr ' ' :
:f5:6d:0a:3b:39:f9
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.
edited May 26 '16 at 7:34
answered May 26 '16 at 7:07
nisetamanisetama
1214
1214
add a comment |
add a comment |
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.
macaddr=$(echo $FQDN$RANDOM|md5sum|sed 's/^(..)(..)(..)(..)(..).*$/02:1:2:3:4:5/')
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
add a comment |
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.
macaddr=$(echo $FQDN$RANDOM|md5sum|sed 's/^(..)(..)(..)(..)(..).*$/02:1:2:3:4:5/')
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
add a comment |
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.
macaddr=$(echo $FQDN$RANDOM|md5sum|sed 's/^(..)(..)(..)(..)(..).*$/02:1:2:3:4:5/')
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.
macaddr=$(echo $FQDN$RANDOM|md5sum|sed 's/^(..)(..)(..)(..)(..).*$/02:1:2:3:4:5/')
edited Oct 31 '18 at 15:26
Michael Hampton♦
179k27326659
179k27326659
answered Jul 28 '16 at 22:30
QuicksilverQuicksilver
212
212
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
add a comment |
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
Note that $RANDOM is available in bash, but may not be available in other shells.
– Michael Hampton♦
Oct 31 '18 at 15:27
add a comment |
I use:
echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':'
add a comment |
I use:
echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':'
add a comment |
I use:
echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':'
I use:
echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':'
answered Jul 19 '16 at 8:54
JezzJezz
1213
1213
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f299556%2fhow-to-generate-a-random-mac-address-from-the-linux-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown