Proper way for changing MAC address in a linux VM?

You are very likely using the "correct" method to change the MAC address, if you are doing this in the advanced network settings in VirtualBox. However, a temporary change as in the other answer by Zuul may be best for your purposes, as it will change the MAC without causing too many additional problems - however it will not survive a reboot.

Changing it in VirtualBox will of course survive a reboot, but the issue you are facing is with udev. This is the process that looks at hardware and sets that hardware up in /dev (or whatever else might need to be done). One of the things it does by default is to try to maintain persistent names for network interface cards [nics]. This is because when you have multiple nics, you want them to always have the same ethX name, so that your /etc/network/interfaces configuration targets the right nic each boot. The OS cannot rely on them just being physically in the same spot each time - you may move them around for example, or some systems do not report their physical connections in the same sequence each boot.

So udev uses persistence rules based on the MAC address of the network card. If it has an entry for eth1 with a known MAC address and a card appears with a different MAC address it will allocate a different ethX device name to the interface.

To counter this, you just need to update the udev rules. Edit:

vi /etc/udev/rules.d/70-persistent-net.rules

and look for the line with your current MAC:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="11:22:33:44:55:66", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

If you change the MAC address here to the new one you chose in VirtualBox settings, then it will retain the same ethX dev number next boot.

The best sequence would probably be to edit it here first, then shutdown the VM, then edit the VB MAC setting, then boot up.


You can change the MAC address with 4 simple steps using ifconfig:

Open a terminal (keyboard shortcut: ctrl + alt + t)

  1. Find the current mac address:

    ifconfig | grep HWaddr
    
  2. Shut down the interface (assuming it to be eth0 for the next steps):

    ifconfig eth0 down
    
  3. Set the new mac address:

    ifconfig eth0 hw ether 00:1E:68:35:FF:91
    
  4. Flag causes the interface to be activated:

    ifconfig eth0 up
    

Now you can recheck your interface mac address:

ifconfig eth0 |grep HWaddr

Note: You can choose any 48 bits hexadecimal address as your MAC address.


For the purpose of completeness, you can combine steps 2 and 3 into one line:

ifconfig eth0 down hw ether 00:00:00:00:00:01