Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we will discuss Media Access Control (MAC) addresses and learn how to find the MAC address of a network interface.

2. What Is MAC Address?

The Media Access Control (MAC) address is also called the hardware address. It is a unique value associated with a network adapter or Network Interface Card (NIC).

A MAC address consists of 48 bits represented by six pairs of hexadecimal numbers separated by colons, e.g. 00:0c:29:cc:55:5e.

While every NIC has at least one MAC address, multiport NICs have a MAC address for each port.

Furthermore, the MAC address gets detected automatically by the device driver. Then, the card uses it to determine where the data is coming from and where it should be sent to.

An example of MAC address used is the DHCP server using it to both restrict and assign IP addresses to the connected clients.

3. Finding the MAC Address

In this section, we’ll look at two methods to find the MAC address.

3.1. Using the ip Command

Let’s use the ip command to find the MAC address of our computer’s NIC:

$ ip address show
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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:cc:55:5e brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.99.91/24 brd 192.168.99.255 scope global dynamic noprefixroute ens33
       valid_lft 6999sec preferred_lft 6999sec
    ... truncated ...

Alternatively, we can shorthand this command to either ip addr show or even just ip a.

By default, it’s very verbose and displays information for all configured network devices. We can reduce the output by specifying the network interface after the show option:

$ ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:cc:55:5e brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.99.91/24 brd 192.168.99.255 scope global dynamic noprefixroute ens33
       valid_lft 4975sec preferred_lft 4975sec
    ... truncated ...

Since we are actually interested in the MAC address, so we can combine the ip command with grep, and even shorten show to s:

$ ip a s ens33 | grep ether
    link/ether 00:0c:29:cc:55:5e brd ff:ff:ff:ff:ff:ff

Here, we can see that our NIC’s MAC address is 00:0c:29:cc:55:5e.

3.2. Using the ifconfig Command

The ifconfig is a command similar to the ip command. It shows us information for all available interfaces:

$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.99.91  netmask 255.255.255.0  broadcast 192.168.99.255
        inet6 2a03:7847:2252:199:7dd2:e1f0:6395:9f7  prefixlen 64  scopeid 0x0<global>
        inet6 2a03:7847:2252:199:9589:759e:2e6f:df28  prefixlen 64  scopeid 0x0<global>
        inet6 2a03:7847:2252:199::8bbb  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::6678:3fc6:cb2f:15bb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:cc:55:5e  txqueuelen 1000  (Ethernet)
        ... truncated ...

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        ... truncated ...

We can again use grep to get only the MAC address:

$ ifconfig | grep ether
ether 00:0c:29:cc:55:5e  txqueuelen 1000  (Ethernet)

4. Storing the MAC Address Into a Shell Variable

Sometimes, we might need the address for further processing e.g., in a shell script. Let’s look at some ways to do that.

4.1. Extract the MAC Address Using grep

We can use a simple regex  or regular expression taking advantage of the fact that a MAC address consists of six blocks of hex numbers separated by colons:

$ ifconfig ens33 | grep -o -E ..:..:..:..:..:..
00:0c:29:cc:55:5e

Since this returns just the MAC address, we can store it in a variable:

$ MYMAC=`ifconfig ens33 | grep -o -E ..:..:..:..:..:..`
$ echo $MYMAC
00:0c:29:cc:55:5e

The same is true for the ip command:

$ MYMAC=`ip a s ens33 | grep -o -E ..:..:..:..:..:..`
$ echo $MYMAC
00:0c:29:cc:55:5e ff:ff:ff:ff:ff:ff

However, the result is not as neat as we expected. So, we could either use a more complex regex or employ another tool.

4.2. Extract the MAC Address Using grep and awk

When we combine the method above with awk, we get the expected result:

$ MYMAC=`ip add | grep link/ether | awk '{print $2}'`
$ echo $MYMAC
00:0c:29:cc:55:5e

5. Conclusion

In this article, we discussed how to use either ip or ifconfig to find the MAC address of a network interface. We also learned how to use grep or a combination of grep and awk to easily assign it to a variable for further processing during scripting.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!