Last active
August 6, 2025 15:35
-
-
Save KebabLord/f9a8ab46f88575a09fd7f84683abe4b8 to your computer and use it in GitHub Desktop.
Run macchanger on boot init script. The original auto spawn doesn't work since it's nearly 10 years old. Should work on all debian based systems, since it's a SysV style init script. Tested on devuan+openrc . Can be easily strict-chained with NM. Requires iw package to obtain interface.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# | |
# Randomize computer's MAC address on boot before the network-manager service. | |
# Using with Kebablord/macchanger-ng is adviced since | |
# original repo didn't receive any PR for nearly 10 years. | |
# Script also exits safely on any fail so you can safely chain it with | |
# network-manager updating Required-Start parameter from NM's init script. | |
# This gist is created for educational purposes only. | |
# | |
# No Copyright (c) 2023 Kebablord Corporation. | |
# | |
### BEGIN INIT INFO | |
# Provides: mac-randomizer | |
# Required-Start: $remote_fs dbus udev | |
# X-Start-Before: network-manager | |
# X-Stop-After: network-manager | |
# Default-Start: 2 3 | |
# Default-Stop: 0 1 6 | |
# Short-Description: mac address randomizer | |
# Description: Randomize MAC address using macchanger. | |
### END INIT INFO | |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
SCRIPTNAME=/etc/init.d/mac-randomizer | |
. /lib/lsb/init-functions | |
type macchanger 2>/dev/null 1>/dev/null || (log_failure_msg "macchanger not found."; exit 1) | |
#iface=$(ifconfig | grep -P '^w.*?:' | awk -F ':' {'print $1'}) | |
iface=$(iw dev | grep Interf |awk -F 'Interface ' '{print $2}' | head -n 1) | |
[ -z $iface ] && (log_failure_msg "network interface not found."; exit 1) | |
# Exit safely in any fail. It's important because you may | |
# want to use this script in strict chain with network-manager | |
safe_exec() { | |
$* || exit 1 | |
} | |
check_if_changed() { | |
status=$(macchanger -s $iface | awk '{print $3}' | uniq | wc -l) | |
[ 2 -eq $status ] || false && true | |
} | |
# This pause functions are used to revert services | |
# back to their previous state after stopping them. | |
pause_nm() { | |
if (pidof NetworkManager>/dev/null);then | |
safe_exec service network-manager stop | |
safe_exec $* | |
safe_exec service network-manager start | |
return 0 | |
fi | |
safe_exec $* | |
} | |
pause_iface() { | |
if (safe_exec ip link show dev $iface | grep UP);then | |
safe_exec ip link set dev $iface down | |
safe_exec $* | |
safe_exec ip link set dev $iface up | |
return 0 | |
fi | |
safe_exec $* | |
} | |
case "$1" in | |
start) | |
pause_iface macchanger -r $iface >/dev/null 2>/dev/null | |
if check_if_changed;then | |
log_success_msg "Randomized MAC address." | |
else | |
log_failure_msg "Couldn't randomize MAC address." | |
fi | |
;; | |
reset) | |
pause_nm pause_iface macchanger -p $iface | |
;; | |
stop) | |
true | |
;; | |
reload) | |
pause_nm $0 start | |
;; | |
status) | |
if check_if_changed; then | |
echo "MAC spoofing enabled:" | |
macchanger -s $iface | xargs -I % printf " %\n" | |
else | |
echo "Address is default hardware MAC, not changed." >&2 | |
fi | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|reload|reset|status}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment