MAC address is the physical address of every system (laptop, mobile, tablet, servers, etc) that is unique for every device and permanent. It does not change from network to network like the IP address. It is generally provided by hardware manufacturer and cannot be changed normally. But still sometimes you may need to change mac address in Linux. It is known as MAC spoofing and might be required in case you don’t want to leave any trace of your access on other systems. It is used by security administrators to investigate issues. It is also used to clone MAC address of a device that is connected to your network, thereby forcing it to disconnect from the network, and reconnect again. This is because two devices cannot have same MAC address on a network. In this article, we will learn how to change MAC address of your system, manually as well as automatically, to a specific MAC address as well as a random one.
How to Change Mac Address in Linux
You can change MAC address of a system manually, or automatically. We will look at how to change MAC address manually as well as automatically.
Change MAC address Manually
First let us view our system’s current MAC address using the following command.
$ ip address show
You will see the following kind of output where MAC addresses contain 12 digits, 6 fields of 2 characters and letters separated by colons “:” like XX:XX:XX:XX:XX:XX.
The first 6 digits of MAC address is for device manufacturer while the next 6 digits for device ID. The above command displays MAC address for 2 network adapters enp2s0 and wlp3s0.
You cannot change the MAC address of a device while it is running. So we will stop one of the network devices using the following command to change its MAC address. Replace <Device> with the device name of your network adapter.
$ sudo ip link set dev <Device> down
Here is an example to stop network adapter enp2s0.
$ sudo ip link set dev enp2s0 down
Next, you can use the ip command to set the MAC address to a new value. Replace <Device> with device whose MAC address you want to change, and <New mac address> with the new mac address.
$ sudo ip link set dev <Device> address <New mac address>
Once you have changed the MAC address of network card, restart it with the following command.
$ sudo ip link set dev <Device> up
Now if you check the MAC address of your device, you will find that it has changed to the new value you have assigned.
$ ip address show
Please note, the new MAC address has to be unique across the entire network. Otherwise, you will get an error.
Also, with this approach, the MAC address reverts to the old value after system reboot. To overcome this problem, you need to change MAC address automatically.
Change MAC Address Automatically
In this section, we will learn how to change MAC address automatically, or assign a random MAC address. For this purpose, you need to install Macchanger utility using the following command.
$ sudo apt install macchanger
During installation, you will see a prompt asking you if you want Macchanger to automtically set a new MAC address for your system, every time it connects to a network.
Once macchanger is installed, you can set the MAC address of your device with the following command. The -r option below instructs macchanger to assign a random address.
$ sudo macchanger -r <Device>
Here is an example to set random MAC address automatically to your device enp2s0 mentioned earlier.
$ sudo macchanger -r enp2s0
If you get an error while running the above command, you need to shut down the device first using the following command, and then run macchanger.
$ sudo ip link set dev <Device> down
Once you have run macchanger, you need to start the device again, with the following command.
$ sudo ip link set dev <Device> up
You can also set a specific MAC address using macchanger, using the -m option.
$ sudo macchanger -m <New Mac Address> <Device>
Please note, this MAC address has to be unique across your network otherwise you will get an error.
In this article, we have learnt a couple of ways to change MAC address in Linux. Remember that if you change MAC address manually, it will be reset to the default value provided by manufacturer when you reboot your system. If you want to change MAC address every time your system boots you need to use a utility like Macchanger.
Also read:
How to Convert Permissions to Octal in Linux
How to Compare Local & Remote Files in Linux
How to Generate Strong Pre Shared Key (PSK) in Linux
How to Run Command After Certain Time in Linux
Timeout Command in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.