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 :
or you can type :
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 :