Mac Address Spoofing and Dynamic Hostname on Linux

Functions: changing the mac address and hostname for each boot.

Tip: Run the command as root user. The example code is on debian.

Mac Address Spoofing

Use macchanger to spoof manually

The macchanger (a.k.a., the GNU MAC Changer) provides a variety of features such as changing the address to match a certain vendor or completely randomizing it.

Install the package macchanger from the official repositories.

apt update
apt install macchanger

Display available network interfaces.

ip a

Change the mac address:

macchanger -r your_interface_name

Automatically change the mac address when boots

Use the NetworkManager to configure MAC address randomization.

vi /etc/NetworkManager/NetworkManager.conf
NetworkManager.conf
1
2
3
4
5
6
7
8
9
[main]
plugins=ifupdown,keyfile
[ifupdown]
managed=false
[connection]
ethernet.cloned-mac-address=random
wifi.cloned-mac-address=random

Dynamic Hostname

Creat a file.

vi /usr/bin/newhostname
newhostname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash
# Description: Generate and set a random hostname on Linux
# Requires: sed, awk
# Usage: set-random-hostname
__set_random_hostname() {
local new_hostname=$(head -n1 < <(fold -w8 < <(tr -cd 'a-z0-9' < /dev/urandom)))
# set new hostname
hostnamectl set-hostname "$new_hostname"
# set new hostname in /etc/hosts
sed -i "2 s/^.*$/127.0.1.1 $new_hostname/g" /etc/hosts
}
__set_random_hostname

Make it executable.

chmod +x /usr/bin/newhostname

Make it run at startup:

vi /etc/systemd/system/newhostname.service
newhostname.service
1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Start Debian with a different hostname each boot
Wants=network-pre.target
Before=network-pre.target
[Service]
ExecStart=/usr/bin/newhostname
Type=oneshot
[Install]
WantedBy=multi-user.target

Now you can reboot to test the functions.