How do you automatically detect a new network card in CentOS 6 / RedHat?

46

25

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: http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-s390info-addnetdevice.html

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?

Joshua

Posted 2011-09-06T18:56:31.627

Reputation: 4 290

Answers

52

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.

Tracy Carlton

Posted 2011-09-06T18:56:31.627

Reputation: 536

There is no ifcfg-eth0 on my system (CentOS 6.6). :-( – david.perez – 2015-04-27T10:06:08.033

2Sounded wonderful tracy, however it did not work as you described. The old ifcfg-eth0 file was still there, with the original MAC address etc instead of the correct new one. – Joshua – 2011-09-08T20:58:15.713

2Sorry yes I had forgetten. You must also MANUALLY update the mac address in ifcfg-eth0 as well sorry. That will make it work. There might be an easier way but I have yet to find it... – Tracy Carlton – 2011-09-08T23:27:02.847

This was a good start. It seems like it could be possible to write a script that will automate the process - even the manual bits described - on bootup. The problem is activating the script only after the machine has been cloned / moved. Still no complete answer to this... – Joshua – 2012-02-20T21:32:40.443

FYI I marked this answer as the correct one, as it helps get it going. Admins still need to write a custom / complicated script for cloning automatically, or just manually do it each time. Bother. – Joshua – 2012-06-21T15:52:32.843

5Go into /etc/udev/rules.d and delete the 70-persistent-net.rules file and DO NOT reboot. Go into /etc/sysconfig/network-scripts/ifcfg-eth0 and manually update to the MAC of your new NIC card. Reboot and the network should come alive. – None – 2012-08-08T14:44:30.577

2@user151091 Instead of rebooting, start_udev then service network restart worked for me. – doshea – 2013-11-27T03:35:02.247

5

You 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

enter image description here

Select Device configuration and press Enter

Then this screen will appear

enter image description here

Here eth0 refers /etc/sysconfig/network-scripts/ifcfg-eth0

Whatever the edit made in eth0 that will affect to ifcfg-eth0 file

max

Posted 2011-09-06T18:56:31.627

Reputation: 3 329

While this might be a useful answer to some people / cases, the goal of this was to script or automate the configuration of network adapters, which cannot be done through a TUI/GUI. Read the original post: "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." – Joshua – 2012-11-19T16:32:24.180

@Joshua sorry got it...+1 – max – 2012-11-20T13:05:41.090

@max, just system-config-network also seems to open this interface. – Acumenus – 2013-12-17T19:25:35.513

>system-config-network-tui

-bash: system-config-network-tui: command not found. Which package is it in??? – Qi Fan – 2014-01-14T01:25:41.920

2@QiFan Try this command yum install system-config-network-tui – max – 2014-01-14T05:51:00.713

1@QiFan To find out which package contains that command, try this yum whatprovides */system-config-network-tui – max – 2014-01-14T05:53:37.213

4

Delete the persistent rules file:

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

Edit ifcfg-eth0:

nano /etc/sysconfig/network-scripts/ifcfg-eth0

REMOVE the HWADDR line altogether (or change it to match your new NIC's MAC address).

Reboot your system:

reboot

If you change the NIC again, just repeat step #1 and # 3.

Martin Anderson

Posted 2011-09-06T18:56:31.627

Reputation: 41

Do you really need to reboot? Why not just restart the network service? – vgoff – 2012-11-19T08:47:21.367

While this might be a useful answer to some people / cases, the goal of this was to script or automate the configuration of network adapters. From the original post: "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." – Joshua – 2012-11-19T16:33:22.430

@vgoff yes, restarting the network service obviously works fine :) Perhaps he's referring to rebooting the network device itself, e.g. "ifup eth0" etc. – Joshua – 2012-11-19T16:34:12.680

That may be, but he says 'system' not 'device'. – vgoff – 2012-11-19T19:03:59.643

I may be wrong, but I think that 'service network restart' won't cause your adapters to be renamed after you removed the persistent rules file, and that a reboot or 'start_udev' is required to cause them to be renamed. – doshea – 2013-11-27T03:23:14.343

4

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.

Scot Inscore

Posted 2011-09-06T18:56:31.627

Reputation: 41

Workable solution for 1 adapter. Also do not forget to make connection type as Bridge in the net settings. – Cherry – 2014-12-04T07:01:30.893

This worked for me - as noted, the MAC address never gets written back to either file, so you can keep changing it and your interface will remain "eth0" with the same config. This seems to only work if you have one network adapter, though, otherwise I got a long boot delay at "Starting udev:" and my second device was called "rename3", presumably because the rules were suggesting both devices should be called "eth0". – doshea – 2013-11-27T03:20:34.313

3

I 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

Electrawn

Posted 2011-09-06T18:56:31.627

Reputation: 226

1

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-eth0

To 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.

OldWolf

Posted 2011-09-06T18:56:31.627

Reputation: 2 293

I tried manually changing the MAC address on eth0 for example, but it still did not work. Subsequent attempts at "service network force-reload", "ifup eth0" etc. would always end in "Device eth0 does not seem to be present..." Help? – Joshua – 2011-09-06T21:07:53.567

What type of virtualization are you using and how did you clone the system? – OldWolf – 2011-09-06T21:20:40.047

VirtualBox, but I don't think the virtualization matters. This should be like shutting a physical box down, removing the existing network adapters, and adding different ones. – Joshua – 2011-09-06T21:24:20.810

@Josh Where are the ifcfg-* files you are editing as there are possibly three sets - check out /etc/sysconfig/network-scripts and /etc/sysconfig/networking/devices and /etc/sysconfig/networking/profiles/default (yeah, I know - it seems to be something to do with which tool/s you use to setup the adaptors). The first should be the ones that make the difference, but you may need to deal with the others too. – Linker3000 – 2011-09-06T21:26:37.613

1The reason I asked and that the virtualization type matters is that it determines what the cloning process looks like. A VMware clone on the same host would not change the virtual network hardware unless you told it to. In your case, the problem is not the mac address nor the cloning process, it's that you are changing the virtual hardware. – OldWolf – 2011-09-06T21:41:41.090

See my edit above. – OldWolf – 2011-09-06T22:04:02.850

@OldWolf, Yep!! – Joshua – 2011-09-07T14:26:21.643

@OldWolf - Kudzu - had not heard of Kudzu. Seems like the right track, however according to different sources kudzu was removed as of CentOS 5 and this procedure is now handled by "the kernel and udev". I need to look that direction instead... how? – Joshua – 2011-09-07T14:36:11.987

@OldWolf - also ifconfig is being replaced with ip, according to the CentOS 6 man page – Joshua – 2011-09-07T16:40:22.707

Do you know what kind of virtual nic you are changing from and to? – OldWolf – 2011-09-07T18:01:53.117

why use "grep" when awk can do that for you: ifconfig | awk '/eth/ { print $5}' – Ed Randall – 2014-01-23T22:43:53.277

1

I'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
fi

Added it to init with

chkconfig --add manglemac

Deleted 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.

silviud

Posted 2011-09-06T18:56:31.627

Reputation: 541

0

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:

    1. install lshw
    2. replace the MAC address in all of your /etc/sysconfig/network-scripts/ifcfg-eth? with MACDADDYnum where num is the number of the network interface. You need to follow the naming pattern for the file.
    3. run:

      chckconfig change_mac_address on
      

      This should add sym links at the proper run levels specified in the file

    4. 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

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 restart
    

    Fixed this by putting a sleep 20 at the beginning of /etc/init.d/network

  • This has not been a problem for me, but the script relies on lshw returning the NICs in the same order as ifcfg-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 what lshw is 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.pl is 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
    

FFQ

Posted 2011-09-06T18:56:31.627

Reputation: 1