1

Cloning VMs creates new network interface everytime on CentOS/RedHat VMs. I need to rename the ifcfg-eth* to the same name as the live interface on the VM.

What I would like to do is to use a script to compare MAC address of live NIC (take it from 'ifconfig' result) tp MAC addresses in all existing ifcfg-* files and rename the corresponding one to ifcfg-liveNICname.

Example:

# ifconfig
eth2      Link encap:Ethernet  HWaddr 00:50:56:8F:5B:D5

there are 3 configuration files in /etc/sysconfig/network-scripts/:

ifcfg-eth0
ifcfg-Auto_eth0
ifcfg-eth1

script compares the MAC address 00:50:56:8F:5B:D5 to MAC addresses in all 3 files and renames the correct one (with the MAC address in question) to:

ifcfg-eth2

Preferably it could also change line NAME in the ifcfg file:

NAME=eth2

but not necessary.

I need that 'fix' so I could run a script on bunch of VMs to change DNS entries in one go. I have script for that but when ran on a test VM it creates a new ifcfg-eth2 file with just DNS entries in it while an active interface has a different name in the file e.g. ifcfg-eth0

As I'm no good in scripting I started with that:

nic=$(/sbin/route -n | grep "^0.0.0.0" | rev | cut -d' ' -f1 | rev)
ifconfig -a | grep $nic`

and that's all I got :(

Can anyone help please?

2
  • Which hypervisor are you using? VirtualBox, Xen, KVM,...? May 20, 2014 at 14:36
  • All VMs are in VMware.
    – Mark
    May 20, 2014 at 14:56

2 Answers 2

0

If I understand you correctly, you want to get the MAC address of the NIC currently being used and then find the file in /etc/sysconfig/network-scripts/ that mentions this MAC address and rename that file to ifcfg-NIC where NIC is the network device found earlier. If so, this should work:

#!/usr/bin/env bash

## Get the NIC's name
# nic=$(/sbin/route -n | awk '/^0\.0\.0\.0/{print $NF}')
nic=$(/sbin/ip route show | awk '/default/{print $5}')


## Get the NIC's MAC address
#mac=$(/sbin/ifconfig "$nic" | grep -oP 'HWaddr\s*\K[^\s]*')
mac=$(/sbin/ip link show "$nic" | grep -oP 'ether\s*\K[^\s]*')

## Get the file name. This assumes that there will only
## be one matching file. 
file=$(grep -lm 1 "$mac" /etc/sysconfig/network-scripts/*)

## Change the NAME line to the new NIC and make
## a backup copy of the file called "$file.bak".
sed -i.bak -r "s/NAME=(.*)/NAME=$nic/" "$file"

## Rename the file
mv "$file" "$(dirname "$file")"/ifcfg-"$nic"
6
  • route and ifconfig are obsolete, have been replaced by the iproute2 suite of commands. May 21, 2014 at 5:27
  • Hi Marius. That script is great but I had to change wlan0 to $nic to get MAC address of live NIC, as I was getting an error that wlan0 doesn't exist. These lines: sed -i.bak -r "s/NAME=(.*)/NAME=$nic/" "$file" mv "$file" "$(dirname "$file")"/ifcfg-"$nic" are giving me results: sed: can't read : No such file or directory mv: cannot stat `': No such file or directory I have to say I would never come up with a script like that. That's great help. Thank You very much.
    – Mark
    May 21, 2014 at 10:45
  • Marius, you're the best. The script has worked just fine now. Not sure what was the problem but i had to change the script just slightly. Biggest thanks to You.
    – Mark
    May 21, 2014 at 11:23
  • @Mark you're welcome. Sorry about the wlan0, that was left over from my testing but it should work now. If this answer solves your issue please take a minute and check the check mark under the vote count to the left, this will signify to everyone that your issue's been resolved.
    – terdon
    May 21, 2014 at 12:12
  • @terdon Thank so much. Sorry I have thanked the wrong name there in previous answers.
    – Mark
    May 21, 2014 at 12:47
0

I am not sure I understand what you wish to do.

Do you wish to change the DNS servers? They are located in the file /etc/resolv.conf, irrespective of NIC name.

Do you wish to have all clones with the same MAC addresses? Different MAC addresses? manually chosen MAC addresses? Read on.

The easiest thing is to assign manually the MAC addresses of your NICs for every (cloned or not) VMs.

The instructions to do so are online, see for instance here. Edit the VM's .vmx file, erase the lines beginning with

  ethernetN.generatedAddress
  ethernetN.addressType
  ethernetN.generatedAddressOffset 

and put in their place the twolines

  ethernetN.address = "00:50:56:XX:YY:ZZ"
  ethernetN.addressType = “static”

where 00:50:56:XX:YY:ZZ is the MAC address you wish to impose. Now save the file and you are done.

4
  • All I'm trying to achieve is to rename the ifcfg-* file for the active interface e.g. eth2 to a proper name which for eth2 would be ifcfg-eth2 I'm trying to do that by running a script through HPSA on a bunch of VMs. As for the DNS change, it's necessary to change DNS entries in ifcfg-* file in RHEL6, as resolv.conf will be overwritten after reboot. That's not an issue in RHEL5
    – Mark
    May 20, 2014 at 15:21
  • @Mark But why? The DNS entry in ifconfig-ethN are, in any case, copied to /etc/resolv.conf, see centos.org/docs/5/html/Deployment_Guide-en-US/… You might as well work on /etc/resolv.conf directly, less of an hassle May 20, 2014 at 15:31
  • Ok. Let's step back. Rhel5 doesn't require DNS entries in ifcfg-* file, as you said, it's in resolv.conf. In Rhel6 the DNS entries are being kept in ifcfg-* file and if I'll change resolv.conf, it'll get overwritten on reboot. I have a working script for that. My issue is that on cloned VMs ifcfg-* files have different names from active NICs. This line: nic=$(/sbin/route -n | grep "^0.0.0.0" | rev | cut -d' ' -f1 | rev) is part of my DNS change script, so DNS is being changed on a filename based on live NIC name. Because files have different names, script creates new file (that's bad)
    – Mark
    May 20, 2014 at 15:36
  • Like I said, I'm no good in scripting. I just learned how to "crawl". I'm sure it's possible to get a script that's able to change the name of the correct ifcfg-* file (for the live NIC)
    – Mark
    May 20, 2014 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .