How To Change MAC address In Linux
If you don’t want to expose the real MAC addres 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 might have 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 CentOS 7 and Ubuntu 18.04 LTS server editions. It worked well as described below. Let us get started.
Find MAC address in Linux
First, let us 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 it automatically, unless you really need to change the MAC id every time. So, choose No and hit ENTER key to continue.
Macchanger has been installed.
To assign any random MAC address, run:
$ macchanger -r <interface-name>
To find out the network interface name, run:
$ ip addr
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:
$ ip addr
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:
$ macchanger --mac=XX:XX:XX:XX:XX:XX
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
However, you don’t have to do this. 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:
$ man macchanger
Method 2: Using iproute2
First, turn off the Network card using command:
$ sudo ip link set dev enp0s3 down
Next, set the new MAC is using command:
$ sudo ip link set dev enp0s3 address XX:XX:XX:XX:XX:XX
Finally, turn it on back with command:
$ sudo 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.
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 hw ether 00:80:48:BA:d1:30
$ sudo ifconfig eth0 up
$ ifconfig eth0 | grep HWaddr
As you can see, both methods are pretty easy. Choose any one that works for you. To revert back to the original MAC address, simply reboot your system. Hope this helps. If you know any other method to change the MAC address, share them in the comment below.