Udev: resolve a changed MAC address
Symptoms
After changing the MAC address (Ethernet address) of your network adapter, it does not get an IP address. This can happen within a virtual machine or when replacing the network card in a physical machine. If you try an ifconfig -a or an ip addr list, you see that the name of the network card has changed (for example, from eth0 to eth1).
Solution
Convince udev to assign the correct name to the network card; maybe you have to change the MAC address in your network scripts too. Activate the change by rebooting, reinserting the kernel module or triggering udev.
Assumption
- You are running a Linux distribution which uses udev;
- Your distribution uses udev-rules for persistent network names.
Procedure
First change the persistent network name udev has assigned; next, change any other places the MAC address has been hardcoded.
You will need to know the new MAC address, as well as the original MAC address1.
Changing the udev persistent network name
Usually, your distribution will have a file /etc/udev/rules.d/70-persistent-net.rules,2, but it may have a different name. If you examine this file, you will see lines containing MAC addresses (after ATTR{address}==) and network adapter names (after NAME=).
Remove (or comment out by placing a hash at the start of the line) the line with the old MAC address; modify the line with the new MAC address and the wrong adapter name to assign the correct name.
An example would be:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:54:4a:a2", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:54:7c:a3", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
In this example, you would remove the first line and change eth1 to eth0 in the second line.
Changes in the networking scripts
On Debian-like distributions, check /etc/network/interfaces for the old MAC address and replace it with the new address.
On Red Hat-like distributions, check /etc/sysconfig/network-scripts/ifcfg-* for the old MAC address and replace it with the new address.
Activating the changes
There are three options to make the new device name active. The easiest way is to simply reboot. Alternatively, you can reinsert the kernel module that governs the network card or trigger udev. This last option is the cleanest solution as well as the least risky.
Option 2: Reinsert the kernel module
First you have to find out which kernel module is used for the network card. You can find this, for example, by examining the /sys file system:
# Set DEVICE to something like eth1
ls -al /sys/class/net/$DEVICE/device/driver
This will return a symbolic link; the last path component of the link should be the name of the kernel module.
Now remove and reinsert the kernel module. Note that all other network cards that are provided by this module will go down too.
# Set MODULE to something like virtio-netmodprobe -r $MODULE && modprobe $MODULE
If all went well you should see the new card name when you issue an ifconfig -a. To assign the proper IP address, try:
# Set NEW_DEVICE to something like eth0
ifup $NEW_DEVICE
Option 3: trigger udev
It is possible to tell udev to redo the registration of the device name for only this device:
# Set MACADDR to the MAC address of the device, for example 52:54:00:01:02:03
udevadm trigger --subsystem-match=net --attr-match=address=$MACADDR
If all went well you should see the new card name when you issue an ifconfig -a. To assign the proper IP address, try:
# Set NEW_DEVICE to something like eth0
ifup $NEW_DEVICE