I was wondering about how to write a script for changing my default MAC address before enabling networking. It happens often, that the MAC address changes to default.

I would like to show the MAC address in conky (or any other graphical desktop interface). This script should also change the MAC address automatically if for any reason the default was restored.

  • you must notice that its impossible to change MAC address, its coming with device. But you can change it in incomping/outgoing packages. – hingev Mar 13 '13 at 12:41
  • everytime i write: sudo ifconfig wlan0 down, sudo macchanger -a wlan0, sudo ifconfig wlan0 up. how can i make this automatic but only if the macadress is default again.?:) – Erling Mar 13 '13 at 12:43
  • run script every time when network will connect ? – hingev Mar 13 '13 at 15:25

NetworkManager itself can change a network interface MAC address and it will not be changed back to the default one while the connection is established.

Edit the network connection (NetworkManager indicator -> Edit connections -> Wireless tab -> Select a connection -> "Edit" button). In the "Wireless" tab you'll find a "Cloned MAC address" textbox. Type in the new MAC address it should use when the connection is established.

enter image description here

| improve this answer | |
  • Hi Eric,muito obrigado;) I knew this and it works well(I think it works , even if the ifconfig stays the same, isnt it?) – Erling Mar 14 '13 at 23:05
  • and still this is also not really perferct, desculpe. mas eu quero um endereco de mac (sounds funny in portuguese) alterado. gosto muito da funcao de macchanger -a. It is like a backup in case I forget to run the script. – Erling Mar 14 '13 at 23:09
  • Google translator? :-P ifconfig shows the new mac, see the output of ifconfig | grep wlan0 with wifi disconnected: wlan0 Link encap:Ethernet HWaddr 0c:60:76:42:9f:37, and after a established connection with "cloned" mac: wlan0 Link encap:Ethernet HWaddr 00:11:22:33:44:55. I think it's better than using macchanger because you can set a different custom mac for each connection without bother with running commands or making scripts. Or maybe I just didn't understand your use case. – Eric Carvalho Mar 15 '13 at 1:36
  • jamais ;) it just happened again. i connected to another wifi while trying what you sad. On my laptop I have a wifi button for disabling wlan0 (not ifconfig wlan0 down). I like to use it because it somekind of cut the connection without delay. the only thing, it resets the macadress. i would like to have a grafical output of the current macadress on my desktop or cairo dock. if this is not possible , well, eu acho que vou dar um jeito um outro dia. pois meu amigo, valeu por ajuda. – Erling Mar 15 '13 at 3:56
  • 1
    Btw. you can set the 'Cloned MAC address' to random or __:__:__:__:__:__ That way NM generates a new MAC address every time it connects to the network. – JepZ Sep 22 '17 at 16:17

Just make script that will change mac address and make it run whenever you connect to network

sudo ifconfig wlan0 down
sudo macchanger -a wlan0
sudo ifconfig wlan0 up

How to run cron job when network is up?


@n00b, answers to your questions. So as I read here you can set macchanger to change MAC address automatically using

macchanger --mac=01:23:45:67:89:AB wlan0

And other question: "how to make it run automatically". So here it says you to add script to /etc/network/if-up.d/ and make it executable.

| improve this answer | |
  • I had once such a script and made a link in my taskbar. it was still not that what I wanted, because I had to (run it in a terminal as administrator and )write the password every time. And my laptop resets the macadress a couple of times every day( – Erling Mar 14 '13 at 22:58
  • ;)I had once such a script and made a link in my taskbar. it was still not that what I wanted, because I had to (run it in a terminal as administrator and )write the password every time. And my laptop resets the macadress a couple of times every day. this was the reason that my initial question is also refering to conky btw:if I am using a router and connect to a server, does the server see my or the routers macadress? Im just asking, maybe I am just to concerned about the macadress at all. or maybe to less because im sitting in europa and we dont have such firewall like in other countries. – Erling Mar 14 '13 at 23:04
  • hm.. @Reese when you use DNS on router it will ask for your mac and get free IP for you, which will be generated on base of your MAC. But I don't know, there may be other protocols as well that will need your MAC address... – hingev Mar 15 '13 at 9:49
  • @ geoh: Thanks for the script. I've 2 questions. Question#1: In which folder do you place the script? Question#2: Will your script automatically input a new MAC address or do I have to enter it manually? – n00b Mar 23 '13 at 9:57
  • @n00b I've made edit and answered questions. – hingev Mar 23 '13 at 11:32

I tried a number of different ways of making the Macchanger script work with Network Manager, but I never found a good one.

I tried the one proposed by Geoh, and apparently worked (it changes Mac Address of the interface every time you disconnect and reconnect), but even though Network Manager show you are connected to a network, you cannot connect anymore.

I tried to put the script in /etc/network/if-pre-up.d/ , /etc/network/if-down.d/ , /etc/network/if-post-down.d/ , but in that case it was just not working. I tried also without the ifconfig up/lines, but no success still.

I guess that is due to the fact Macchanger requires wireless card to be down, and Network Manager is kind of resetting some parameters of it every time you disconnect/connect.

For this reason I switched to Wicd, with this connection manager you simply save the script in /etc/rc.local

Good to know Network Manager now has "Cloned MAC address" option, though it is not the same as starting with a randomize Mac every boot (or even connection)

In the last part of this discussion, they propose workarounds, but I haven't tried them (https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/336736).


| improve this answer | |

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