Change MAC Address in Linux using Python
MAC (Media Access Control) is a 48-bit address embedded into every NIC (Network Interface Card) and uniquely identifies each device on a network. MAC address is used within the network to identify devices and transfer data between devices.
Characteristics of MAC Address
- Permanent
- Physical
- Unique
- Assigned by Manufacturer
Why there is a need to change MAC address?
- Increase anonymity
- Impersonate other devices (sometimes there is filtering that particular mac address can only connect to the network)
- Bypass filters
Today, we will learn about changing MAC address in Linux!
You can directly check the python file here
How to find MAC address in Linux
We can find the details of IP address and MAC address using the command ‘ifconfig’.
The above is the output of the ‘ifconfig’ command.
Here ‘eth0’ (ethernet) and ‘lo’ (localhost) are the Linux OS interfaces. Under ‘eth0’ , ‘inet’ signifies IPv4 address, ‘inet6’ signified IPv6 address, and most importantly ‘ether’ represents MAC address (00:0c:29:24:29:0d)
Change MAC address manually
ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up
We can directly write these command in the terminal, and that will successfully change the MAC address to 00:11:22:33:44:55
Get Pulkit Khandelwal’s stories in your inbox
Join Medium for free to get updates from this writer.
But to automate this whole process, we can use python and create a script that changes the MAC address
Python Script to Change MAC Address
This is the python script to change MAC address and is using the function-oriented approach
Understanding Libraries
Subprocess library helps run system commands,
Optparse library helps to deal with command-line options,
Re is the regex library that allows coping with Regular Expressions.
Understanding Each Function
- change_mac()
This function helps to change the MAC address by taking arguments, i.e., Interface name and the new MAC address, and then passing these values in ‘subprocess.call’, which lets you run system commands and thus changing the MAC address
2. get_arguements()
This function gets the command-line options using Optparse, for example -i or — — interface, -m or — — mac. Optparse Library helps to retrieve the arguments and their respective values.
Using the ‘add_option’ method, we can add arguments that we want as a command-line option. Later ‘parse_args’ method parses the arguments defined and returns two values, i.e., options and arguments. We can access the value using the ‘options.[variable name]’ . eg:- ‘options.interface’ helps to retrieve the interface name provided as an argument.
3. get_current_mac()
This function returns the current MAC address using Regex. ‘subprocess.check_output’ method returns the command output like here it is ‘interface [interface_name]’ and the output is stored in a variable named ‘ifconfig_result’. Then using ‘re.search’ method, the regular expression of MAC address is passed, and if there exists a value,MAC Address will be returned but if not, error will be displayed saying ‘Could not read MAC address.’
In the end, firstly, get_arguements() function is called and will take all the command-line option values and the returned value is stored in ‘options’ variable. Next, current MAC address is retrieved using ‘get_current_mac()’ and MAC address is displayed as output. Then using ‘change_mac()’ function, MAC address is changed. The current MAC address is retrieved again, but this time it got changed to the new MAC address. Lastly for confirmation, we check that if the current mac address (changed one) is equal to the user-supplied MAC address, then the MAC address is successfully changed and result is printed accordingly.
Hurray, MAC address was successfully changed to new MAC address :)
Happy Learning !








