How to Find and Change the MAC Address on Linux?

The Media Access Control (MAC) is a unique address of the machine assigned by the manufacturers. A MAC address consists of a unique 6-byte identifier used to identify a network’s Network Interface Controller (NIC). 

This article will address multiple methods to find and change the MAC address in Linux with the following outline: 

What are the Methods to Find the MAC Address on Linux?

There are three methods to find the MAC address on Linux, i.e., using the ifconfig, ip link, and the /sys directory explained here.

Method 1: Using the ifconfig Command

The ifconfig is pre-installed on Linux distributions. If it is not installed, then it can be installed by using the command:

$ sudo apt install net-tools -y    #Debian/Ubuntu-based
$ sudo dnf install net-tools       #Fedora/CentOS/RHEL

After ensuring the command is installed, run the command:

$ ifconfig -a

The output will display the MAC address of all the network interfaces.

Method 2: Using the ip link Command

Another method of displaying the MAC address is using the ip link command:

$ ip link show

The MAC Addresses have been displayed.

Method 3: Using the /sys Directory

The MAC address is by using the sys directory. Display the contents of the sys directory with the command:

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

The MAC address has been displayed. 

How to Change the MAC Address Manually on Linux?

We can manually change the interface’s MAC address using the ip link command. 

Trace the Network Interface

First, trace the network interface to whom you want to change the MAC address. 

$ ip addr

Change the MAC Address 

We will change the MAC address of the enp0s31f6, we will first down the network interface by using the command:

$ sudo ip link set dev enp0s31f6 down

Then assign a new MAC address:

$ sudo ip link set dev enp0s31f6 address 00:00:00:00:00:02

Now, up the network interface again to apply the changes:

$ sudo ip link set dev enp0s31f6 up

Verify MAC Address

Verify the changes by running the command:

$ ip address show

The MAC address has successfully changed. 

How to Change the MAC Address Automatically on Linux?

The “macchanger” is a command line utility used to modify the MAC address automatically. 

Install MacChanger on Linux

It can be installed by using the commands:

$ sudo apt install macchanger -y        #Debian/Ubuntu
$ sudo dnf install macchanger            #RHEL-based
$ sudo pacman -S macchanger          #Arch Linux

A message will be prompted to ask for the change the MAC address automatically of the command, choose “Yes”:

Change the MAC Address (Randomly)

To assign a random MAC address using the “r” option of the MAC Changer, run the below command. The “enp0s31f6” is the name assigned to a network interface in Linux:

$ sudo macchanger -r enp0s31f6

Changing the MAC Address (User Defined)

To set the new MAC address by yourself, use the “m” option of the Mac Changer:

$ sudo macchanger -m 00:00:00:00:00:05 enp0s31f6

Following the above methods, the MAC address can be changed. 

Conclusion

To find and change the MAC address, the ifconfig command, ip command, and “/sys/class/net/*/address” file are used in the system. Additionally, users can change the MAC address using the MAC Changer (application). This post explained all possible methods to find and change the MAC address on Linux.