Change MAC address

Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
I am trying to change my MAC address but im having problems. I have tried checking my MAC address with
Code:
ifconfig en1 | grep ether
and that results in my correct MAC address.

I then try to change it by
Code:
sudo ifconfig en1 ether af:f8:35:2a:26:c6
, with me wanting the MAC address to be
Code:
af:f8:35:2a:26:c6
, although when I check my mac address it stil stays as the original.

How can I change it?

I know it resets every time I reset my computer, so I was planning on ultimately writing a script that, if the WIFI is on, turn it off, then change my MAC address to a random address and then turn WIFI back on.

Any help is greatly appreciated :)
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Ok, I have corrected the error, I can now change the MAC address.

I have also the correct commands to turn the wifi on/off and also creating a random MAC number, but I dont know how I can actually create a file that executes the commands. The code I have is below

Code:
#Turn wifi off
networksetup -setairportpower en1 off

#Generate random MAC address
randomAddress = openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

#set MAC address to random address
sudo ifconfig en0 ether randomAddress

#Turn wifi on
networksetup -setairportpower en1 on
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Looking at the man page, it looks like you want the "lladdr" option instead of the "ether" one. I might be wrong but it's worth a shot.
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
I have managed to get the script working by saving it as a .sh file but I cant get the random Mac address generated correctly, I have to enter the MAC address in the script. This is fine but I would like a random one each time I run the script. Below is my script so far:

Code:
#Turn wifi off
networksetup -setairportpower en1 off

#Generate random MAC address
set randomAddress = openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

#set MAC address to random address
sudo ifconfig en0 ether $randomAddress

#Turn wifi on
networksetup -setairportpower en1 on
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
Try replacing this,
Code:
set randomAddress = openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

With this,
Code:
randomAddress=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')


Oh and after you make your script executable with
Code:
chmod +x pathToScript.sh

You can make it openable like a regular application by changing it's file extension from .sh to .command
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Perfect, thanks!
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Please don't delete your posts in the future. If someone has the same issue (or a similar one) in the future, it's nice to have the conversation and solution up for others.
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Ok. Point taken in.

I have realised it does not change the MAC address for the wifi, it only changes it if it is 'en0' which is the ethernet no?

I still have the same MAC address after running the script for some reason?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Because you've only changed it for en0. ;)

Look at line two of your script:
Code:
sudo ifconfig en0 ether $randomAddress
Change that to en1 and you should be good to go.
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Even after changing that to en1, I got an error. The script ran but does not change the en1 MAC address

Code:
my-macbook:RND Me$ ./test.sh
en1: flags=8823<UP,BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
	ether 00:e3:e4:xx:xx:xx

I have removed the last 6 parts of my MAC address. This is the same as my old MAC address for some reason
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
If you do the four commands individually, do they work then?

I also just noticed that the script requires elevated privileges at points. Are you passing the script your user password at any point?
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Yes the 4 of them work individually. I have had to modify the script, to get it kind of working, although the random number implementation is not working.

I have had to hard code the new MAC address in, which is not great as I want it to change randomly whenever the script is ran. Below is the code I have which changes the wifi MAC code to a pre defined one.

Code:
#Turn wifi off
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z 
sleep 10

#Generate random MAC address
randomAddress=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')

#set MAC address to random address
sudo ifconfig en1 ether cc:cc:dd:dd:ee:ff

#Turn wifi on
networksetup -setairportpower en1 on

Can somebody please suggest why, when I change the script to encorporate the random generator, it does not change the MAC address?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
As I asked earlier, how are you passing your password to satisfy that requirement for sudo? If you don't execute it with the elevated privileges (executing the script with sudo) or pass it your password, it's going to fail.
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
Oh, sorry. I am not passing it my password, I thought because I am the admin, and log in as admin I would not need the password. How can I pass the password to the script?

Does the script work for you when you run it, if you pass it your password?
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
The only secure way to do this is like this, replace the filepaths/username with your own path/username. You can find the path by dropping the script into a terminal window or running it.

Step 1
sudo chown root:root /path/filename.sh
sudo chmod 700 /path/filename.sh



Step 2
Set up sudo to allow filename.sh to execute without requiring a password

Type sudo visudo at the terminal to open the sudo permissions (sudoers) file
Around line 25, you'll see this line: %sudo ALL=(ALL:ALL) ALL

Below that line, insert the following line, where username is your username:
username ALL=(ALL) NOPASSWD: /path/filename.sh

Depeding what your system is set to either vim or nano will open the file for you to edit.
For saving the file via the nano editor: Exit the editor (Ctrl+X)
For saving the file via the vim editor: Press [Esc] key and then : (the colon) then type wq and hit enter
Collected from:
bash - How do I sudo a command in a script without being asked for a password? - Ask Ubuntu
HowTo: Save File in Vi / Vim Text Editor
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
When i run the first command in step 1 I get the following error:

Code:
my-macbook:Desktop Me$ sudo chown root:root /Users/Me/Desktop/test.sh
Password:
chown: root: illegal group name
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
Alright, I made a slight mistake…
It would seem this is the only way to do this, or my googlefu is failing me.

Ignore step 1.

Step 2. ( Updated )
Set up sudo to allow filename.sh to execute without requiring a password

Type sudo visudo at the terminal to open the sudo permissions (sudoers) file
Around line 25, you'll see this line: %admin ALL=(ALL) ALL

Below that line, insert the following line, where username is your username:
username ALL= NOPASSWD: /path/filename.sh
Exit the editor. Hit esc, then : and then enter wq and hit enter

Done! ( Self tested this time :p )

Only downside is that this method doesn't allow you to have a .command file to double-click.

But it will work without password if you run the command sudo path/to/file.sh.
But since terminal has a command log, you can always just open terminal, hit the up key to the command you want to use and hit enter.

By the way what Mac OS are you running? If I run a .sh file on Mavericks that needs sudo permissions somewhere inside, terminal asks me for my password.
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
That is infact working now, thanks very much for your help :)

Im running Mavericks
 
OP
imfromwales
Joined
Apr 11, 2013
Messages
194
Reaction score
2
Points
18
Your Mac's Specs
13" rMBP 2014, 8GB RAM, 128GB SSD, 2.6GHz i5
How can i determine my original MAC address, after it has been changed without looking at the hardware, a terminal script?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
In your script, get your MAC address before changing it and redirect the output to a text file:
Code:
ifconfig en1 | grep ether > ~/Desktop/macaddr

That's not the cleanest but it gets the job done.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top