3

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}'
Speedy059
  • 133
  • 2
  • 5
  • Actually, this appears to grab it for me. How do you execute a command in awk? `ip -br link | awk '$3 ~ /^ac:1f:6b:4c:d1:91/ {print $1 " : "$3; exit 1}'` I need to set $1 to with the ip link set – Speedy059 Sep 05 '19 at 05:26

3 Answers3

2

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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
2

You also can set arbitrary devices names by creating a file /etc/udev/rules.d/70-persistent-net.rules , which contains the mapping with MAC-ADDRESS for each interface. For example:

# eth at pci address: 00:03.0
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:ad:33:02" ATTR{type}=="1", NAME="enp0"

# eth at pci address: 00:03.1
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:ad:33:03" ATTR{type}=="1", NAME="enp1"

Once created, you need to - reboot to apply changes. - change the networking configuration (e.g. /etc/sysconfig/network-scripts/ifcfg and route files, or using NetworkManager if active).

tonioc
  • 1,047
  • 8
  • 11
1

I would do this with a small script that first got the name of the interface into a variable and then used that to set the new name

#!/bin/bash

oldinterfacename=$(ip -br link | awk '$3 ~ /90:10:00:9f:46:c3/ {print $1}')
if [ -z "$oldinterfacename" ]
then
  echo "Did not find interface to rename"
  exit 1
fi
ip link set "$oldinterfacename" name newName  || { echo "Error: Failed to set newName"  ; exit 1; }
user9517
  • 115,471
  • 20
  • 215
  • 297