taylorkh
Posts: 106
Joined: Mon Nov 20, 2017 7:57 pm

Changing the MAC address on a Pi 3B+

Wed May 02, 2018 6:29 pm

I am running Ubuntu Mate 16.04 on a Pi 3B+ which I installed per Tolentino_Cotesta's workaround shown in this thread https://ubuntu-mate.community/t/ubuntu- ... -b/15867/4 Seems to work fine. I had put the Pi in place of my CentOS 7 server which was functioning as a router/firewall/dhcp/cpn sharing device on my LAN. The Pi's on-board NIC is connected to my DSL modem and a USB to Ethernet dongle serves the LAN. The modem is in transparent bridging mode so the Pi gets the IP address assigned by the ISP.

The one issue I am working on is related to my ISP's dhcp server. Even though I have a dynamic IP address for my service, the dhcp server will not honor release/renew requests for a new address. As a last resort on the CentOS box I took advantage network-nanager-applet's feature to assign a random MAC address to a NIC each time the connection is brought up. This works!

Unfortunately the network-manager-gnome application on Ubutu Mate does not have the option to set the MAC to random. I have tried the utility called "macchanger" which seems like it should do the job. My process is (run with root permissions):

mncli networking down (takes the NIC down so I can manipulate the MAC)
macchanger -a enxb827ebd88666 (assign a new MAC from the same manufacturer's range to the designated device)
macchanger -s enxb827ebd88666 (show the newly assigned and permanent MACs - I see a new MAC)
mncli networking down (bring up the NIC/connection)
macchanger -s enxb827ebd88666 (I am back to the original MAC)

Does anyone know if it is possible to change the MAC on the NIC in a Pi? Any suggestions welcomed.

TIA,

Ken

SteveSpencer
Posts: 351
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: Changing the MAC address on a Pi 3B+

Fri May 04, 2018 12:11 pm

You can do it via the /boot/cmdline.txt, see viewtopic.php?t=33252#p284683
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

User avatar
mark3112
Posts: 46
Joined: Mon Apr 09, 2018 10:39 am

Re: Changing the MAC address on a Pi 3B+

Fri May 04, 2018 12:26 pm

A problem I hit when spoofing the MAC Address on a 3B was the Pi would register with the DHCP server using its ‘proper’ MAC and then re register with the Spoofed address. As I didn’t want the original MAC Address showing up, here is what I do.

In /etc/network/interfaces – Add (Incomplete info so doesn't start but doesn't get killed either and stops the DHCP request)
iface eth0 inet static

Then in rc.local
sudo ifconfig eth0 hw ether 12:34:05:5F:5E:12 #Spoofed MAC
sudo ifconfig eth0 up # Start
sudo dhclient eth0 # Start DHCP
You can make a real CPU in a FPGA, but you can’t make a real FPGA in a CPU.

taylorkh
Posts: 106
Joined: Mon Nov 20, 2017 7:57 pm

Re: Changing the MAC address on a Pi 3B+

Fri May 04, 2018 1:41 pm

Thanks Steve S. I have seen that approach although I have not tried it. It would require that I manually "make up" the spoofed MAC and enter it in the file... I could write a program to do that but... this is simply a workaround because of the ISP's screwed up dhcp server which will not even respect a RELEASE from their supplied modem/router. I was hoping for an automated approach.

Thanks mark3112, I do not control the dhcp so I am not sure that this will help. I don't care if the ISP's dhcp server remembers the prior MAC or "real" MAC. I am just trying to get the Pi to present the new MAC to the dhcp server.

Ken

taylorkh
Posts: 106
Joined: Mon Nov 20, 2017 7:57 pm

Re: Changing the MAC address on a Pi 3B+

Wed May 09, 2018 2:04 pm

When all else fails...

Ubuntu 18.04 has an updated version of nm-applet which allows the MAC address to be automatically changed to a random value - at least on the Intel version of the distribution. I guess I will have to wait until the Pi version comes out and see if the same is true.

I checked Raspbian - nm-client will NOT allow the MAC to be changed.

macchanger and macchanger-gtk do not function on Raspbian.

This brings me back to Ubuntu Mate 16.04 and the cmdline.txt approach referenced earlier in the thread. Not wanting to edit the file by hand each time, I decided to develop a script to do the job. I knew what I wanted to do so a little searching found me the necessary commands. The only ringer I found was that cmdline.txt ends with a newline character - ASC(10) or ASC(0A). When I added my MAC data to the file it appeared on the next line and was ignored by the boot process. I found a way to fix that as is shown in the script below.

Code: Select all

#!/bin/bash

# generate and append a random Pi MAC to the /boot/cmdline.txt file 
# to assign a new MAC address to the on-board NIC at next startup.
#
# The original file is saved as /boot/cndline.orig - no NIC specfied
# Need to append " smsc95xx.macaddr=B8:27:EB:xx:xx:xx" where the xx
# part is the new MAC number (first 6 refer to Pi mfgr.)
# need 3 more hex pairs

p4=$(openssl rand -hex 1)
p5=$(openssl rand -hex 1)
p6=$(openssl rand -hex 1)
add_this=" smsc95xx.macaddr=B8:27:EB"
add_this=$add_this":"$p4":"$p5":"$p6

# save the current file just in case

mv /boot/cmdline.txt /boot/cmdline.save

# As delivered, cmdline.txt ends with a newline. This seems to hose 
# things up after adding the MAC setting string. Remove the newline
# and create the new cmdline.txt

tr -d '\n' < /boot/cmdline.orig > /boot/cmdline.txt

# write the new parmeter to the cmdline.txt file
# enclose the variable in quotes to make sure the leading space 
# is written to the file (not sure why)

printf "$add_this" >> /boot/cmdline.txt

# Finally, reboot if desired

echo To reboot now enter YES

read shoe

if [ $shoe = "YES" ];then
  shutdown -r now
else
  exit
fi

Note that I run sudo su before executing the script to deal with file permissions on /boot/ Feel free to ask if you don't understand something in the script (other than my bizarre humor in variable naming - I have been playing with this too long). I will try to explain.

Thanks again for the tips and encouragement.

Ken

User avatar
Paeryn
Posts: 2782
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Changing the MAC address on a Pi 3B+

Wed May 09, 2018 8:28 pm

taylorkh wrote:
Fri May 04, 2018 1:41 pm
this is simply a workaround because of the ISP's screwed up dhcp server which will not even respect a RELEASE from their supplied modem/router.
How do you know it isn't respecting a release? A release request is just you giving a courtesy call saying that you have finished with the address before your allotted time was up. Their server could decide to keep the ip address it last gave you in reserve for a certain length of time (as long as it doesn't need to give it to someone else) so that it can offer you the same one when you next connect, especially if you reconnect not long after you disconnected.
She who travels light — forgot something.

