How To Change MAC Address in Linux [2024]

How To Change MAC Address in Linux In 2024

In this Linux tutorial post, You can find the step-by-step guide to change the MAC address in Linux based operating system.

How To Change MAC Address in Linux In 2024

We need to take help of one tool during this process. Tools will be used to change MAC addresses in Linux. Run the following command to install the tools called macchanger and net-tools packages.

For CentOS-based systems:

sudo yum install macchanger net-tools

For Arch-based systems, use the following command:

sudo pacman -S macchanger net-tools

sudo apt install macchanger net-tools

There are two methods to change the MAC address in Linux-based operating systems.

Change the MAC Address In Linux Permanently

First, run the following command to list all the network devices in Linux. Make sure that you remember the name of the interface :

ifconfig

Run the following command to see the current MAC address of the network interface:

sudo macchanger --show <interfacename>

Run the macchanger tool with the following command to change the MAC address permanently.

sudo macchanger -r <interfacename>

Run the following command to assign a particular MAC address in Linux  by specifying the MAC address.

sudo macchanger --mac=<macaddress> <interfacename>

Create a /etc/systemd/system/changemac@.service systemd file so that you can get a new MAC address each time when you boot into the system.

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

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

Now, run the following command to enable the service:

sudo systemctl enable changemac@<interface_name>.service

 

How to Change the MAC Address Temporarily In Linux

First, run the following command to list all the network devices in Linux , Make sure that you remember the name of the interface :

ifconfig

Now, run the following command to disable the device’s connection to the network to change its MAC address in Linux.
sudo ifconfig <interfacename> down

sudo ip link set dev <interfacename> down

Now run the following command to change the MAC address in Linux.
sudo ifconfig <interfacename> down hw ether <newmacaddress>

Run the following command to enable the device.
sudo ifconfig <interfacename> up

Leave a Comment