13

What is the naming convention standard for Ethernet and Wi-Fi interfaces on a Linux machine?

We are developing a tool that should show only the Ethernet and Wi-Fi interfaces of the Linux machine and its current status.

For example, below is the list of network interfaces (both physical and virtual) on my Linux (Ubuntu) machine:

docker0, enp0s25, lo, wlp3s0

When I run the tool, below is the result I get:

enp0s25, wlp3s0

We have written the code with the logic that all the Ethernet interfaces always start with the letter e and Wi-Fi interfaces always start with the letter w.

Is the logic right? If not, how can we address this?

  • related: unix.stackexchange.com/a/179931/117549 – Jeff Schaller Nov 1 '18 at 15:32
  • I would look at how iwconfig does filter WiFi interfaces from other ones. – Patrick Mevzek Nov 1 '18 at 22:46
  • 1
    Is there a reason why you wouldn't look at something like lshw? Specifically inspect the contents of lshw -class network maybe? Look for everything that is capabilities: ethernet physical? Possibly look for some of the other capabilities also? – Zoredache Nov 1 '18 at 23:38
  • To deal with the frame challenge raised by @dirkt, a better question would be simply: "how do I get the type of a network interface on Linux (or macOS, or other...)?" – Roger Lipscombe Nov 3 '18 at 11:29
41

Network interfaces can be named anything, so no matter what you do, you'll run into situations where (1) there is a "physical" network interface with a name that didn't match your pattern, or (2) there is a "physical" network interface that will match your pattern.

On top of that, if I was a user of your tool, the moment your tool wouldn't allow me to do something I want, because I have a network interface which is "virtual", though for practical purposes it should be considered "physical" in my setup, I'd start cursing loudly and forcefully at your app, and won't ever use it again.

