linux mac address commands

How To Find The MAC Address On Ubuntu / Linux (4 ways)

The MAC address is a unique identifier assigned to each network adapter that you can use to whitelist authorized devices or assign a specific IP address to each device. But finding it for your computer might not be that easy, that is why I explain how to do this in this tutorial.

The easiest way to find the MAC address on any Linux computer is to use the command “ip link”. This will show all the network interfaces and the corresponding MAC address for each one. A MAC address is represented as a 12-digit hexadecimal number (AA:BB:CC:DD:EE:FF).

In this tutorial, I’ll show you how to find it with the command line, as well as other ways to find it in different situations.

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

Find the MAC address in the desktop environment

If you have a desktop environment installed on your computer, you don’t really need to use the terminal, you can find it in a few clicks in your network settings.

On Ubuntu, for example, you can follow these instructions:

  • Click on the network icon in the top-right corner.
  • Then click on “Settings”.
  • Click on the wheel next to your active network connection.
    In this example, I use a wired connection, so it will be slightly different in Wi-Fi.
  • A screen like this will show up:
  • The MAC address is highlighted on my screenshot.
    It’s indicated as “Hardware address” in this popup, you’ll find it easily.

That’s it, that’s pretty quick and easy if you have a desktop interface.
Let’s now learn how to do the same thing from a terminal, or even if you don’t have access to the computer yet.

If you are also interested in finding the current IP address on Ubuntu, you can click on the link to read my complete guide.

Find the MAC address with the command line

The command “ip link” will show all the network interfaces existing on the computer, and the MAC address is mentioned for each of them, after the keyword “link/ether”.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Here is what it looks like on my test virtual machine:

In general, “eth0” will be the wired connection, and “wlan0” the wireless connection.
The other ones are probably useless in most cases.

So, in this example, I know that the MAC address for my wired connection is “00:15:5d:de:19:05”.
I can now use it to assign a static IP address to this computer or allow the connection if I have a whitelist system on my network.

If you know the network interface name and want to only show this one, you can specify it in the command line:
ip link show <identifier>
For example:
ip link show eth0

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

This way, I only get what I’m looking for.

Using the command line to find the MAC address is also quick, but you need to remember the command line, which for some people is not that easy. So, either way, you got your MAC address with one of these methods.

But what if you don’t have access to your system (for a server without a monitor, for example)? Don’t worry, I have another solution for you.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

Get the MAC address from a network scan

If the device is already connected to the network, and you want to get the MAC address without connecting to it, you can also scan the network to find any equipment connected to the network.

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

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
  • Start by downloading it here: https://www.advanced-ip-scanner.com/.
  • Install it like any other application and then 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, each connected device is displayed with the hostname, the current IP address, and the MAC address.

If you don’t have a Windows computer, you can use nmap on Linux to get something similar.
You’ll first need to install it with:
sudo apt install nmap
And then run a scan with:
sudo nmap -sn 192.168.1.0/24
Adjust the range depending on your network setup.

You’ll get something like the image below, with all the devices listed, including the IP address, hostname, and MAC address:

Don’t forget to use sudo (or root), it won’t show the MAC address with a normal user.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Get the MAC address in a script

As a bonus solution, you can also create a script to get the MAC address of your computer. Most of the time, the previous solutions will be enough, but if you need them, here is how to create one.

Shell script

In a shell script, we generally use system commands.

As far as I know, there isn’t a 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.

It is less simple than the “ip link” command I gave you earlier, but basically, you can use:
cat /sys/class/net/eth0/address
To only get the MAC address of the wired interface.

So if you want to create a script:

  • Create a new file, for example:
    nano get-mac.sh
  • Paste the following lines in it:
    #!/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
  • Add the execution right:
    chmod +x get-mac.sh
  • Run the script with:
    ./get-mac.sh

This script tries to read the file corresponding to eth0.
If it doesn’t exist, it reads the wlan0 file. Adjust the interface names if needed.

Python script

Another option can be to use any programming language instead of a simple shell script. Let’s try it with Python, for example.

If you need to find the MAC address of a system in Python, there are several ways to do this.
I prefer to keep it simple by installing get-mac and using it directly in my code.

Here is how to do this:

  • If not yet installed, you need to install pip on your system:
    sudo apt install python3-pip
  • Create a virtual environment if you don’t have one already:
    sudo apt install python3-full
    python3 -m venv /home/$USER/python
  • Activate your virtual environment, for example:
    source python/bin/activate
  • Then install get-mac with the pip command:
    pip install get-mac
    The project page is here if you need more information.
  • Once installed, you can create a new Python file:
    nano mac.py
  • And use it in your Python script like this:
    from getmac import get_mac_address

    eth_mac = get_mac_address()
    print(eth_mac)
  • Then run the script with:
    python3 mac.py
  • There are many options 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.

If you are new to Python programming, I highly recommend starting with this article, which will explain the basics. It’s not complicated, but you have to learn in the correct order before trying this.

Anyway, I hope this article was useful, and that you’ll remember at least one way to find the MAC address on your Linux system. The two first solutions are probably the easiest ones to use, but I gave you other options if needed.

If you need more help with your Linux systems, I have many tutorials on this website, so feel free to check some of them listed here or use the search engine:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts