16

My main ethernet interface is called enp2s0, but some software requires me to name it to eth0.

How can I rename it?

I tried creating /etc/udev/rules.d/10-rename-network.rules (as per this post), creating /etc/udev/rules.d/70-persistent-net.rule and editing /etc/network/interfaces.

The contents of /etc/udev/rules.d/10-rename-network.rules were:

SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="ff:ff:ff:ff:ff:ff", NAME="eth0"

with ff:ff...:ff replaced with the mac address of enp2s0.

3
  • In your research, did you look at this resource
    – graham
    Commented Feb 17, 2021 at 8:24
  • I just tried this, with no success - the network went down when trying sudo netplan try
    – merc1er
    Commented Feb 17, 2021 at 8:46
  • No software / applications should need to know the network interface name ... what sw are you using ?
    – Soren A
    Commented Feb 17, 2021 at 9:03

5 Answers 5

30

Using netplan which is the default these days. File /etc/netplan/50-cloud-init.yaml file.

Find the target devices mac/hw address using the lshw command:

lshw -C network

You'll see some output which looks like:

root@ys:/etc# lshw -C network
  *-network
       description: Ethernet interface
       physical id: 2
       logical name: eth0
       serial: dc:a6:32:e8:23:19
       size: 1Gbit/s
       capacity: 1Gbit/s
       capabilities: ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt 1000bt-fd autonegotiation
       configuration: autonegotiation=on broadcast=yes driver=bcmgenet driverversion=5.8.0-1015-raspi duplex=full ip=192.168.0.112 link=yes multicast=yes port=MII speed=1Gbit/s

So then you take the serial

dc:a6:32:e8:23:19

Note the set-name option.

This works for the wifi section as well.

network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: <YOUR MAC ID HERE>
            set-name: eth0

Then then to test this config run.

sudo netplan try

When your happy with it

sudo netplan apply
10
  • Network interface went down after creating this file with this content.
    – merc1er
    Commented Feb 17, 2021 at 9:08
  • Did you replace <MAC 'eth0' [ID1]> with your devices hardware address? Commented Feb 17, 2021 at 9:14
  • 1
    If you do ip addr it will list all the devices and part of that output will be your mac id.. e.g. link/ether dc:a6:32:e8:23:19 Commented Feb 17, 2021 at 9:16
  • That's how netplan identifies the device to rename.. Then run.. sudo netplan apply Commented Feb 17, 2021 at 9:17
  • I've added more details to the answer to help you.. Commented Feb 17, 2021 at 9:22
6

Open "etc/netplan/01-network-manager-all.yaml" with any text editor and copy paste text from down bolow and don't forget to paste your own "mac addresses"

# Let NetworkManager manage all devices on this system
  network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: fc:15:b4:bn:34:g5
            set-name: eth0
        wlan0:
            dhcp4: true
            match:
                macaddress: 00:22:34:f4:d3:45
            set-name: wlan0

and save the file after that use

netplan try

netplan apply
1
  • this just adds a global NetworkManager connection with the set-name value, does not change the product name of the interface
    – allanlaal
    Commented Nov 6, 2022 at 0:38
1

The arm64 version of Focal Fossa I have does not have netplan, it uses nmcli, and changing the kernel command line is difficult. I was able to create /etc/udev/rules.d/73-special-net-names.rules (which on this install overrides /usr/lib/udev/rules.d/73-special-net-names.rules) to change the name of a usb attached ethernet port (after using lsusb to find the id 413c:a102) by adding:

ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb", ATTRS{idVendor}=="413c", ATTRS{idProduct}=="a102", NAME="eth0"

and then rebooting.

1
  • Alternately you could just install the netplan package netplan.io its a lot more friendly then Netplan will do all the hard work for you. Commented Mar 22, 2023 at 17:09
1

There is a simple way to do this that uses udev indirectly. Add a file per interface, with the format PRIORITY-DESCRIPTIVE_NAME.link, in /etc/systemd/network/. E.g. 10-eth-internal.link:

[Match]
PermanentMACAddress=11:11:11:11:11:11

[Link]
Name=eth0

and 10-eth-dock.link:

[Match]
PermanentMACAddress=22:22:22:22:22:22

[Link]
Name=eth1

If the devices aren't hot-plugged or the system is otherwise not showing the new names, reboot for them to take effect. You should be able to check your changes with ip link.

Explanation

From the statement on why we now have these ugly but consistent names, the second option for overriding those names is very straightforward:

You create your own manual naming scheme, for example by naming your interfaces "internet0", "dmz0" or "lan0". For that create your own .link files in /etc/systemd/network/, that choose an explicit name or a better naming scheme for one, some, or all of your interfaces. See systemd.link(5) for more information.

It has two main benefits: it's independent of network connection config, because you shouldn't need to set dhcp4, nor double-specify the name; and it falls back to the predictable naming scheme for new or temporary devices added to the system. You retain consistency but only have to set names for the devices you want.

If you don't want to use MAC addresses, e.g. when deploying on system images, you'd instead match by Path= or some other consistent identifier(s) (see the docs in the quote). You can get these IDs from the output of udevadm info /sys/class/net/*.

0

Some might like this method better than the others mentioned here. It's mac address independent and simple and low level enough that it seems to play well with network reconfiguration tools. (I did this on 22.04 but I suspect 20.04 and 24.04 will work the same)

Put this into /etc/systemd/network/01-eth0.link

[Match] 
OriginalName=enp2s0

[Link] 
Name=eth0

Then either reboot or run

udevadm trigger --action=add /sys/class/net/enp2s0

Note: the trigger will work only once, because after that the name will be changed to eth0 as far as the trigger and the .link files are concerned. If you want to rename a interface that has already been renamed, you can create a temporary link file and trigger it, but that path is perilous.

You can add other .link files for all your other interfaces to rename them to the old friendly names.

The obvious alternative is to use Walf's suggestion of matching by MAC address or pci path, which has the advantage of being re-applyable. I'll also repeat his suggestion of using udevadm info /sys/class/net/[interface] to check out the current state of udev's management of an interface

Another alternative way to do this is to change

GRUB_CMDLINE_LINUX=""

to

GRUB_CMDLINE_LINUX="net.ifnames=0"

in /etc/default/grub then run update-grub and reboot. This disables the new "predictable naming" scheme entirely. I think I like the .link method better because you can mess with renaming things without rebooting and/or only change names you care about.

You must log in to answer this question.

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