How to change MAC address in Linux
Have you ever wondered how to change the MAC address of a network interface card in Linux? No? It’s okay, I will show you how. But wait, why would we change it in the first place? For privacy! If you don’t want to expose the real MAC id when you are connected with a public WiFi hotspot, you can simply change it or fake it with another MAC address. Some times, the network administrator has blocked a particular MAC address in the router or firewall. In such cases, you can use this method to change the MAC id. Changing MAC address is also called spoofing MAC address or faking MAC address. In this brief tutorial, we will see how to change MAC address in Linux.
I tested this guide on Ubuntu 16.04 LTS and CentOS 7 server editions. It worked like a charm as described below. Let us get started.
Find MAC address in Linux
Before going further, let me show you how to find out the MAC address or Hardware address of a Network interface card in Linux.
ifconfig | grep HWaddr
enp0s3 Link encap:Ethernet HWaddr 08:xx:xx:xx:xx:x1
ip link show
enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:xx:xx:xx:xx:x1 brd ff:ff:ff:ff:ff:ff
ifconfig | grep ether
ether 24:xx:xx:xx:xx:x9 txqueuelen 1000 (Ethernet) ether c0:xx:xx:xx:xx:xf txqueuelen 1000 (Ethernet)
You can use any one of the above commands to find out the MAC id of a network interface card. These commands should work on almost all Linux distributions.
Now let us see how to change the MAC address.
Change MAC address in Linux
We can do this in two methods. Please be mindful that you should not do this on a live network card which is currently in use. If you do it, probably, your network connection will be terminated. Try this method with an additional card.
Method 1: Using Macchanger
Macchanger is simple utility to view, modify, and manipulate MAC addresses for your Network interface cards. It is available in almost all GNU/Linux operating systems.
On Arch Linux:
To install Macchanger in Arch Linux and its derivatives, run:
sudo pacman -S macchanger
On Fedora, CentOS, RHEL:
sudo yum install macchanger
sudo dnf install macchanger
On Debian / Ubuntu:
To macchanger in Debian, Ubuntu and its derivatives, run:
sudo apt-get install macchanger
Specify whether macchanger should be set up to run automatically every time a network device is brought up or down. This gives a new MAC address whenever you attach an ethernet cable or reenable wifi. I recommend you not to run automatically, unless you really need to change the MAC id every time.
Macchanger has been installed.
To assign any random MAC address, run:
macchanger -r <interface-name>
To find out the network interface name, run:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:xx:xx:xx:xx:x1 brd ff:ff:ff:ff:ff:ff inet 192.168.1.105/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe12:f8c1/64 scope link valid_lft forever preferred_lft forever
As you see in the above screenshot, enp0s3 is my network interface card’s name.
To change the MAC address of enp0s3 network card, run:
sudo macchanger -r enp0s3
After changing the MAC id, verify it using command:
You will now see that MAC has been spoofed.
To change the MAC address to a specific value, specify any custom MAC address using command:
Where XX:XX:XX:XX:XX:XX is the new MAC id that you wish to change to.
Finally, to return the MAC address to its original, permanent hardware value, run the following command:
macchanger -p enp0s3
You don’t have to do this. Please note that once you reboot the system, the changes will be automatically lost, and the actual MAC address will be restored again.
For more details, check the man pages:
Method 2: Using iproute2
First, turn off the Network card using command:
ip link set dev enp0s3 down
Next, set the new MAC is using command:
ip link set dev enp0s3 address XX:XX:XX:XX:XX:XX
Finally, turn it on back with command:
ip link set dev enp0s3 up
Now, verify new MAC id using command:
ip link show enp0s3
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
Alternatively, you can do this using ‘ifconfig’ command as described below.
ifconfig eth0 down
ifconfig eth0 hw ether 00:80:48:BA:d1:30
ifconfig eth0 up
ifconfig eth0 | grep HWaddr
That’s it. As you can see, both methods are pretty easy. Follow any one of the method you prefer. Want to revert back to the original MAC address? just reboot your system.
Hope this helps. If you know any other method to change the MAC address, share them in the comment below.