Physical and virtual network interfaces all share a common API are one thing which makes Linux really flexible. Please don't try to babysit your user, and take that away from him or her. Your users will thank you.

  • 11
    You haven't actually answered the question; you've spent 3 paragraphs challenging the background context of the question. While I'm aware that frame challenge answers are a thing, this doesn't feel like one of those cases ... especially since user4556274 gave a succinct answer to the question as asked. – Mike Ounsworth Nov 1 '18 at 17:37
  • 18
    @MikeOunsworth: The problem is that the answer of user4556274 doesn't work in the general case, it only works if the interface names are indeed predicatable interface names. Which a simple ip link set ... name ... can change. And yes, I could have listed the predictable interface names myself. The answer to the original question is "please don't do it that way". Because no matter how you go about it, it won't work. – dirkt Nov 1 '18 at 18:27
  • @fpmurphy1 No, I think he means predictable interface names. Doesn't matter if it's systemd, traditional (ethN), your company's specific conventions or any other standards which make the names predictable – slebetman Nov 2 '18 at 4:06
  • 12
    @MikeOunsworth: The answer is in the very first sub-clause of the very first sentence of the very first paragraph: "Network interfaces can be named anything […]" Therefore, what the OP is asking about is impossible and cannot possibly be done. You cannot detect the type of a network interface based on a naming convention standard, because there is no naming convention standard and anybody can name their interfaces anything they want. For example, on my old Linux router, I head colored stickers on the back, and the physical Ethernet interfaces were named red, blue, and green. – Jörg W Mittag Nov 2 '18 at 11:33
  • 1
    @JörgWMittag, of course you can detect them based on a standard, if you know the standard is used (and you're expecting one that exports that information in the name in the first place). We know systemd has a standard for network interface naming, so it's no use saying one doesn't exist. – ilkkachu Nov 2 '18 at 21:19
27

For systemd predictable interface names, the prefixes can be seen in udev-builtin-net_id.c :

* Two character prefixes based on the type of interface:
*   en — Ethernet
*   ib — InfiniBand
*   sl — serial line IP (slip)
*   wl — wlan
*   ww — wwan

so for both the traditional ethX style of naming and the newer systemd naming, an initial letter e should be an ethernet interface for any automatically generated interface names. All wifi interfaces should begin with a w in both schemes, although not all interfaces beginning with w will be wifi.

If this tool has to work in an arbitrary environment (rather than just on internal environments which you control), note that users may rename interfaces on linux systems with arbitrary names, such as [wan0, lan0, lan1, dmz0] which will break any assumptions about initial letters.

  • 7
    And some of us are staunch systemd refuseniks. – chrylis Nov 1 '18 at 18:55
  • 1
    Note that this solution won't work on systems using biosdevname for interface naming, where an Ethernet interface can be named like p3p7. This method is a predecessor of the new systemd predictable names and although it's now deprecated on common distros, there will still be many systems that picked it up when it was the default a few years ago. – TooTea Nov 2 '18 at 11:56
  • systemd helpfully calls one of my ethernet interfaces 'rename3'. Which one it is can vary, sigh :( – user1908704 Nov 3 '18 at 13:06
  • 1
    @user1908704: Usually this means that udev was told to give the same name to multiple interfaces. (Perhaps you have an old auto-generated "persistent names" file in /etc/udev/rules.d?) That said, the rename process was fixed in v187 to be fully atomic (either succeed or rollback) and the rename%u format hasn't existed in the source code since 2012. I would like to sigh at your software being six years out-of-date. – grawity Nov 3 '18 at 15:43
  • 1
    @grawity This is Ubuntu 18.04. It turns out particular motherboards have two NICs with the same ACPI entry, resulting in them both being eno1. Since that can't happen, one of them gets called 'rename3'. I haven't fully worked out the logic of who wins the race, but any changes can perturb the contest and cause the other to be rename3. – user1908704 Nov 4 '18 at 0:01
9

The naming convention is that LAN interfaces are named eth0, eth1, ... and that WLAN interfaces are named wlan0, wlan1, ...

What you see are the so called "predictable names" that systemd introduced. In practice, they are anything but predictable, and they can even change when the hardware is changed, which is exactly the problem they are supposed to avoid.

For a guess the start letter may be good enough. Some interfaces, in particular WLAN, have hints in /sys/class/net/*/uevent:

DEVTYPE=wlan

Unfortunately, there is no such DEVTYPE for LAN interfaces.

  • 2
    Device name changes when the hardware changes is not the problem predictable interface names is trying to avoid. Predictable interface names avoids name changes when the hardware is unchanged. This is a problem when the hardware devices are detected in a random order. – Johan Myréen Nov 2 '18 at 7:10
  • @JohanMyréen That is wrong: "... that they (interface name) stay fixed even if hardware is added or removed" freedesktop.org/wiki/Software/systemd/… – RalfFriedl Nov 2 '18 at 16:16
  • The motivation for introducing the predictable interface names was that probing is not deterministic and "the assignment of the names "eth0", "eth1" and so on not fixed anymore and it might very well happen that "eth0" on one boot ends up being "eth1" on the next." It is a bonus if the predictable names can survive hardware changes, but this was not the primary reason for them. – Johan Myréen Nov 2 '18 at 19:12
  • 1
    Win some lose some - in some scenarios, it is very desirable if changed hardware reliably clicks into the same "naming slot" :) – rackandboneman Nov 2 '18 at 20:35
  • @JohanMyréen The old scheme of assigning interface names based on MAC or some other properties worked more reliable when adding interfaces, without the hassle of the new names. – RalfFriedl Nov 3 '18 at 7:06
5

One caveat with my answer (applies to most of the others, too): I don't know the purpose of your application. If it is a throwaway application to troubleshoot one particular problem, or to understand networking better, never to be used again, then relying on the first letter of the interface may be a great quick-and-dirty option. If you are planning to write the next competitor to Wireshark or tcpdump, you need to be sure you get it right for all kinds of edge cases.

And if the application you are writing falls somewhere between those extremes, only you (and your customers) can know how carefully you need to implement your logic.

Others have already pointed out that the names are never reliable, for any number of reasons. The ultimate problem is a very common one in software: hard-coding assumptions instead of relying on known/documented facts.

The second issue that hasn't been mentioned is also based on an assumption about your requirements: that the list of interfaces you want to list is always exactly "hardware ethernet interfaces" and "wifi interfaces".

The third issue is yet another assumption: that all interface will fall into the categories you can think of right now. How about Infiniband, as mentioned by @user4556274? How about tunnel interfaces for a VPN? How about bridged interfaces? How about bridged interfaces that combine physical and logical interfaces?

But there may be options to accomplish what you are looking for. First, define exactly what characterizes an interface you want to list, vs one that you don't.

In most cases, one characteristic you can rely on is the routing table (however, this will only work as long as the interface is up, so it may not be what you are actually looking for).

Any interface that has a default route (i.e., a route to 0.0.0.0) is likely to be one you are looking for.

Note that even this is still based on an assumption, just a more reliable one: it is conceivable that a system is configured to route all outbound traffic through a virtual machine or a docker container (for instance, if there is a container running a firewall). And the reverse is also true: a sysadmin could potentially lock down outside traffic by deleting the default route.

Another option is to go by the actual hardware and see which driver it uses. You can then exclude certain well-known drivers

4

Are you explicitly excluding bonded or teamed interfaces? Our default here is to use bond0 or team0 for the primary interface for our servers.

I think you need to re-think your logic. Maybe try iterating though all the defined network interfaces on a given system, divide them up between ethernet, wifi, infiband, serial, etc., and then do your magic.

3

Don't hardcode, or pattern match hardware names. This applies to all devices. Rely on the tools provided by udev to determine the list of devices and take appropriate action. See 16.7 Persistent Device Naming, and then read through that entire document, specifically 16.2 and 16.3. Note that udev is distribution agnostic, and also init system agnostic, as udev is now the preferred method for device management in linux.

Notice that @user4556274, alluded to this in his answer when referring to udev-builtin-net-id.c, which in short means that the pattern matching you're trying to accomplish is a builtin part of udev.

Quoting PredictableNetworkInterfaceNames:

With systemd 197 we have added native support for a number of different naming policies into systemd/udevd proper and made a scheme similar to biosdevname's (but generally more powerful, and closer to kernel-internal device identification schemes) the default. The following different naming schemes for network interfaces are now supported by udev natively:

  1. Names incorporating Firmware/BIOS provided index numbers for on-board devices (example: eno1)
  2. Names incorporating Firmware/BIOS provided PCI Express hotplug slot index numbers (example: ens1)
  3. Names incorporating physical/geographical location of the connector of the hardware (example: enp2s0)
  4. Names incorporating the interfaces's MAC address (example: enx78e7d1ea46da) Classic, unpredictable kernel-native ethX naming (example: eth0)

By default, systemd v197 will now name interfaces following policy 1) if that information from the firmware is applicable and available, falling back to 2) if that information from the firmware is applicable and available, falling back to 3) if applicable, falling back to 5) in all other cases. Policy 4) is not used by default, but is available if the user chooses so.

This combined policy is only applied as last resort. That means, if the system has biosdevname installed, it will take precedence. If the user has added udev rules which change the name of the kernel devices these will take precedence too. Also, any distribution specific naming schemes generally take precedence.

2

As others have said you can't fully depend on the name.

In my case it seems that for wireless /sys/class/net/<ifacename>/ will have a directory called "wireless" in it if it is a wireless interface:

# kbrandt @ kbrandtlx in /sys/class/net [9:47:43] C:130
$ ls
br-b293588ecdae  enp11s0  lo    veth6061cd8  virbr0-nic
docker0          enp12s0  ppp0  virbr0       wlp13s0

# kbrandt @ kbrandtlx in /sys/class/net [9:47:44] 
$ echo  /sys/class/net/*/wireless
/sys/class/net/wlp13s0/wireless

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.