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:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
     inet6 addr: fe80::ba27:ebff:fefc:9fd2/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     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
  try:
    str = open('/sys/class/net/%s/address', %interface).readline()
  except:
    str = "00:00:00:00:00:00"
  return str[0:17]

This function can be called using the following line :

getMAC(“eth0”)
This entry was posted in Debian, Python and tagged , , , , , , . Bookmark the permalink.



5 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:
    ifconfig

  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?

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=""> <strike> <strong>