Celeb Glow
general | March 14, 2026

Compare MAC address of live network interface using grep to existing configuration files

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

2 Answers

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy