How To Change MAC Address in Linux
In this Linux tutorial post, we will show you the step-by-step guide to changing MAC address in Linux based operating system.
How To Change MAC Address in Linux
We need to install tools to change MAC addresses in Linux. Run the following command to install 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
We will show you two methods to change the MAC address in Linux-based operating systems.
Method 1:
Change the MAC Address In Linux Permanently
First, run the following command to list all the network devices in Linux and you also need to note down the interface name using the following command:
ifconfig
Run the following command to see the current MAC address of the network interface:
sudo macchanger --show <interface_name>
Run the macchanger tool with the following command to change the MAC address permanently:
>sudo macchanger -r <interface_name>
If you want to assign a particular MAC address in Linux then run the following command by specifying the MAC address.
sudo macchanger --mac=<mac_address> <interface_name>
Create a /etc/systemd/system/[email protected]
systemd file so that you can get a new MAC address each time you boot into the system.
sudo vim /etc/systemd/system/[email protected]
Then, paste the following text inside the [email protected] 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 :
ifconfig
Now, run the following command to disable the device’s connection to the network to change its MAC address in Linux.
sudo ifconfig <interface_name> down
sudo ip link set dev <interface_name> down
Use the following command to change the MAC address in Linux.
sudo ifconfig <interface_name> down hw ether <new_mac_address>
Run the following command to enable the device.
sudo ifconfig <interface_name> up