General Linux

How to Change MAC Address in Linux

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.

What MAC Addresses Are Used For

At the lowest networking level, network interfaces attached to a network use MAC addresses to communicate with one another. When a browser on your computer needs to grab a web page from a server on the Internet, for example, that request passes down through several layers of the TCP/IP protocol. The web address you type gets translated to the IP address of the server. Your computer sends the request to your router, which then sends it out onto the Internet. At the hardware level of your network card, though, your network card is only looking at other MAC addresses for interfaces on the same network. It knows to send the request to the MAC address of your router’s network interface.

How to Change MAC Address in Linux

I am going to show the steps for changing MAC address (also called spoofing/faking MAC address).

I tested this guide on CentOS 7 and Ubuntu 18.04 LTS server editions. It worked well as described below. Let us get started.

Changing MAC address in Linux

Let’s go through each step:

1. Find your MAC address and network interface

Use this command to get the network interface details:

# ip link show
output
1: lo:LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1:NO-CARRIER,BROADCAST,MULTICAST mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
    link/ether 94:c6:f8:a7:d7:30 brd ff:ff:ff:ff:ff:ff
3: enp0s31f6:BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether 38:42:f8:8b:a7:68 brd ff:ff:ff:ff:ff:ff
4: docker0:NO-CARRIER,BROADCAST,MULTICAST,UP mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 42:02:07:8f:a7:38 brd ff:ff:ff:ff:ff:ff

As you can see, in this case, my network interface is called enp0s31f6 and its MAC address is 38:42:f8:8b:a7:68.
or

# ifconfig | grep HWaddr

Sample output:

enp0s3    Link encap:Ethernet  HWaddr 08:xx:xx:xx:xx:x1

2. Change MAC address 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 and you can install is using the package installer of your distribution.
On Arch Linux or Manjaro:

$ sudo pacman -S macchanger

On Fedora, CentOS, RHEL:

$ sudo dnf install macchanger

On Debian, Ubuntu, Linux Mint, Kali Linux:

$ sudo apt install macchanger

Important! You’ll be asked to 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 re-enable WiFi.

I recommend not to run it automatically, unless you really need to change your MAC address every time. So, choose No (by pressing tab key) and hit Enter key to continue.

How to Use Macchanger to change MAC address

Do you remember your network interface name? You got it in the Step 1 earlier.

Now, to assign any random MAC address to this network card, use:

$ sudo macchanger -r <interface name>

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 <interface name>

Where XX:XX:XX:XX:XX:XX is the new MAC id that you want to change.

Finally, to revert the MAC address to its original hardware value, run the following command:

$ macchanger -p <interface name> 

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.

You can always check the man page for more details.

Changing Mac address using iproute2 [intermediate knowledge]

I would recommend using Macchanger but if you don’t want to use it, there is another way to change the MAC address in Linux.

First, turn off the network card using command:

$ sudo ip link set dev <interface name> down 

Next, set the new MAC using command:

$ sudo ip link set dev <interface name> address XX:XX:XX:XX:XX:XX 

Finally, turn the network back on with this command:

$ sudo ip link set dev <interface name> up 

Now, verify new MAC address:

$ ip link show <interface name> 

Conclusion

That is all you will ever need to change your MAC address. Just as a caution, do not misuse this freedom that Linux provides you, to create a nuisance. You can be comparatively safer by changing your MAC address, but not invincible. Even after changing the MAC address, you must keep yourself secure on public networks.

About the author

Jamie Vadym

Jamie Vadym

Founder of Error Hat. Expertise in Virtualization, Cloud Computing, Linux/UNIX systems, Programming,Storage systems,HA, Server Clustering e.t.c.

Add Comment

Click here to post a comment