CentOS 7 how to get interface name by macaddress?

Speedy059 asked:

I’m trying to figure out how I can get the network interface name and then change the name. Right now it’s easy to change the name with this example:

ip link set eth1 name <newName>;

However I want to have a script or command where I have it auto set the link name based on the mac address.

Example, I’m trying to do this:

1) Get interface name with mac address “68:05:ca:0e:74:c4”.

2) Change the interface name once I find it by locating it by mac address. By issuing ‘ip link set name ;’

Is it possible to do this in a bash script/command to automatically locate the interface name and rename it? I’ve been trying to do it by “ip link show” and using awk, but i’ve had no luck.

When I try using awk, i run into issues since the interface name and mac address are on separate lines.

ip link | awk '{print $2}'

My answer:


You can have consistent network device naming create interface names based on the MAC address. For instance, with this method, an interface with the hardware address 68:05:ca:0e:74:c4 would be named enx6805ca0e74c4.

On RHEL/CentOS 7, you need to change the udev naming rules just a bit. You can do that with these directions:

Copy the default file /usr/lib/udev/rules.d/80-net-name-slot.rules to the directory /etc/udev/rules.d, then edit the copy.

In the copied file, find these three lines:

NAME=="", ENV{ID_NET_NAME_ONBOARD}!="", NAME="$env{ID_NET_NAME_ONBOARD}"
NAME=="", ENV{ID_NET_NAME_SLOT}!="", NAME="$env{ID_NET_NAME_SLOT}"
NAME=="", ENV{ID_NET_NAME_PATH}!="", NAME="$env{ID_NET_NAME_PATH}"

Replace these lines with:

NAME=="", ENV{ID_NET_NAME_MAC}!="", NAME="$env{ID_NET_NAME_MAC}"

Then reboot.

Before the change, the interface looks like:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:21:d1:84 brd ff:ff:ff:ff:ff:ff
    inet 172.20.203.174/24 brd 172.20.203.255 scope global noprefixroute dynamic enx52540021d184
       valid_lft 3588sec preferred_lft 3588sec
    inet6 2001:db8:b0b9:0:3962::a1be/128 scope global noprefixroute dynamic 
       valid_lft 3591sec preferred_lft 3591sec
    inet6 fe80::5054:ff:fe21:d184/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

After the change, it is now:

2: enx52540021d184: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:21:d1:84 brd ff:ff:ff:ff:ff:ff
    inet 172.20.203.174/24 brd 172.20.203.255 scope global noprefixroute dynamic enx52540021d184
       valid_lft 3588sec preferred_lft 3588sec
    inet6 2001:db8:b0b9:0:3962::a1be/128 scope global noprefixroute dynamic 
       valid_lft 3591sec preferred_lft 3591sec
    inet6 fe80::5054:ff:fe21:d184/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Remember that with this naming scheme, manual intervention is required to reconfigure networking if you have to replace a NIC.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.