1

On previous Ubuntus this was easily achieved with macchanger, but it seems not to work on 16.04. The MAC just seems to reset to the default as soon as a connection to the Internet is reestablished. Yes, now it can be done in the network-manager GIU, but this removed the automation of having a script do it, and also removed the random MAC generation facility of macchanger.

I see other questions that point the solution as using the GUI to change it manually, but I don't see any that can do it randomly from command line as would be needed for a boot script.

Ravexina
  • 55,668
  • 25
  • 164
  • 183
fpghost
  • 1,659
  • If your computer is directly connected to a cable/dsl modem, won't your ISP throw a fit when they see the various MAC addresses? If your computer is connected to a router, I'm not sure that I understand the reason for what you're trying to do. So... why? Inquiring minds want to know. – heynnema May 12 '17 at 01:52
  • There are all kinds of reasons one would like to change a mac, but I think they would be beyond the scope of this question to get into here. – fpghost May 12 '17 at 01:55
  • Give me the simple answer. ISP's normally only allow one CPU (or router) to be connected to their cable/dsl modem. Changing the MAC if your computer is directly connected to your router shouldn't really buy you anything. Are you trying to "hide" your computer? If so, consider a good VPN instead. – heynnema May 12 '17 at 01:58
  • I think you can easily just Google the answer. To give you one simple use case, consider a coffee shop that limits an hour usage per MAC address. Or consider any other local network where the admin has blocked your MAC. – fpghost May 12 '17 at 02:01
  • At any rate this question is about the "how" , not the "why", which is another topic. – fpghost May 12 '17 at 02:03
  • If you're sitting in a coffee shop for more than one hour, trying to use their network for more than 1 hour, and not buying coffee, the owner will probably kick you out long before. And a local network doesn't put your MAC in a block list... they put it in an allow list. So your two examples really don't fly. – heynnema May 12 '17 at 02:05
  • Really not true if the coffee shop is busy and you are sat upstairs. MAC deny lists do exist too, my network is administered exactly like this, so no idea where you get that idea from. There are lots more reasons too, but I sense that you are just trolling rather than genuinely inquiring for which Google would be your friend. – fpghost May 12 '17 at 02:08

1 Answers1

4

How to do it

Without any third party application, we can simply use NetworkManager's cli tool nmcli to change MAC address in terminal, scripts, etc.

nmcli connection modify enp3s0 802-3-ethernet.cloned-mac-address 02:7d:xx:xx:...
  • Change enp3s0 with your desired connection name, e.g: Home, Office Wi-Fi, etc.

If you are trying to clone a Wi-Fi connection then use 802-11-wireless.cloned-mac-address instead of 802-3-ethernet.cloned-mac-address.

Also we need a way to generate a random MAC here is a simple solution to create a completely random MAC address (Base source):

echo $RANDOM | md5sum | sed\
 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/'

Final solution

Finally in your script use something like this:

mac=$(echo $RANDOM | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/')
nmcli connection modify enp3s0 802-3-ethernet.cloned-mac-address $mac

There might be also a need to reload the connection:

nmcli connection down enp3s0
nmcli connection up enp3s0

You can put it in .profile or any other place you want.


Create random mac with valid OUI

If you don't want a completely random mac address then download this from GNU MAC Changer GitHub repository.

Then use this line to generate mac addresses:

shuf -n1 OUI.list | cut -f1-3 -d' ' | tr ' ' ':' | xargs -I company echo\
 company:`echo $RANDOM|md5sum|sed 's/^\(..\)\(..\)\(..\).*$/\1:\2:\3/'`
  • shuf -n1 OUI.list: selects a random line from that file.
  • cut -f1-3 -d' ' cuts the three necessary fields
  • tr ' ' ':' transforms it to a form we want
  • xargs ... creates the other three random part and concatenates the result.
Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • Thanks! After 'modify', I think you want the connection name not interface, e.g. "airportwifi", also for WiFi MAC changing it would be "802-11-wireless" probably. – fpghost May 11 '17 at 22:43
  • This seemed to change the value in network manager's edit connection to the new random MAC, however, my router admin still sees me with the old MAC using this method. – fpghost May 11 '17 at 22:44
  • Yeah, and my connection name is same as my interface name, if you look close you'll see that I mentioned " Change enp3s0 with your desired connection". – Ravexina May 11 '17 at 22:45
  • Have you tried reloading, bringing down and up the connection? – Ravexina May 11 '17 at 22:46
  • OK all good now, thanks. My one concern is that these MACs are probably not starting with valid OUIs? – fpghost May 11 '17 at 22:52
  • Something like incorporating the OUI list from the macchanger repo maybe? (github.com/alobbs/macchanger/master/data/OUI.list). – fpghost May 11 '17 at 22:55
  • check my update. – Ravexina May 11 '17 at 23:25