I'm using CentOS 6 in a virtual environment. When cloning a virtual version of CentOS, the old eth adapters are "removed" and replaced with new ones and net MAC addresses. However, the ifcfg-ethn files still exist. I am trying to figure out how to get CentOS to automatically rescan & recreate the network adapters / eth files, just like it did on install.
Otherwise I am left with the tedious process as described here:
I know there is a quick way to do this in the GUI, but we are using a server so GUI does not apply in this case. Help?
Edit: @OldWolf suggested Kudzu, however Kudzu has been removed as of Centos 5 so I would rather avoid that. There is a procedure that Linux runs on initial install - can someone help me figure out what that is so I can manually trigger it?
8 Answers
With CentOS 6 everthing is handled by udev now. Go into /etc/udev/rules.d and delete the 70-persistent-net.rules file and reboot. If you open it berfore hand you will most likey see the original NIC MAC listed as eth0 and the new one as eth1.
Now you need to edit /etc/sysconfig/network-scripts/ifcfg-eth0 and manually update to the MAC of your new NIC card.
Deleting the file forces the detection process to run again at boot with no baggage left over from the cloning process, namely the old NIC MAC address(es).
I have to do it with my CentOS 6 clones on VMware ESXi 4.1 all the time. It's a pain kudzu would just handle it in the past with previous versions.
7Delete the persistent rules file:
rm /etc/udev/rules.d/70-persistent-net.rulesEdit ifcfg-eth0:
nano /etc/sysconfig/network-scripts/ifcfg-eth0REMOVE the HWADDR line altogether (or change it to match your new NIC's MAC address).
Reboot your system:
rebootIf you change the NIC again, just repeat step #1 and # 3.
5You can use this tool also (This is not a GUI tool, its TUI tool, Text-based User Interface)
[root@localhost ~]# system-config-network-tui
Type above command and press Enter
Then this screen will appear
Select Device configuration and press Enter
Then this screen will appear
Here eth0 refers /etc/sysconfig/network-scripts/ifcfg-eth0
Whatever the edit made in eth0 that will affect to ifcfg-eth0 file
Modifying files after clone would not work for my use-case, thus I solved the problem as follows.
You need to edit two files, removing the references to the Mac addresses in each:
/etc/sysconfig/network-scripts/ifcfg-eth0 - remove the HWADDR= line.
/etc/udev/rules.d/70-persistent-net.rules - remove from ATTR{address}== up to and including the next comma.
Now when you clone the VM and change the mac address networking will work as the Mac address never gets written to either file.
2I create and delete so many CentOS 6 virtual machines I wrote some Bashfu to fix eth0 upon cloning in VirtualBox.
[root@jp-xm-base ~]# cat fixeth0.sh
if grep -q eth1 /etc/udev/rules.d/70-persistent-net.rules; then sed -i '/eth0/d' /etc/udev/rules.d/70-persistent-net.rules; sed -i 's/eth1/eth0/g' /etc/udev/rules.d/70-persistent-net.rules; ETHERNET=`grep eth0 /etc/udev/rules.d/70-persistent-net.rules | awk -F"," '{print $4}' | awk -F"\"" '{print $2}'`; echo Ethernet Addr: $ETHERNET; sed -i 's/HWADDR=".*"/HWADDR="'$ETHERNET'"/' /etc/sysconfig/network-scripts/ifcfg-eth0; /sbin/start_udev /sbin/service network restart;
fi If your only problem is the mac address, you can run something similar to
TEST=`ifconfig | grep eth | awk '{ print $5}'`; sed "s/HWADDR.*/HWADDR\=$TEST/g" ifcfg-eth0 > TMP; mv TMP ifcfg-eth0To update the HWADDR entry.
Edit: Since it looks like the problem is an actual change in virtual hardware you can try the following. (untested and referenced from here )
edit /etc/sysconfig/hwconf and remove all reference to the previous NIC and rerun kudzu to see if it detects the new hardware. You may need to reboot.
11I'm not using Vmware but KVM with virsh - this is what I've done.
Created a 'base' image with CentOS 6.4, this is the source of all my clones. After the first boot I created a script like this
cat /etc/init.d/manglemac
#!/bin/bash
#
# manglemac This starts and stops mangle-mac
#
# chkconfig: 2345 11 88
# description: This obtains tha mac of eth0 and writes into ifcfg
mac=$(grep -H . /sys/class/net/*/address | grep eth0 | cut -d ':' -f2-10)
match_mac=$(grep $mac /etc/sysconfig/network-scripts/ifcfg-eth0)
if [ -z "$match_mac" ];
then echo HWADDR=$mac >> /etc/sysconfig/network-scripts/ifcfg-eth0
fiAdded it to init with
chkconfig --add manglemacDeleted all the references into /etc/sysconfig/network-scripts/ifcfg-eth0 of HWADDR or UUID, also deleted the udev rules from /etc/udev/rules.d/70-persistent-net.rules.
At this point I shutdown the machine and started to clone. All works fine. The script I made is very simple but works fine, however it makes a few assumptions about your setup for network (eth0 only).
I hope will help.
I was dealing with multiple NICs and nothing above worked for me (VMware Fusion 7/VMware 6 and below), so I wrote a script. The tarball is here.
Here is the README that comes with it:
- This tarball and related scripts will change the MAC address of your recently cloned VMware CentOS 6 or below machine (no systemd.)
All you need to do in the machine you will clone from one time only:
- install
lshw - replace the MAC address in all of your
/etc/sysconfig/network-scripts/ifcfg-eth?with MACDADDYnumwhere num is the number of the network interface. You need to follow the naming pattern for the file. run:
chckconfig change_mac_address onThis should add sym links at the proper run levels specified in the file
- Clone the machine as normal and fire it up. It should change the MAC address in each of your
ifcfg-eth?files,- erase the udev file,
- rescan your e1000 device,
- and restart the network
- install
Problems:
Not sure why, but sometimes you need to restart the network a second time to get all the NICs recognized:
/etc/init.d/network restartFixed this by putting a
sleep 20at the beginning of/etc/init.d/network- This has not been a problem for me, but the script relies on
lshwreturning the NICs in the same order asifcfg-eth? - This assumes your NIC device(s) are e1000. VMware standard?
- It might be worth switching the numbering of your
ifcfg-eth?files so they match whatlshwis giving back in the unlikely event that it does not work and you are doing lots of machines with lots of NICs. - With one NIC it should just work.
- After the first startup the
/usr/bin/change_mac_address.plis moved to/usr/bin/change_mac_address.pl.old - This will keep it from running on each reboot even though it will not do anything if there is no MACDADDY? in your
ifcfg-eth? You can also and probably should run
chkconfig change_mac_address off