Mac Address Spoofing and Dynamic Hostname on Linux

Contents
  1. 1. Mac Address Spoofing
    1. 1.1. Use macchanger to spoof manually
    2. 1.2. Automatically changes the mac address when boots
  2. 2. Dynamic Hostname

Last Update: 2019-09-10


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

Tip: 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.

1
2
sudo apt update
sudo apt install macchanger -y

Display available network interfaces.

1
ip a

Change the mac address:

1
sudo macchanger -r your_interface_name

Automatically changes the mac address when boots

Use the NetworkManager to configure MAC address randomization.

1
sudo vim /etc/NetworkManager/NetworkManager.conf
/etc/NetworkManager/NetworkManager.conf
1
2
3
4
5
6
7
8
9
10
11
12
[main]
plugins=ifupdown,keyfile

[ifupdown]
managed=false

[device]
wifi.scan-rand-mac-address=yes

[connection]
ethernet.cloned-mac-address=random
wifi.cloned-mac-address=random

Dynamic Hostname

Creat a file.

1
2
sudo mkdir -p /etc/systemd/scripts
sudo vim /etc/systemd/scripts/newhostname
/etc/systemd/scripts/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.

1
sudo chmod +x /etc/systemd/scripts/newhostname

Make it run at startup:

1
sudo vim /etc/systemd/system/newhostname.service
/etc/systemd/system/newhostname.service
1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Start Debian with a different hostname for each boot
Wants=network-pre.target
Before=network-pre.target

[Service]
ExecStart=/etc/systemd/scripts/newhostname
Type=oneshot

[Install]
WantedBy=multi-user.target
1
sudo systemctl enable newhostname

Now you can reboot to test the functions.