taylorkh
Posts: 106
Joined: Mon Nov 20, 2017 7:57 pm

Re: Changing the MAC address on a Pi 3B+

Wed May 09, 2018 9:09 pm

From my reading of RFC2131 I do not think it is supposed to give it back. Anyhow, I have my concern resolved.

I originally needed to get rid of the Internet address many years ago when someone assigned my address to their print server. My router was getting flooded by CUPS traffic until I zapped the address. I then did it routinely for privacy reasons. I use a VPN now so that is not a big concern any more. However, sometimes I find that my download speed is poor. I have found that releasing and getting a new address will often clear things up - different routing patterns on the ISP's side of things I guess. This approach is a lot less painful than arguing with the ISP's "technical" support folks.

Ken

User avatar
Paeryn
Posts: 2782
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Changing the MAC address on a Pi 3B+

Wed May 09, 2018 10:48 pm

taylorkh wrote:
Wed May 09, 2018 9:09 pm
From my reading of RFC2131 I do not think it is supposed to give it back.
From these two parts of RFC2131 I read it as the server ideally attempts to give the same address back to a client if it had previously accepted one. Emphasis added by me to show what leads me to read it this way.
The basic mechanism for the dynamic allocation of network addresses is simple: a client requests the use of an address for some period of time. The allocation mechanism (the collection of DHCP servers) guarantees not to reallocate that address within the requested time and attempts to return the same network address each time the client requests an address.
Upon receipt of a DHCPRELEASE message, the server marks the network address as not allocated. The server SHOULD retain a record of the client's initialization parameters for possible reuse in response to subsequent requests from the client.
She who travels light — forgot something.

Return to “Troubleshooting”