Finding the MAC Address of a Raspberry Pi


It can sometimes be useful to obtain the MAC address of your Raspberry Pi. The Media Access Control address is a unique identifier given to all networked devices. The address is different for all Pi’s and can be used to identify your device. Think of it as a digital fingerprint.

There are a number of ways to identify it using the command line or using Python code. Below are some quick examples you can use to find the MAC address.

From the Command Line

To find the MAC address from the command line you can use the following command :

cat /sys/class/net/eth0/address

or you can type :

ifconfig eth0

You can swap “eth0” for “wlan0” if you have an active wireless connection.

This will result in output similar to :

eth0 Link encap:Ethernet  HWaddr c7:35:ce:fd:8e:a1
     inet addr:  Bcast:  Mask:
     inet6 addr: fe80::ba27:ebff:fefc:9fd2/64 Scope:Link
     RX packets:336 errors:0 dropped:0 overruns:0 frame:0
     TX packets:304 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:27045 (26.4 KiB)
     TX bytes:43758 (42.7 KiB)

The “HWaddr” is the MAC address. In this example “c7:35:ce:fd:8e:a1”

Finding the MAC Address Using Python

To get the MAC address into a Python variable you can use the following example code :

# Read MAC from file
myMAC = open('/sys/class/net/eth0/address').read()

# Echo to screen
print myMAC

The following Python function can be used to obtain the MAC address of your Raspberry Pi :

def getMAC(interface):
  # Return the MAC address of interface
    str = open('/sys/class/net/' + interface + '/address').read()
    str = "00:00:00:00:00:00"
  return str[0:17]

This function can be called using the following line :


Or if you have a WiFi connection :



    • ifconfig and friends are wrappers to the /sys filesystem.

      Isn’t there something nice and simple about read()ing and write()ing to a file rather than spawning another program, and parsing the output?

  1. Several minor fixes and improvements to the Python code:

    1. The open command should not have a comma in the arguments.
    2. readline() could just be read() as there will never be more than one line.
    3. Instead of returning str[0:17] just return str.strip() and it will remove the trailing newline.
    4. (Stylistic only) Try to avoid “str” as the name of a variable, as it masks the str type which is a builtin function. Obviously not important in a tiny function like this but it’s a good practice not to mask builtin names… some day you’ll avoid wasting hours troubleshooting the obscure bugs caused by doing such things.

    • Thanks for the tips. I’m going to be tidying up this function soon so I will take on board all your suggestions. “str” is a stupid name for a variable and I’m not even sure how I didn’t notice that at the time!

  2. Thank you for this! I installed Pidora and ifconfig wasn’t there! Thank you for posting both. I have been working with Linux for years and thanks to ifconfig I had no idea about the underlying file structure.

  3. Alternative method: Plug your Rpi via ethernet cable to your router, switch on, wait a minute, and then check your routers status page for it’s list of connected devices: If it can see the Pi, the MAC address will probably be listed there (works with my “EE Brightbox” router anyway)

Leave A Reply