How to rename NIC?

After installing Mellanox OFED driver, my network interfaces were renamed, e.g. enp197s0f0 is now enp197s0f0np0. nmcli con shows the old names but nmcli dev shows the new names. I’d like to rename these to something easier to remember like eth1. I tried doing this with the following udev rule but it has no effect. Is there a log file somewhere that would show why this isn’t working? I didn’t find anything about ‘eth2’ in dmesg or journalctl.

# cat /etc/udev/rules.d/85-net-persistent-names.rules 
#PCI device 15b3:1019 (mlx5_core)
#NAME:="some name" , := is used to make sure that device name will be persistent.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="B8:CE:F6:82:D1:64", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME:="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="B8:CE:F6:82:D1:65", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME:="eth2"

There’s a few ways to do it without udev, depending on if you’re on Rocky 8 or 9.

# Identify the mac address of the interface
[root@cm01 ~]# ip a s enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:85:14:ac brd ff:ff:ff:ff:ff:ff
    inet 10.100.0.12/24 brd 10.100.0.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe85:14ac/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

# Rocky Linux 8 only as it still uses /etc/sysconfig/network-scripts/ifcfg-* files
[root@cm01 ~]# echo 'HWADDR="52:54:00:85:14:ac"' >> /etc/sysconfig/network-scripts/ifcfg-enp1s0
[root@cm01 ~]# mv /etc/sysconfig/network-scripts/ifcfg-enp1s0 /etc/sysconfig/network-scripts/ifcfg-cm0
[root@cm01 ~]# sed -i 's/enp1s0/cm0/g' /etc/sysconfig/network-scripts/ifcfg-cm0
[root@cm01 ~]# init 6

[label@router ~]$ ssh cm01
[label@cm01 ~]$ ip a s cm0
2: cm0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:85:14:ac brd ff:ff:ff:ff:ff:ff
    inet 10.100.0.12/24 brd 10.100.0.255 scope global noprefixroute cm0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe85:14ac/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Using systemd links:

# Rocky Linux 8 or 9 can use this
[root@idp ~]# mkdir /etc/systemd/network
[root@idp ~]# vi /etc/systemd/network/70-custom.link
[Match]
MACAddress=52:54:00:ea:a2:6b

[Link]
Name=idp0

# Rocky Linux 9 only operations
[root@idp ~]# mv /etc/NetworkManager/system-connections/enp1s0.nmconnection mv /etc/NetworkManager/system-connections/idp0.nmconnection
[root@idp ~]# sed -i 's/enp1s0/idp0/g' /etc/NetworkManager/system-connections/idp0.nmconnection
[root@idp ~]# init 6

. . .

[root@idp ~]# ip a s idp0
2: idp0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:ea:a2:6b brd ff:ff:ff:ff:ff:ff
    inet 10.100.0.13/24 brd 10.100.0.255 scope global noprefixroute idp0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:feea:a26b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Note that with Rocky Linux 9, using systemd links is the most effective way to change an interface name without using udev.

Thanks! I’ll give this a shot. I’m on Rocky 8.6. So is udev deprecated now?

Awesome, one of those methods should work for you then.

It’s not deprecated. udev is a linux kernel device manager and is a fundamental part of majority of distributions, especially Enterprise Linux and Fedora. Unfortunately in my experience, it has been inconsistent and difficult to maintain/manage since RHEL 8’s release in changing device names.

I have found that it’s just simpler to use systemd network or ifcfg-* directives to get around udev not operating like it should for network device names.