Every network interface has its own unique MAC address. Unlike IP addresses, which can change frequently and easily, MAC addresses are permanently tied to the hardware.
In this article, you will learn how to obtain the MAC address on a Ubuntu Linux system via many ways:
- With ip command
- With ifconfig command
- With address file
- With getmac.sh file
- With GUI
1. With ip command
Start by opening a terminal and using the following ip command to view information for all installed network interfaces.
Command: ip a
In below screenshot, we have highlighted our MAC address for the ens33 interface.
With the help of some regular expressions, you can also find the MAC address
Command: ip a | grep ether | cut -d ” ” -f6
Alternatively, you can also try the below command:
Command: ip link show ens33 | grep link/ether | awk ‘{print $2}’
Command: ip a | grep link/ether | awk -F ” ” ‘{print $2}’
2. With ifconfig command
ifconfig in short “interface configuration” utility for system/network administration in Unix/Linux OS to configure, manage and query network interface parameters via command-line interface or in a system configuration scripts.
The “ifconfig” command with no arguments will display all the active interfaces details such as IP Address, MAC Address, Interface Name, Netmask Address etc.
Command: ifconfig
The below command also gives you the exact information (MAC address) of your ens33 interface:
Command: ifconfig ens33 | grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’
Or in simple way, you can also use the following command:
Command: ifconfig | grep ether | cut -d ” ” -f 10
Command: ifconfig | grep “ether*” | tr -d ‘ ‘ | tr -d ‘\t’ | cut -c 6-22
3. With address file
If you just need to pull MAC addresses for a system and pair to an interface, you can easily get it done via below Bash command.
Command: cat /sys/class/net/*/address
In case, if you want to get the details of particular interface, then you can replace the * with your interface name:
Command: cat /sys/class/net/ens33/address
You can also use the following command:
Command: cat /sys/class/net/*/uevent /sys/class/net/*/address | grep -v IFINDEX | column | sed ‘s/INTERFACE=//’
4. With getmac.sh file
The below getmac.sh bash file can also be used to find the MAC address of any network adapter. To be able to do this, you will need the following piece of code:
#!/bin/bash
# Powered by Techhyme.com
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q up $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done
Change the permissions of getmac.sh file and execute the file by typing the following command:
Command: ./getmac.sh
5. With GUI
The following instructions will vary depending on which desktop environment you are using. However, all desktop environments will provide a way for the user to obtain their MAC address.
In the following screenshots, we are using the GNOME desktop environment of Ubuntu.
Open up your system’s network settings menu. On GNOME, you can access it by clicking the upper right corner and clicking on Wired/Wi-Fi Settings.
Open the settings panel for the network interface that you wish to obtain the MAC address of. In the case of GNOME, that is done by clicking on the corresponding cog wheel as shown in below screenshot.
Our MAC address can now be seen under the label of Hardware Address as highlighted in below screenshot.
We recommend using the command line method, as it is universal across all Linux systems and should work regardless of which Linux distribution you are on.
You may also like:- Top 40 Useful Linux Command Snippets
- Using Elasticsearch Ingest Pipeline to Copy Data from One Field to Another
- Top 10 Useful Windows Commands
- Essential Commands For Process Management in Kali Linux
- How To Install Python 2.7.18 From The Source
- How To Parse SSH Authentication Logs with Logstash
- A Comprehensive Guide to File System Commands in Linux
- Essential File Compression Commands in Linux
- Secure Shell (SSH) Protocol – A Comprehensive Guide
- Monitoring Active Connections in Kali Linux Using Netstat