5

I know that a process generates persistent network card names based on rules found in /lib/udev/rules.d/75-persistent-net-generator.rules. I also know how to completely disable this process with a simple

echo '#' > /etc/udev/rules.d/75-persistent-net-generator.rules

but I've read that I "could also write my own rules file to give the interface a name — the persistent rules generator ignores the interface if a name has already been set" (/etc/udev/rules.d/README confirms that this is possible).

Can you provide instructions and/or examples on how to write such rules? (I mostly care about Debian/Ubuntu and a bit less for CentOS). In my case I would like to add rules like the following:

  • cards with MAC A or B should be named eth0
  • cards with MAC C or D should be named eth1
  • follow default naming scheme for anything else

As a specific example of why I want to write custom rules: I have two identical servers with one onboard LAN and one PCI LAN. In case of HW failure I want to be able to move disks from HW#1 to HW#2 and it's important for eth0 to continue pointing to the onboard card and eth1 to the PCI card (no one wants to mess with cabling in the middle of a HW failure panic). My current workaround works but is a lot of work[1] so I wonder if writing custom rules would allow me to express the above rules


[1] install the OS in HW#1 and keep a copy of /etc/udev/rules.d/70-persistent-net.rules. Move the disks to HW#2 and keep a second copy of the same file. Concatenate the two copies and manually edit the NAME="ethX" part. Replace /etc/udev/rules.d/70-persistent-net.rules with my version. Finally disable auto-creation of a new 70-persistent-net.rules using

echo '#' > /etc/udev/rules.d/75-persistent-net-generator.rules
ndemou
  • 1,215
  • 2
  • 16
  • 27
  • You should try rephrasing your question, so that it's more specific. Say what you want to do (not what you want to learn about). – Cristian Ciupitu Jun 04 '14 at 21:43
  • The truth is that to learn is what I want :). I've already found a workaround for my needs by disabling the persistent net name generator but I wonder if writing rules would be a better solution. However I can find no documentation. Thanks for your suggestion though – ndemou Jun 05 '14 at 06:54
  • Server Fault is not the place for this, so I'll have to keep my close vote. – Cristian Ciupitu Jun 05 '14 at 13:32
  • Why is it not? It does seem like a very good place to me (but I've been wrong a few times in the past :). I can surely rephrase my question to "game the system" but I don't want to trick a community that has served me well and I value a lot. – ndemou Jun 05 '14 at 17:29
  • Your question is related to servers (and desktops), no doubt about it, but Server Fault isn't the place where anything goes. From [What types of questions should I avoid asking?](http://serverfault.com/help/dont-ask): "You should only ask practical, *answerable questions based on actual problems that you face*. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page.". As a suggestion, [Unix & Linux](http://unix.stackexchange.com/) *might* be more appropriate for this kind of question, but please double check their rules before asking. – Cristian Ciupitu Jun 05 '14 at 18:00
  • Regarding gaming the system, asking for a way to make the network interfaces be named in a specific manner, should be fine. – Cristian Ciupitu Jun 05 '14 at 18:09
  • I don't think my question was either chatty or open-ended (however I'm biased because I like me as a person a lot) but your suggestion about "asking for a way to make the network interfaces be named in a specific manner" seems good to me. Edited my question but after so many hours/days I'm loosing hope of getting an answer :( – ndemou Jun 06 '14 at 07:39
  • I've retracted my closing vote. It wouldn't hurt to also mention what operating system are you using, because a lot have changed in udev in the last years. Some rules might not work anymore. – Cristian Ciupitu Jun 06 '14 at 08:44

2 Answers2

5

To answer your specific question, add this to persistent-net.rules:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="AA:AA:AA:AA:AA:AA", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="BB:BB:BB:BB:BB:BB", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="CC:CC:CC:CC:CC:CC", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="DD:DD:DD:DD:DD:DD", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth1"

Leave persistent-net-generator.rules alone. udev will not overwrite the rules you added manually but the generator rules will cause new rules to be added for new cards as usual.

András Korn
  • 641
  • 5
  • 13
  • Many thanks András. I obviously should add the above to /etc/udev/rules.d/70-persistent-net.rules, right? Should I also empty the /etc/udev/rules.d/75-persistent-net-generator.rules file so that the system doesn't overwrite my custom 70-persistent-net.rules? If I should will the system follow default naming scheme for any other network card? – ndemou Aug 30 '14 at 13:28
  • I updated my answer to address these questions as well. – András Korn Aug 31 '14 at 15:08
  • 2
    András, it's best to remove the KERNEL="eth*" part of your rules otherwise they will not apply in cases where during boot the kernel gives a name that is not like eth* to an interface (in my case it was p9p1. I removed the KERNEL constrain and the rules got applied as expected). – ndemou Jun 05 '15 at 12:30
1

Usually all I want to do in terms of naming interfaces is to swap eth0 and eth1. I let the system create the file /etc/udev/rules.d/70-persistent-net-rules, then edit it by switching the names, restart, then configure the interface stanzas in the network config files. Tedious but gets it done. There's a hint at the top of the file about this:

/etc/udev/rules.d/70-persistent-net.rules 
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x108c (e1000e) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:25:90:22:74:24", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
nortally
  • 381
  • 2
  • 11