OpenWrt Forum Archive

Topic: macchanger upon boot

The content of this topic has been archived on 20 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.


I'd like to utilize macchanger to randomize my wireless mac address upon boot, i.e. before the wifi initially comes up. So far, I came to the conclusion that /etc/init.d/network would be the best way to introduce a respective macchanger command. But ... Without knowing every fine detail of the OpenWrt boot process (i.e. "insider tricks"), I'd like to ask if you have any other, maybe better, ideas?


EDIT: Thinking about it, I may introduce /etc/rc.d/Sxymacchanger myself (OpenWrt Wiki evolved as I see!). Having the default order ...

lrwxrwxrwx    1 root     root            14 Jun  8  2012 S10boot -> ../init.d/boot
lrwxrwxrwx    1 root     root            14 Jun  8  2012 S11ubus -> ../init.d/ubus
lrwxrwxrwx    1 root     root            17 Jun  8  2012 S20network -> ../init.d/network

At what order Sxy would such a custom script be sufficient from your perspective? Between S11ubus and S20network? Just trying to get a second opinion before messing up wink

(Last edited by orange on 13 Jun 2013, 14:47)

Hi, Orange,

Good to see you active again.

Any progress about AAP v4?

Now I have a tp-link wr840n v1, flashed with backfire 10.3.1 rc6.
In your post, you just listed the scripts but I found I'm using ath9k not madwifi for my wr840.And the macchanger ipk can't be installed in rc6.

So I'm finding ways to flash with it for madwifi or wating for you next version of AAP.

(Last edited by eeff11 on 14 Jun 2013, 06:28)

hi, Organge,

Have you solved this problem? Anyone knows to help?

I have a tplink router that works in 'sta'mode, now I need it to randomly change it's wwan mac each boot for this router repeats a free city wifi that each mac has 12 hour connection limit.

While I installed the macchanger ipk in AA 12.09, put the "macchanger wlan0 -r" in the initial script, the wwan mac address stays the same, which is the original one.

Any one can help, I have searched a lot.Others have AP runing before sta, I only did it as a simple repeater, ''sta',

Any idea?

anyway i wrote a script that changes the macaddress on boot before the wifi interface comes online, and each time the timeout runs out

#!/bin/sh /etc/rc.common


prefix='28:6A:BA' #prefix used for the mac address, randomizing this part gave strange errors (sometimes)
iface=0 #interface number according to: uci show wireless
renew=39600 #timeout to renew the mac address in secconds


start() {
  if [ -f "${pidfile}" ]; then
    pid=`cat "${pidfile}"`
    if [ ! -z `top -bn1 | awk '{if ($1 == "'"${pid}"'") print $1}'` ]; then
      logger -t "mac changer" -s -p daemon.error "already running!"
      return  1
  logger -t "mac changer" -s -p "started"
  while [ true ]; do
    mac="${prefix}"`hexdump -n3 -v -e '/1 ":%02X"' /dev/urandom`
    network=`uci get wireless.@wifi-iface[${iface}].network`
    if [ $? -ne 0 ]; then
      logger -t "mac changer" -s -p daemon.error "iface ${iface} not found, exiting"
      exit 1
    uci set wireless.@wifi-iface[${iface}].macaddr="${mac}"
    uci commit wireless
    logger -t "mac changer" -p "new mac: ${mac} set for: ${network}"
    [ "$action" != "boot" ] && /sbin/wifi restart 2> /dev/null || action=''
    sleep "${renew}"
  echo $! > "${pidfile}"

stop() {
  if [ -f "${pidfile}" ]; then
    pid=`cat "${pidfile}"`
    rm "${pidfile}"
    if [ ! -z `top -bn1 | awk '{if ($1 == "'"${pid}"'") print $1}'` ]; then
      kill ${pid} $(top -bn1 | awk '{if ($2 == "'$pid'") print $1}')
      logger -t "mac changer" -s -p "stopped"
      return 0
  logger -t "mac changer" -s -p daemon.error "not running!"
  return 1

save the file to /etc/init.d/macChanger
make it executable chmod +x /etc/init.d/macChanger
and enable it /etc/init.d/macChanger enable

Make sure that you save the file under unix type, (windows uses a slightly different line termination).
one way to do it under putty:
type cat > /etc/init.d/macChanger
paste the whole script and press [crtl]+d and the file is saved as well
(afterwards you can ofcourse edit it with vi on the router.

edit: removed small bug

(Last edited by FriedZombie on 3 Oct 2013, 17:40)

yeah, FriedZombie,thank you so much.

I am exploring how to use the official macchanger.ipk correctly, I have already installed it. Wondering how to use it for each boot, so that I can connect to the free city wifi each day for 12 hours. Since there too many 'inside tricks' in openwrt, so still no sucess.

Your script seems to reinvent the wheel. But I will test it when I got another mini router.

Good luck with the macchanger, the problem you will run into is the wifi manager, (wpa_supplicant) and some other things.
that's why I wrote this script. (I never got macchanger working properly, but if you only need it at boot before those things get started you have a good chance).

Also it are not really insider tricks, they are pretty well documented on the wiki.
Understanding how linux works goes a long way as well

if you want macchanger to run before the wifi comes online, i recommend to write a simple init script. Since the rc.local script runs after all the other scripts.

store into something like /etc/init.d/script

#!/bin/sh /etc/rc.common


start() {
  # your code here

and enable it with /etc/init.d/script enable

the network starts on 20 so your command will be executed before the network comes online
One more advantage I can think of in my script, is that you don't have to restart your router every 12 hours (call it lazy)

(Last edited by FriedZombie on 4 Oct 2013, 14:43)

The discussion might have continued from here.