I'm experiencing some difficulties when I attempt to identify the MAC address of the built-in wifi in my raspberry pi 3 model B. My application requires the built-in wifi to always be identified as wlan0 (not to mix interface name with other wifi adapter connected)

I have had success configuring the wifi adapter manually, when I know the mac address of the adapter. Such as using the the 70-persistent-net.rules to set the interface name such as:

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="b8:27:cc:cc:cc:cc", NAME="wlan0"

However, this manual method is undesirable and I’m seeking an automated method that would allow configuring a new raspberry using an image, rather than configuring it with SSH or other ways.

Does anyone know a method to in python to read and identify the correct mac address of built-in wifi and write a new 70-persistent-net.rules to name this wifi to interface to wlan0?

I thank you all in advance!

  • possibly helpful in SO, for old questions check for newer answers: 1, 2, 3 – uhoh Dec 15 '18 at 2:36

Easiest way is by installing netifaces (prepend sudo if it doesn't work), Just do:

pip install netifaces

After that is done, check the current interface's name in the terminal by typing


and remember it or write it down. After that, paste the following in a new python script and replace 'your_interface_name' with the name you got before (in string form) and save the script with the name of your choice:

import netifaces

IFNAME = 'your_interface_name'

MAC = str(netifaces.ifaddresses(IFNAME)[netifaces.AF_LINK][0]['addr'])
print MAC #Check if MAC parsing is ok

file = open('/etc/udev/rules.d/70-persistent-net.rules','a+')
file.write('''ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="%s", NAME="wlan0"\n''' % MAC)

file = open('/etc/udev/rules.d/70-persistent-net.rules','r')
print file.read() #Check if new rules were written successfully

That's it. I don't have a Raspi with me, and I don't remember if 70-persistent-net.rules is write protected, but just run the script with sudo if it doesn't work.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.