Naming convention standard for Ethernet and Wi-Fi interfaces on Linux machines

10,203

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
10,203

Related videos on Youtube

Mohan Raj
Author by

Mohan Raj

Updated on September 18, 2022

Comments

  • Mohan Raj
    Mohan Raj almost 2 years

    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?

    • Jeff Schaller
      Jeff Schaller over 5 years
    • Patrick Mevzek
      Patrick Mevzek over 5 years
      I would look at how iwconfig does filter WiFi interfaces from other ones.
    • dribler
      dribler over 5 years
      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?
    • Nikhath K
      Nikhath K over 5 years
      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...)?"
  • Mike Ounsworth
    Mike Ounsworth over 5 years
    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.
  • dirkt
    dirkt over 5 years
    @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.
  • John H
    John H over 5 years
    And some of us are staunch systemd refuseniks.
  • slebetman
    slebetman over 5 years
    @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
  • Johan Myréen
    Johan Myréen over 5 years
    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.
  • Jörg W Mittag
    Jörg W Mittag over 5 years
    @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.
  • TooTea
    TooTea over 5 years
    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.
  • RalfFriedl
    RalfFriedl over 5 years
    @JohanMyréen That is wrong: "... that they (interface name) stay fixed even if hardware is added or removed" freedesktop.org/wiki/Software/systemd/…
  • Johan Myréen
    Johan Myréen over 5 years
    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.
  • ilkkachu
    ilkkachu over 5 years
    @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.
  • Barmar
    Barmar over 5 years
    The question asks about conventions, which implies that it's only expected to work if the sysadmin adheres to the convention.
  • RalfFriedl
    RalfFriedl over 5 years
    @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.
  • user1908704
    user1908704 over 5 years
    systemd helpfully calls one of my ethernet interfaces 'rename3'. Which one it is can vary, sigh :(
  • user1686
    user1686 over 5 years
    @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.
  • user1908704
    user1908704 over 5 years
    @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.
  • user1686
    user1686 over 5 years
    @user1908704: ...you just cannot trust firmware these days, can you. Systemd ought to have already have given up with its 'predictable' scheme after encountering all those motherboards which rearrange PCI slots on reboot (which makes the names not predictable in any way), but there's an ACPI mechanism specifically to avoid that problem and manufacturers screw that up as well?
  • Harald
    Harald over 5 years
    FWIW they can even change with different releases of systemd...