How to Get the MAC Address of a Network Interface Card

You are about to learn what a MAC address is, where they are stored, how they are assigned, the relationship between MAC addresses and IP addresses, and finally, how to get the MAC address of a network interface card.

What is a Mac Address?

The Media Access Control (MAC) address, also referred to as a physical or hardware address, is a unique identifier of the network interface card (NIC) set at the time of device manufacturing. It consists of 48-bit characters grouped into six pairs of two hexadecimal digits separated by a colon (:) in 00:14:22:14:db:db format.

Mac address attached to hardware device
MAC address attached to the hardware device

Where are they stored?

The address is fed into the Network Interface Card (NIC) read-only memory (ROM) or BIOS system by the NIC manufacturer. There are various NIC manufacturers: Broadcom, Buffalo Technology, D-Link, Huawei, Dell, Cisco, etc.

How are they assigned?

The first three groups of two hexadecimal digits are used to identify the NIC manufacturer, called OUI (organizationally unique identifier). The OUI will always be the same for all the NICs manufactured by that specific manufacturer.

mac address
MAC address

For example, my system MAC address is 00:14:22:14:db:db where the first three sets of this address are 00:14:22 considered OUI. The 00:14:22 identifies the manufacturer as Dell, and the other three sets are used to determine the device.

Click here to find your MAC address manufacturer

Surprisingly, this OUI is registered and assigned to the manufacturer by the IEEE, the same organization responsible for assigning IPv4/IPv6 addresses.

Relationship Between MAC Address & IP Address

The relationship between a MAC address and an IP address is essential, and both combinations are the way to access the global internet.

You know, the MAC address is the unique physical/hardware address assigned during manufacturing. Let us understand local and global IP to find the MAC address place when accessing the internet.

A local IP address is assigned to you by the mediator (routers, switches, etc.) for local communication between devices without any connection to the outside world.

local network
local network

A global IP address is assigned to you by the internet service provider (ISP) to help your local network join the global internet.

global network
Global Network

The MAC address plays an essential role in the time of data communication over the internet. As you know, the global IP address used to connect with the internet is a single address specified for the whole local network.

The mediator (router, switches, etc.) assigns a unique local IP address to all devices connected to it and stores the record in its ARP table with the device’s MAC address.

So, whenever data is requested from the devices connected through the mediator, the data travels through the mediator, where the mediator records the local IP with the device MAC address and matches it with the ARP table. If the match is found, the data will travel through the internet and the returned data will be sent to the requested system with the help of the ARP table.

What is the ARP table?

The Address Resolution Protocol (ARP) table is a mediator (router, switches, etc.) feature to keep a record of the connected local IP address and MAC address of the devices for internal communication. Execute arp -a to display the ARP table of your network.

I hope you understand the workings of the internet and how important MAC addresses are. Let’s move to the next topic and learn how to find the MAC address of a network interface card.

How to Get the MAC Address of a Network Interface Card

There are various ways to find the system MAC address using the ip command, ifconfig command, and reading the device information file for the MAC address.

Using the ip command

The easiest way to find the MAC address for the network interface card is by using the ip command.

$ ip ad
OR
$ ip addr show

Below is the output of the above command.

Finding the MAC address using the IP command
Finding the MAC address using the IP command

When executing the above command, the highlighted section will change to your device’s MAC address.

If you want to output a specific network interface card MAC address, you can specify the device name after the ip ad show command, as shown below.

$ ip ad show wlan0

Below is the output of the above command.

Finding the MAC address using the IP command
Finding the MAC address of a specific NIC using the IP command

If you are just interested in knowing the MAC address of the network interface card, follow the below command.

$ ip ad show wlan0 | grep ether

Below is the output of the above command.

Finding just the MAC address of a specific NIC using the IP command
Finding just the MAC address of a specific NIC using the IP command

Using the ifconfig command

The ifconfig and ip commands are identical. If you are familiar with the ip command, then you can easily apply the same methods to the ifconfig command.

If the ifconfig command is not found, execute sudo apt install net-tools to install the utility.

Execute the below command to know the IP address of all network interface cards.

$ ifconfig

Below is the output of the above command.

Finding the MAC address using the ifconfig command
Finding the MAC address using the ifconfig command

Execute the below command to find the specific network interface card MAC address.

$ ifconfig wlan0

Below is the output of the above command.

Finding the MAC address of a specific NIC using the ifconfig command
Finding the MAC address of a specific NIC using the ifconfig command

Execute the below command if you are just interested in knowing the MAC address of the network interface card.

$ ifconfig wlan0 | grep ether

Below is the output of the above command.

Finding just the MAC address of a specific NIC using the ifconfig command
Finding just the MAC address of a specific NIC using the ifconfig command

Find MAC Address from /sys in Linux

Most of the Linux distributions store all the network interface card devices’ information in the /sys directory, from where you can find the MAC address of all or specified interfaces.

Execute the below command to fetch all the network interface card MAC addresses.

$ cat /sys/class/net/*/address

Below is the output of the above command.

Find MAC address from sys in Linux
Find MAC address from sys in Linux

If you have multiple network interface cards and want to display the MAC address of the specific device, use the below command.

$ ls /sys/class/net/                           # List all NIC device in your system
$ cat /sys/class/net/wlan0/address             # Output the MAC address of wlan0 NIC

Below is the output of the above command.

Find specific NIC MAC address from sys in Linux
Find specific NIC MAC address from sys in Linux

Extracting the MAC Address for Shell Variable

While working on a shell script, you are only interested in knowing the network interface card list and their MAC addresses. This can be achieved using a regular expression with the commands we learned till now.

We will use regex to extract just the MAC address for a shell variable or shell script from the commands we learned so far.

$ ip link show wlan0 | grep link/ether | awk '{print $2}'  # Filter MAC address from ip command
OR
$ ifconfig wlan0 | grep -o -E ..:..:..:..:..:..            # Filter MAC address from ifconfig command
OR
$ cat /sys/class/net/wlan0/address                         # Filter MAC address from specified NIC

Once it finds the matching pair it will list it, as shown below.

Extracting the MAC Address for Shell Variable
Extracting the MAC Address for Shell Variable

Since this returns just the MAC address, you can easily assign a variable to it for use as the MAC address in your shell script, as follows.

$ MAC=`ip link show wlan0 | grep link/ether | awk '{print $2}'`
$ echo $MAC

Below is the output of the above command.

Output the variable value
Output the variable value

I have shown assigning the ip command to a shell variable, but you can replace it with any of the mentioned commands by encapsulating it with the grave accent (`) symbol.

I hope this guide will fulfil all the requirements you have. If you have any queries, then let us know in the comment section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments