How to Retrieve the MAC Address of Your Raspberry Pi

Jinku Hu Feb 02, 2024
  1. Retrieve MAC Address Using ip Command on Raspberry Pi OS
  2. Retrieve MAC Address Using ifconfig Command on Raspberry Pi OS
How to Retrieve the MAC Address of Your Raspberry Pi

This article will introduce several methods to retrieve the MAC address of your Raspberry Pi device.

Retrieve MAC Address Using ip Command on Raspberry Pi OS

Using the ip command on Raspberry Pi OS, you can retrieve MAC addresses.

Generally, the ip command is preinstalled on most Linux distributions, and it can be utilized to display or manipulate network interfaces, routing, and other related parameters.

We must specify the link object as a command argument to retrieve link-layer information in such a case.

Since the MAC addresses are unique identifiers for NICs (network interface controllers), you’ll find that the ip link command returns several entries.

On Raspberry Pi devices that have both wired and wireless network interfaces, there should be two entries named eth0 and wlan0 (not considering the loopback interface).

ip link

Output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:16:3e:e2:52:1c brd ff:ff:ff:ff:ff:ff

A similar output, including MAC addresses, can be printed using the ip a command, but its output can be more complicated to read for beginners.

ip a

Output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
---
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:e2:52:1c brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.12/24 brd 192.168.0.255 scope global 
---

However, printing the needed MAC address for the given interface would be nice. The following commands can be utilized to extract MAC address lines from the outputs of the previous commands.

Notice that one of the commands is for wired interface MAC address (includes eth0) and wireless (includes wlan0).

ip link | grep -A 2 eth0 | grep link
// OR
ip link | grep -A 2 wlan0 | grep link

Output:

link/ether 00:16:3e:e2:52:1c brd ff:ff:ff:ff:ff:ff

Retrieve MAC Address Using ifconfig Command on Raspberry Pi OS

Another useful command-line utility for listing MAC addresses is ifconfig. The latter usually comes preinstalled on the latest Raspberry Pi OS releases.

The ifconfig command is also used for many network-specific configurations, so we will only utilize it to display currently active network interfaces.

The output should usually include several Raspberry Pi devices with wired and wireless NICs. We use the grep command to extract relevant lines since the ifconfig output can be cumbersome.

ifconfig | grep -A 4 eth0 | grep ether 
// OR
ifconfig | grep -A 4 wlan0 | grep ether 

Output:

ether 00:16:3e:e2:52:1c txqueuelen 1000 (Ethernet)
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Raspberry Pi