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

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/%s/address', %interface).readline()
    str = "00:00:00:00:00:00"
  return str[0:17]

This function can be called using the following line :

This entry was posted in Python, Raspbian and tagged , , , , , , . Bookmark the permalink.

8 Responses to Finding the MAC Address of a Raspberry Pi

  1. Phil Dobbin says:

    or you can use:

    `$ ifconfig`

    & it’s the `HWaddr` numbers

  2. JacksonCE says:

    You should also mention typing the
    following in the command line:

  3. kjhank says:

    And what about good old-fashioned #ifconfig?

    • Ben Cordero says:

      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?

  4. Peter Hansen says:

    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.

    • Matt says:

      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!

  5. Lexy says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>