How to Spoof Your MAC Address and Host Name on Ubuntu
If you value privacy and don’t want networks to track your computer, a good first step is to spoof your computer’s MAC address and host name.
You MAC address is the hardware identifier assigned to your network card. When you connect to a network, a router gives your computer an IP address and stores both your MAC and IP address in a table.
This happens with any network you join and your MAC address never changes. This means that there’s a record of all the places you’ve been to over the years. In short, networks are tracking you.
Along with your MAC address, your computer’s name or “hostname” is also visbile on a network depending upon which services you use. On MacBooks, your hostname could be something like “Bobby’s MacBook Pro”. But it’s possible you’re not comforable with broadcasting your name over a network.
To protect yourself, you want to do two things when connecting a computer to a wireless network:
- Use a fake MAC address by randomly choosing a MAC address to assign to the wireless network card.
- Use a fake hostname by randomly choosing a name from the
first-names.txt
file and assigning it as the new hostname.
The code would look like this:
#!/bin/sh
set -e
basedir=$(dirname "$0")
# Get wireless adapter name
wireless_name=$(ls /sys/class/net | grep w)
# Turn off wireless
sudo ifconfig $wireless_name down
# Change MAC address to random MAC address
sudo macchanger -r $wireless_name
# Turn on wireless again
sudo ifconfig $wireless_name up
# Get random name from first-names.txt file
first_name=$(shuf -n 1 $basedir/first-names.txt | sed -e 's/[^a-z]//g')
sudo hostnamectl set-hostname $first_name
Now all you need to do is execute this code any time your computer joins a wireless network. In Ubuntu, this means:
- Copying your script and putting it in
/etc/network/if-up.d/
. - Ensuring your script name doesn’t have a
.sh
at the end. - Making the script executable (i.e.
chmod +x my-script
).
Then the script might look like:
#!/bin/sh
set -e
basedir=$(dirname "$0")
# Get wireless adapter name
wireless_name=$(ls /sys/class/net | grep w)
# If the interface is wireless (and not ethernet)
if [ "$IFACE" = $wireless_name ]; then
# Turn off wireless
sudo ifconfig $wireless_name down
# Change MAC address to random MAC address
sudo macchanger -r $wireless_name
# Turn on wireless again
sudo ifconfig $wireless_name up
# Get random name from first-names.txt file
first_name=$(shuf -n 1 $basedir/first-names.txt | sed -e 's/[^a-z]//g')
sudo hostnamectl set-hostname $first_name
fi
That’s it! You can verify a change in hostname by running hostname
in the terminal and verify you’ve got a new MAC address by running ip link show | awk '{print$2}' | tail -1
.
If you have a MacBook instead, you can achieve what you did in this article by following Sun Knudsen’s steps for macOS.