I am writing a script that will change my ethernet MAC address on a local network. It will work as follows:

  1. Initially eth0 will be brought down
  2. MAC will be changed using the ifconfig utility
  3. eth0 will be brought up

sleep 5
ifconfig eth0 down
sleep 10
ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX
sleep 1
ifconfig eth0 up

If I execute the above script, the MAC changes successfully, also the network icon in the panel shows 'Connecting' status, and after few seconds the eth0 gets connected to the network again. Then I added few lines to increase the functionality of my code-

/sbin/ifconfig eth0| grep 'inet addr:'
ifconfig eth0 | awk '/HWaddr/ {print $5}'
ping -c 2

The above lines displays the new IP address, MAC address and also sends a ping request to confirm that network is working as usual. But the problem I'm facing is that after adding the above lines, I get a ping failure, and also the network icon in the panel shows 'Connecting' status. After the script is completely executed, after few seconds, the eth0 is connected completely. (ping can be done) So I tried adding sleep 30 after the following line

ifconfig eth0 up

assuming it would fix the ping issue. But still the ping(mentioned in the script) fails and the network takes some time after the execution of the script to establish a complete connection.
How should I fix this issue?


Which distribution are you using? Basically, MAC address change should get done before starting NetworkManager. Possible options are:

  • Install a custom udev rule for eth0.
  • Write a script working with your init system (sysvinit, upstart, systemd, etc).
  • Utilize systemd.link feature of systemd.

I found useful info at archlinux wiki. You might also be interested in macchanger, which is commonly available in various distributions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.