So I like to set a new, randomly-generated MAC address after every restart using

ruby -e 'print ("%02x"%((rand 64)*4|2))+(0..4).inject(""){|s,x|s+":%02x"%(rand 256)} + "\n"'

and then copy/pasting the output after the command

sudo ifconfig en0 ether

so that I get a new IP address from my ISP each day. I would like to further automate this process so that on startup my computer automatically runs these two commands, but I don't know enough about CLI syntax to make it work. How can I combine these two commands so that ifconfig takes the generated output of the ruby command in a single step? And then, how can I get that to automatically run at startup?

Thank you.

1 Answer 1


Run EDITOR=nano sudo crontab -e and add a line like this:

@reboot ifconfig en0 ether $(printf \%02x $(($RANDOM\%128*2)))$(head -c4 /dev/random|hexdump -v -e '/1 ":\%02x"')

Commands scheduled by @reboot are run after you restart or turn the computer on but not after you log out and back in. % has to be escaped as \% in crontab.

From a comment at http://osxdaily.com/2010/11/10/random-mac-address-generator/:

You need to be careful that this does not generate multicast mac addresses, as these are technically illegal as source macs. The strict definition of a multicast mac address is one where the least significant bit of the first byte is set to 1. So if the first octet’s LSB is 1 (01, 03,05, a1, etc) you technically have a multicast mac source. See http://en.wikipedia.org/wiki/MAC_address.

Using multicast src macs might not cause immediate connectivity problems, but certainly has implications for switches learning the mac address preventing unicast flooding, and routers allowing arp to resolve.

Cisco, for example, will not allow mac learning if the source is non-unicast, and Cisco routers will not install arp entries for multicast macs to unicast ip addresses.

  • I was aware of the multicast issue from the osxdaily link; I believe the ruby code I was using accounted for this. I'm still trying to reverse-engineer the syntax you're using to figure out the general case for how to "embed" one command inside another.
    – Mathew
    Sep 21, 2013 at 7:34
  • The ruby command you included does also account for it, but I just added the quote as an explanation why the command used $(($RANDOM%128*2)). A third option would be ruby -e 'puts ([rand(128)*2]+(1..4).map{|x|rand(256)}).map{|x|x.to_s(16)}*":"'. You can use command substitution $() to embed commands.
    – Lri
    Sep 21, 2013 at 7:38
  • Thanks. Where is crontab supposed to be saved in OS X? After doing ^O to save changes, nano puts it in /tmp/crontab.jqR7fMrDDo.save which I'm not sure is going to work after a restart (I'm not in a position to be able to restart yet).
    – Mathew
    Sep 21, 2013 at 7:41
  • The crontab of the root user is stored in /usr/lib/cron/tabs/root, but you're not supposed to edit it directly.
    – Lri
    Sep 21, 2013 at 7:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .