2020-06-18 10:59:51 -04:00
<!--
2020-12-03 06:29:52 -05:00
Title: How to spoof MAC address and hostname automatically at boot on macOS
Description: Learn how to spoof MAC address and hostname automatically at boot on macOS.
2020-06-18 10:59:51 -04:00
Author: Sun Knudsen < https: / / github . com / sunknudsen >
Contributors: Sun Knudsen < https: / / github . com / sunknudsen >
2020-09-10 10:07:07 -04:00
Reviewers:
2020-06-18 10:59:51 -04:00
Publication date: 2020-05-19T00:00:00.000Z
2020-11-10 07:13:35 -05:00
Listed: true
2023-09-06 19:23:39 -04:00
Pinned:
2020-06-18 10:59:51 -04:00
-->
2020-12-03 06:29:52 -05:00
# How to spoof MAC address and hostname automatically at boot on macOS
2020-06-18 10:59:51 -04:00
2023-09-06 19:28:21 -04:00
[](https://www.youtube.com/watch?v=ASXANpr_zX8 "How to spoof MAC address and hostname automatically at boot on macOS")
2020-06-18 10:59:51 -04:00
2021-09-27 07:53:59 -04:00
> Heads-up: unfortunately, it is not possible to spoof MAC address on some Macs (see [issue](https://github.com/sunknudsen/privacy-guides/issues/15)).
2020-06-18 10:59:51 -04:00
2021-11-05 09:07:15 -04:00
## Requirements
- Computer running macOS Catalina or Big Sur
2020-08-25 07:40:01 -04:00
## Caveats
- When copy/pasting commands that start with `$` , strip out `$` as this character is not part of the command
- When copy/pasting commands that start with `cat << "EOF"` , select all lines at once (from `cat << "EOF"` to `EOF` inclusively) as they are part of the same (single) command
## Guide
2021-05-21 13:21:41 -04:00
### Step 1: create `/usr/local/sbin` directory
2020-06-18 10:59:51 -04:00
```shell
2020-08-25 07:40:01 -04:00
sudo mkdir -p /usr/local/sbin
2021-01-13 08:49:10 -05:00
sudo chown ${USER}:admin /usr/local/sbin
2020-06-18 10:59:51 -04:00
```
2021-05-21 13:21:41 -04:00
### Step 2: add `/usr/local/sbin` directory to `PATH` environment variable
2020-09-26 07:20:10 -04:00
```shell
2021-06-28 10:44:39 -04:00
echo 'export PATH=$PATH:/usr/local/sbin' >> ~/.zshrc
2020-09-26 07:20:10 -04:00
source ~/.zshrc
```
2021-09-27 07:53:59 -04:00
### Step 3: download [first-names.txt](./first-names.txt)
2022-03-10 07:32:45 -05:00
This list includes top 2048 most popular names for people aged 25 from the [USA Social Security Administration ](https://www.ssa.gov/oact/babynames/limits.html ) (last [updated ](./misc/update-first-names.sh ) on March 9th 2022).
2021-09-27 07:53:59 -04:00
```shell
2024-05-11 06:22:07 -04:00
curl --fail --output /usr/local/sbin/first-names.txt https://raw.githubusercontent.com/sunknudsen/privacy-guides/master/how-to-spoof-mac-address-and-hostname-automatically-at-boot-on-macos/first-names.txt
2021-09-27 07:53:59 -04:00
```
2022-03-10 07:32:45 -05:00
### Step 4: create `spoof.sh` script
2020-06-18 10:59:51 -04:00
```shell
cat < < "EOF" > /usr/local/sbin/spoof.sh
#! /bin/sh
set -e
2021-04-17 16:33:15 -04:00
set -o pipefail
2020-06-18 10:59:51 -04:00
export LC_CTYPE=C
2020-09-26 07:20:10 -04:00
basedir=$(dirname "$0")
2020-06-18 10:59:51 -04:00
# Spoof computer name
2021-03-16 14:23:21 -04:00
first_name=$(sed "$(jot -r 1 1 2048)q;d" $basedir/first-names.txt | sed -e 's/[^a-zA-Z]//g')
model_name=$(system_profiler SPHardwareDataType | awk '/Model Name/ {$1=$2=""; print $0}' | sed -e 's/^[ ]*//')
2020-09-10 10:10:51 -04:00
computer_name="$first_name’ s $model_name"
2021-03-16 14:23:21 -04:00
host_name=$(echo $computer_name | sed -e 's/’ //g' | sed -e 's/ /-/g')
2020-06-18 10:59:51 -04:00
sudo scutil --set ComputerName "$computer_name"
sudo scutil --set LocalHostName "$host_name"
sudo scutil --set HostName "$host_name"
2020-09-10 10:10:51 -04:00
printf "%s\n" "Spoofed hostname to $host_name"
2020-06-18 10:59:51 -04:00
2021-09-27 07:53:59 -04:00
# Spoof MAC address of Wi-Fi interface
2022-03-10 07:32:45 -05:00
mac_address_prefix=$(networksetup -listallhardwareports | awk -v RS= '/en0/{print $NF}' | head -c 8)
2021-03-16 14:23:21 -04:00
mac_address_suffix=$(openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//')
2022-03-10 07:32:45 -05:00
mac_address=$(echo "$mac_address_prefix:$mac_address_suffix" | awk '{print tolower($0)}')
2021-10-07 06:23:30 -04:00
networksetup -setairportpower en0 on
2021-09-27 07:53:59 -04:00
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport --disassociate
2020-06-18 10:59:51 -04:00
sudo ifconfig en0 ether "$mac_address"
2020-09-10 10:10:51 -04:00
printf "%s\n" "Spoofed MAC address of en0 interface to $mac_address"
2020-06-18 10:59:51 -04:00
EOF
```
2022-03-10 07:32:45 -05:00
### Step 5: make `spoof.sh` executable
2020-06-18 10:59:51 -04:00
```shell
chmod +x /usr/local/sbin/spoof.sh
```
2022-03-10 07:32:45 -05:00
### Step 6: create `local.spoof.plist` launch daemon
2020-06-18 10:59:51 -04:00
```shell
2020-08-25 07:40:01 -04:00
cat < < "EOF" | sudo tee /Library/LaunchDaemons/local.spoof.plist
2020-06-18 10:59:51 -04:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
< plist version = "1.0" >
2020-08-25 07:40:01 -04:00
< dict >
< key > Label< / key >
2022-04-05 07:33:00 -04:00
< string > local.spoof< / string >
2020-08-25 07:40:01 -04:00
< key > ProgramArguments< / key >
< array >
< string > /usr/local/sbin/spoof.sh< / string >
< / array >
< key > RunAtLoad< / key >
< true / >
< / dict >
2020-06-18 10:59:51 -04:00
< / plist >
EOF
```
2022-03-10 07:32:45 -05:00
### Step 7: create `spoof-hook.sh` script
2021-09-27 07:53:59 -04:00
```shell
cat < < "EOF" > /usr/local/sbin/spoof-hook.sh
#! /bin/sh
# Turn off Wi-Fi interface
networksetup -setairportpower en0 off
EOF
```
2022-03-10 07:32:45 -05:00
### Step 8: make `spoof-hook.sh` executable
2021-09-27 07:53:59 -04:00
```shell
chmod +x /usr/local/sbin/spoof-hook.sh
```
2022-03-10 07:32:45 -05:00
### Step 9: make sure `com.apple.loginwindow` does not exist
2021-09-27 07:53:59 -04:00
> Heads-up: if `com.apple.loginwindow` exists, one needs to backup user default carefully and consider using an abstraction that runs both current `LogoutHook` script and `/usr/local/sbin/spoof-hook.sh`.
```console
$ sudo defaults read com.apple.loginwindow
2021-09-27 06:58:02.301 defaults[2267:25227]
Domain com.apple.loginwindow does not exist
```
Domain com.apple.loginwindow does not exist
👍
2022-03-10 07:32:45 -05:00
### Step 10: configure user default (used to disable Wi-Fi interface at logout)
2021-09-27 07:53:59 -04:00
```shell
sudo defaults write com.apple.loginwindow LogoutHook "/usr/local/sbin/spoof-hook.sh"
```
2022-03-10 07:32:45 -05:00
### Step 11: reboot and confirm hostname and MAC address have been spoofed
2020-06-18 10:59:51 -04:00
2021-08-26 06:57:51 -04:00
#### Spoofed hostname
2020-12-25 10:08:11 -05:00
```console
2020-08-25 07:40:01 -04:00
$ scutil --get HostName
2022-03-10 07:32:45 -05:00
Ottos-MacBook-Pro
2021-08-26 06:57:51 -04:00
```
2020-06-18 10:59:51 -04:00
2021-09-15 09:42:41 -04:00
#### Spoofed MAC address
2021-08-26 06:57:51 -04:00
```console
2020-08-25 07:40:01 -04:00
$ ifconfig en0 | grep ether | awk '{print $2}'
2022-03-10 07:32:45 -05:00
98:01:a7:8e:0f:51
2021-08-26 06:57:51 -04:00
```
#### Hardware MAC address
2020-06-18 10:59:51 -04:00
2021-08-26 06:57:51 -04:00
```console
2020-08-25 07:40:01 -04:00
$ networksetup -listallhardwareports | awk -v RS= '/en0/{print $NF}'
2022-03-10 07:32:45 -05:00
98:01:a7:5e:d0:c2
2020-06-18 10:59:51 -04:00
```
2021-09-27 07:53:59 -04:00
“Spoofed hostname” is random and “Spoofed MAC address” does not match “Hardware MAC address”?
2020-06-18 10:59:51 -04:00
👍
---
## Want things back the way they were before following this guide? No problem!
2021-09-27 07:53:59 -04:00
### Step 1: delete files
2020-06-18 10:59:51 -04:00
```shell
2021-09-27 07:53:59 -04:00
rm /usr/local/sbin/first-names.txt
rm /usr/local/sbin/spoof-hook.sh
rm /usr/local/sbin/spoof.sh
sudo rm /Library/LaunchDaemons/local.spoof.plist
2020-06-18 10:59:51 -04:00
```
2021-09-27 07:53:59 -04:00
### Step 2: delete user default
2020-06-18 10:59:51 -04:00
```shell
2021-09-27 07:53:59 -04:00
sudo defaults delete com.apple.loginwindow LogoutHook
2020-06-18 10:59:51 -04:00
```
2021-09-27 07:53:59 -04:00
### Step 3: set computer name, local hostname and hostname
2022-02-16 16:07:46 -05:00
> Heads-up: replace `John` with your name.
2020-06-18 10:59:51 -04:00
```shell
2021-09-27 07:53:59 -04:00
sudo scutil --set ComputerName "John’ s MacBook Pro"
sudo scutil --set LocalHostName "Johns-MacBook-Pro"
sudo scutil --set HostName "Johns-MacBook-Pro"
2020-06-18 10:59:51 -04:00
```
2020-12-03 06:29:52 -05:00
2021-09-27 07:53:59 -04:00
### Step 4: reboot
2020-12-03 06:29:52 -05:00
👍