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, 2018 at 2:36

2 Answers 2


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!


Here's a program that gets the MAC addr for every interface


import fcntl, socket, struct
from os import walk

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

def getIPAddr(ifname):
    if (ifname == "lo"):
        return ""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        info = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
        return "No address assigned"
    return socket.inet_ntoa(info[20:24])

f = []
path = '/sys/class/net'

for (dirpath, dirnames, filename) in walk(path):

for iface in f:
    print "Interface:", iface
    print "IPv4 Addr:", getIPAddr(iface)
    print "HW MAC Addr:", getHwAddr(iface)
    print "-----------------------------"

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.