How to Change MAC Address in Linux

The MAC address is a unique identifier assigned to every device connected to a network. Even though the MAC address is permanent, you may want to change it on your device in some cases. For example, you can change the MAC address on your device to hide your identity or to resolve network compatibility issues. With that in mind, this guide will show you how to change the MAC address on a Linux device. So whether you’re a beginner or an experienced Linux user, follow this guide to modify the MAC address in a quick and easy manner.

Changing the MAC Address in Linux (2023)

What is MAC Address?

As mentioned above, the MAC Address (or Media Access Control address) is a unique identifier used to recognize devices connected to a network. This address is used to identify and communicate with other devices, be it a computer, smartphone, or printer, on a network.

The MAC address is a 48-bit hexadecimal address consisting of six sets of two digits or characters separated by colons or hyphens. It is also referred to as the Physical address or Burned-in address. That’s because the MAC address is assigned by the manufacturer and is burned into the device’s hardware. So it usually cannot be changed, or at least they don’t change on their own like an IP address.

Difference Between MAC Address and IP Address

Each network device needs at least two addresses to get recognized by other devices and network interfaces – one is the MAC address, and the other is the IP address (Internet Protocol address). While the former is used to uniquely identify devices on a network, the latter helps identify a device’s connection to the network. That means an IP address makes it easier to locate your device, so the network knows where to send your data. Moreover, the IP address gets assigned by the Internet Service Provider(ISP), and the MAC address is assigned by the manufacturer, as we mentioned above.

However, we can change the MAC address using some neat software tricks, which we will learn in this article. Unlike an IP Address, which can be changed permanently, the MAC address gets reverted to the original manufacturer’s address when you reboot the device.

Why You May Want to Change the MAC Address?

There may be numerous reasons why you want to change the MAC address on your Linux computer. Changing the MAC address makes the network devices treat you like a new individual. This can make you completely anonymous on a public network, hence, protecting you from cyberattacks on a public network. You can also get access to unlimited free public Wi-Fi at airports, cafes, etc., by changing your device’s MAC address.

The MAC address change can also be used for some illegal activities, such as impersonating the admin of an organization. By changing your MAC address to that of the admin, you can gain illegal access to restricted networks. However, we condemn any such malicious actions and advise against them.

Installing Package to Change MAC Address in Linux

There are several Terminal tools, such as macchanger, net-tools, etc., that can help you change the MAC address on your Linux PC. Here, we have listed the Linux commands to install both packages, so follow along.

To install macchanger and net-tools packages on a Debian-based system, use the following command:

sudo apt install macchanger net-tools

For CentOS-based systems, use the following command to install the packages:

sudo yum install macchanger net-tools

To install the two packages in Arch-based systems, use the following command:

sudo pacman -S macchanger net-tools

While installing the macchanger package, it displays a prompt asking users whether they wish to change the MAC address every time they boot into the system or not. Use the arrow keys to navigate the choices. Choose either Yes or No, depending on your preference. Then, hit Enter to confirm your choice.

macchanger prompt during installation in Linux

How to Change the MAC Address Temporarily

Step 1: Checking for Network Devices

Before you change the MAC address, you need to know what is the name of the device and its current MAC address. To list all the network devices present on your system, use this command:

ifconfig

For older systems or in case of any errors, use the following command:

ip addr show

When you run the command, the first part of the output displays the Loopback address information, which can be recognized with the lo label and is used for diagnosing any problems in the network.

The second part shows details about the network interface, which is eth0 in this example. Furthermore, the ether sub-label specifies the hardware address or the MAC address, which is 08:00:27:05:10:68 in our case. The inet sub-label specifies the IPv4 IP address and the inet6 sub-label specifies the IPv6 IP address.

Step 2: Disabling the Network Device

Now that you have noted your network interface name, you first have to disable the device’s connection to the network to change its MAC address in Linux. Disable the device using the following command:

sudo ifconfig <interface_name> down

When you run this command, you will get disconnected from the internet. If the above command does not work on your system, you can either reinstall the net-tools package or use the following command:

sudo ip link set dev <interface_name> down

Step 3: Changing the MAC Address

After you have disabled the device’s network connection, you can now change the MAC address. Use the following command to change the MAC address in Linux.

sudo ifconfig <interface_name> down hw ether <new_mac_address>

Then, you can enable the device again using this command. Run the commands in order for the desired result.

sudo ifconfig <interface_name> up

In case of any errors, use the following command to change the MAC address and enable the device’s network connection:

ip link set dev <interface_name> address <new_mac_address>

ip link set dev <interface_name> up

Step 4: Verifying the Changes Made

To verify that the MAC address of your Linux system has changed successfully, run the following command:

ifconfig

And in case of any errors or if your system is old, use the following command:

ip addr show

How to Change the MAC Address Permanently

Step 1: Checking for Network Device

Like the previous section, you first need to list all the network devices in the system and note down the interface name using the following command:

ifconfig

To see the current MAC address of the network interface, use the following command:

sudo macchanger --show <interface_name>

Step 2: Assigning A New MAC Address

When using the macchanger tool to change the MAC address permanently, you do not need to disable the device network connection and re-enable it. You can directly assign a random MAC address to your PC using the following command:

sudo macchanger -r <interface_name>

To assign a particular MAC address in Linux use the command below. You will need to specify the MAC address (6 sets of two digits or characters separated by colons) you wish to assign to your Linux system. Here is what the syntax looks like:

sudo macchanger --mac=<mac_address> <interface_name>

For example, we have changed the mac address to 00:00:00:31:33:73 for the interface eth0 using the command below.

sudo macchanger --mac=00:00:00:31:33:73 eth0

Step 3: Making Changes Permanent

1. To get a new MAC address each time you boot into the system, you can create a /etc/systemd/system/changemac@.service systemd unit file using a Linux text editor of your choice. For that, type the following command in the Terminal:

sudo vim /etc/systemd/system/changemac@.service

2. Then, paste the following text inside the changemac@.service file:

[Unit]
Description=changes mac for %I
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
ExecStart=/usr/bin/macchanger -r %I
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

In the above piece of code, a new MAC address is assigned to the network interface every time you boot your Linux computer. You can add a specific MAC address using the -m option instead of -r in the 10th line, as shown below:

ExecStart=/usr/bin/macchanger -m XX:XX:XX:XX:XX:XX %I

3. Next, all you need to do is enable the service you just created using the following command:

sudo systemctl enable changemac@<interface_name>.service

Now, you are all set. Your Linux computer will automatically change the MAC address (permanently) to a new one every time you boot into a new session.

Frequently Asked Questions

Are MAC addresses permanent?

Yes, MAC Addresses are permanent and get assigned by the manufacturer of the network device. But they can be changed using some tricks in the Linux terminal, as shown above.

Do MAC addresses get reused?

Since the number of available MAC addresses is limited, manufacturers do need to reuse the MAC addresses.

How long is a MAC address?

A MAC address includes 48 bits or 6 bytes, where each byte consists of 2 hexadecimal digits. They are shown in sets of two, separated by a colon or hyphen.

Modify the MAC Address in Linux

Changing the MAC address in Linux is simple and can help improve your privacy and security while using the Internet. While you need to use the Terminal to modify the MAC address permanently, note that the steps might vary slightly depending on your Linux distro. Further, while changing the MAC address on your Linux PC, make sure it does not conflict with any other MAC address, or else both addresses will get disconnected from the network. We hope this article helped you permanently change your MAC address on your Linux PC. If you have any questions, let us know in the comments below.

#Tags
Comments 0
Leave a Reply

Loading comments...