7

Based on: http://kernelpanik.net/rename-a-linux-network-interface-without-udev/

We can easily change the name of an interface using:

ifconfig peth0 down  
ip link set peth0 name eth0  
ifconfig eth0 up

How do we do it at startup using the above way ?

In /etc/network/interfaces or any other file ?

5
  • 1
    Why are you trying to avoid udev? May 2, 2018 at 18:15
  • Presumably the operating system is Linux given what you linked, but what distribution? May 4, 2018 at 13:27
  • @John Mahowald: On ubuntu 16.04 May 9, 2018 at 9:58
  • @Michael Hampton You are right. If this is the way to go I will stick to udev May 9, 2018 at 9:58
  • I've come up with one reason to avoid udev: You're bringing up a VM by cloning, on the first boot NICS have already been detected (named), you've got a couple of NICs and they came up in the wrong order. You need to rename them. You add the udev rules, but that's just for the second boot. So you resort to the ip command. However now you have a set of udev rules that were never tested. How do you know they are right without rebooting? This is one edge-case where putting the ip commands in rc.local may be better.
    – Keeely
    Nov 23, 2018 at 9:47

3 Answers 3

9

Thanks to netplan (default in ubuntu 18.04) this is now particularly easy. You can set the interface name based on macaddress or driver:

Edit an existing .yaml configuration file in /etc/netplan/ or create a new one:

sudo nano /etc/netplan/config.yaml

Here is an example with MAC address matching. Names are set with 'set-name' and matched by the MAC address of the interface:

network:
  ethernets:
    wan:
      match:
        macaddress: 00:ab:cd:ef:12:34
      addresses: 
        - 10.5.1.2/16
      dhcp4: true
      optional: true
      set-name: wan0
    lan:
      match:
        macaddress: 00:ab:cd:ef:12:45
      addresses: 
        - 10.6.1.1/16
      optional: true
      set-name: eth0
  version: 2

Save the .yaml file and apply the configuration with:

sudo netplan apply

A reboot may be required to apply the name change.

3
  • 2
    Keep in mind that netplan is just talking to udev for you. And like many of Canonical's other really bad ideas, netplan will likely go away. I wouldn't put too much effort into learning it. Nov 26, 2018 at 14:16
  • 5
    Likely go away ? I think it is the best replacement I have ever seen for configuration. It would be a shame. Especially that the learning curve is quite flat. Dec 6, 2018 at 16:50
  • @MichaelHampton just curious - do you have a specific reason to say that Netplan is going away (like a thread in a mailing list), or just commentary on Canonical's whimsy? Mar 18, 2021 at 12:51
4

Knowing the ip command is nice, but there are persistent ways to configure using existing scripts, and yes, udev.

One thing you can dois map a NIC of a specific MAC address to a name. Append something like this to /etc/udev/rules.d/70-persistent-net.rules

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

As of 2021 there is a move away from udev rules (although they still work fine) and a move towards doing it in systemd, to keep all the network configuration in one place.

To set a persistent network device, create /etc/systemd/network/10-eth0.link with the following contents. The filename must start with a number and end with .link but the rest is not important and up to you. You need one file for each network interface you want to rename.

[Match]
# This is the MAC address of the card to change.  You can also
# match against other properties like PCI/USB slot.
MACAddress=00:11:22:33:44:55

[Link]
# This is the name to assign.  It must not conflict with any existing
# device, so be careful if you use a name like eth0 which can fail
# unexpectedly if you plug in another device that ends up on eth0 first.
Name=lan0

# You can also change the MAC address at the same time if you like.
# Make sure you follow the MAC address numbering rules or you can run
# into problems, e.g. if you assign a broadcast MAC address by mistake.
MACAddress=02:00:00:00:00:10

At the time of writing systemd will not re-apply .link files once the system is up and running (even if you restart systemd-networkd), so you'll need to reboot in order to confirm it can apply the change successfully.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.