Score:0

Finding the name of a wireless interface from kernel module

uz flag
IoT

I have a WiFi PCI-e card that adds two wireless interfaces to my Linux OS. I am writing a script that basically automates the initialization and configuration of this WiFi card. However, I would like to find a way to get the name of the wireless interfaces that appears in my system after loading the corresponding Linux Kernel module, i.e., this card's driver.

I came up with the following two solutions but they are still not ideal:

  1. Check the Kernel log output(dmesg | grep "driver_name") but I prefer another way in which I do not rely on a log file as it might get over-written.

  2. Check the output of lshw -C network but this is a bit slow and requires a bit of parsing for the output string.

Any better solution to do this?

chili555 avatar
cn flag
How about: `iwconfig` ?
Marco avatar
br flag
Usually all this is handled by `udev` and you can add your own rules there.
uz flag
IoT
@chili555 `iwconfig` does not do what I want. It just show a list of wireless interfaces but I am interested in the ones loaded by specific kernel module
uz flag
IoT
@mpboden it does not show the name of the wireless interface
uz flag
IoT
@Marco can you elaborate on this with any example, please?
Marco avatar
br flag
It's quiet complex and depending on your installation. But there are plenty of examples in the web, e.g. https://wiki.archlinux.org/title/udev
Marco avatar
br flag
In short, as soon as a device is detect the `udev` is triggered. Usually the rules create nodes in `/dev` or mount something etc. But scripts can be triggered, too. I think this is the right place for you to add the functionality you want. Maybe there is already a rule for your device and you just have to modify it a bit.
uz flag
IoT
@mpboden the name of the wireless interface that you get in ifconfig or iwconfig e.g., wlp0s20f3.
uz flag
IoT
@Marco that is not what I am looking for and I want to execute my script at a later time, not during the device boot-up. I need a simple mechanism just to let me find out what Ethernet/wireless interfaces were added to the system when a certain kernel module was loaded.
Score:1
do flag

If you know the name of the driver that the device will be bound to, then you can search the /sys filesystem and get a list of network devices that are using it.

If you don't know the name of the driver just yet, you can find it first with: lspci -nnk | grep -i 'network\|ethernet' -A3.

Example 1

Let's find the drivers:

$ lspci -nnk | grep -i 'network\|ethernet' -A3
06:00.0 Ethernet controller [0200]: Intel Corporation 82574L Gigabit Network Connection [8086:10d3]
        Subsystem: Super Micro Computer Inc 82574L Gigabit Network Connection [15d9:10d3]
        Kernel driver in use: e1000e
        Kernel modules: e1000e
07:00.0 Ethernet controller [0200]: Intel Corporation 82574L Gigabit Network Connection [8086:10d3]
        Subsystem: Super Micro Computer Inc 82574L Gigabit Network Connection [15d9:10d3]
        Kernel driver in use: e1000e
        Kernel modules: e1000e
08:08.0 FireWire (IEEE 1394) [0c00]: Texas Instruments TSB43AB22A IEEE-1394a-2000 Controller (PHY/Link) [iOHCI-Lynx] [104c:8023]

Notice that is says Kernel driver in use: e1000e. That's the driver my network cards are using. For your system, substitute your driver name in place of e1000e in the following example.

Now that we know the driver in use, e1000e, I can search the /sys filesystem and find which network devices are using this driver with the following command:

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "e1000e" {} \; 2> /dev/null
/sys/class/net/eth0/device/uevent:DRIVER=e1000e
/sys/class/net/eth1/device/uevent:DRIVER=e1000e

From there, I can use awk to get the network device names:

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "e1000e" {} \; 2> /dev/null | awk -F / '{print $5}'
eth0
eth1

Example 2

As another example on a different computer, I have both WIFI and an ethernet controller, each with a different driver.

Let's find the drivers:

$ lspci -nnk | grep -i 'network\|ethernet' -A3
09:00.0 Network controller [0280]: Intel Corporation Device [8086:2725] (rev 1a)
    Subsystem: Intel Corporation Device [8086:0020]
    Kernel driver in use: iwlwifi
    Kernel modules: iwlwifi
--
0b:00.0 Ethernet controller [0200]: Intel Corporation Device [8086:15f2] (rev 03)
    Subsystem: Lenovo Device [17aa:22d8]
    Kernel driver in use: igc
    Kernel modules: igc

WIFI with the iwlwifi driver:

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "iwlwifi" {} \; 2> /dev/null
/sys/class/net/wlp9s0/device/uevent:DRIVER=iwlwifi

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "iwlwifi" {} \; 2> /dev/null | awk -F / '{print $5}'
wlp9s0

Ethernet Controller with the igc driver:

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "igc" {} \; 2> /dev/null
/sys/class/net/enp11s0/device/uevent:DRIVER=igc

$ find -L /sys/class/net -maxdepth 3 -name "uevent" -exec grep -iH "igc" {} \; 2> /dev/null | awk -F / '{print $5}'
enp11s0
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.