How to Find the MAC Address on Raspberry Pi? (3 ways)


On some networks, you can configure a MAC address whitelist, to allow only authorized devices
You probably know how to do this on other systems (Windows for example) but you need help to find it on Raspberry Pi
You are at the good place 🙂 I will tell you everything about this

How to find the MAC address corresponding to your Raspberry Pi?
A MAC address is a unique identifier assigned to your hardware, each network card has a different MAC address
The most common way to get it on Raspberry Pi is to use ifconfig in a terminal

In this tutorial, I’ll show you how to find it with ifconfig, but also other ways to get it in different situation

Find the MAC Address on Raspbian

So, the first way on Raspbian is to use ifconfig
This command is available on any Raspbian version (Lite or Desktop)

On Raspbian Lite, you can use it directly once logged
On Raspbian Desktop, you need to open a terminal, and then you can use it

Here is how to do this on Raspbian Desktop:

  • Open the terminal (shortcut in the top bar)
  • Type the ifconfig command and press enter
  • The result look like this:
  • You can see one paragraph per network card on your system
    eth0 correspond to the wired card, and wlan0 is the Wi-Fi card
    In each paragraph you can see the IPv4 and IPv6 configuration, the MAC address and a few statistics about the network card
  • The MAC address is visible after the “ether” keyword, here:
  • So, in this case, the MAC address is b8:27:eb:4f:15:95

That’s it, you can now do the same thing on your Raspberry Pi, and use the MAC address in your router configuration for example

Get the MAC Address in a network scan

If your goal is not to authorize a device to access your network, but for example to assign it a static IP address in the DHCP server, you can also scan the network to find an equipment connected on the network (including any Raspberry Pi)

To do this, I like to use Advanced IP Scanner on Windows:

  • Start by downloading it here: https://www.advanced-ip-scanner.com/
  • Install it like any other application and start it
  • Enter the network range to scan
    (I think the software will detect it automatically)
  • And press the “Scan” button
  • After a few seconds, you’ll get the full list of your network devices, like this:
  • And as you can see on the highlighted line, you can also get the Raspberry Pi MAC address this way!

Get the MAC Address in a script

The last scenario I imagine is if you need to get the MAC address in a script that you will use on many Linux systems, including some Raspberry Pi
I will show you here two ways to do this: in Python and in a Shell script

Python script

Python is a popular language on Raspberry Pi, and is also available on any operating system
So, it’s a good idea to use it for your projects

If you need to find the MAC address of a system in Python, you have several ways to do this
The way I prefer to keep it simple, is to install get-mac and use it directly in your code
Here is how to do this:

  • If not yet installed, you need to install pip on your system:
    sudo apt install python-pip
  • Then install get-mac with the pip command:
    sudo pip install get-mac
    The project page is here if you need more information
  • Once installed, you can use it in any Python script like this:
    from getmac import get_mac_address

    eth_mac = get_mac_address()
    print(eth_mac)
  • There are many options that you can use, for example to get the MAC address from a remote device or to specify if you want the eth0 or wlan0 address.
    All the information is on the project website

If you prefer not to install anything on your system, you can use the uuid library
The cleanest way I found to get it is like this:

import re,uuid

mac=':'.join(re.findall('..', '%012x' % uuid.getnode()))
print(mac)

uuid.getnode() returns the identifier, and you need to use join and findall to format it the correct way

Shell script

The last method I want to show you is in a shell script
In a shell script, we generally use system commands
As far as I know there is no command to directly get the MAC address, but you can read the /sys/class/net/<INTERFACE>/address file to read the MAC address currently used

You can do something like that for example:

#!/bin/sh
 
if [ -e /sys/class/net/eth0 ]; then
      MAC=$(cat /sys/class/net/eth0/address)
else
      MAC=$(cat /sys/class/net/wlan0/address)
fi

echo $MAC

This script tries to read the file corresponding to eth0
If it doesn’t exist, it reads the wlan0 file

Conclusion

That’s it, you now know how to get the current address MAC on a Raspberry Pi
You have seen the basic way (ifconfig), but also alternative methods to get it depending on your needs
If you have any other case where you need to get it, feel free to leave a comment below, so I can try to help you

As usual, thanks for sharing this post on your favorite social network if you find it useful 🙂

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

3 thoughts on “How to Find the MAC Address on Raspberry Pi? (3 ways)

  1. Hi, Patrick! I must say right away that my knowledge of networks is extremely small.
    I have a problem, my raspberry has a static IP address in dhcpcd.conf and it is not specified correctly (most likely, this is an error in the gateway or in the value of the final address).
    There is no physical access to raspberries. How can I find it on the network with incorrect settings? Can an advanced IP scanner help you find it?

    1. Hello,
      In theory, if it’s just a gateway problem, you can access it from the same network (set your computer on the same network for example)

Leave a Reply

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

Recent Content