If my target has one device connected and many drivers for that device loaded, how can I understand what device is using which driver?

share|improve this question

migrated from stackoverflow.com Jun 28 '12 at 8:37

This question came from our site for professional and enthusiast programmers.

up vote 24 down vote accepted

Just use /sys.

Example. I want to find the driver for my Ethernet card:

$ sudo lspci
...
02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller (rev 01)
$ find /sys | grep drivers.*02:00
/sys/bus/pci/drivers/r8169/0000:02:00.0

That is r8169.

First I need to find coordinates of the device using lspci; then I find driver that is used for the devices with these coordinates.

share|improve this answer
22  
lspci -v does it by itself. – poige Jul 1 '12 at 4:50
5  
lspci -nk will show you attached drivers. In general the sysfs is the right place to search for. – 0andriy Nov 18 '15 at 20:03
    
@AndyShevchenko thank you! This will be a great timesaver for me :-D – pepoluan Nov 29 '16 at 3:19
    
I know the OP asked for "drivers being used", but what if the driver is not installed nor being used? How to find out just by the vendorID:productID? Also, what if it is not a PCI device, and you only see it in lsusb for example? – Dr Beco Jun 26 '17 at 18:48
1  
@DrBeco: But if driver is not installed, what do you want to find? You should just google in this case – Igor Chubin Jun 27 '17 at 15:18

Here's a little script I wrote:

#!/bin/bash
for f in /sys/class/net/*; do
    dev=$(basename $f)
    driver=$(readlink $f/device/driver/module)
    if [ $driver ]; then
        driver=$(basename $driver)
    fi
    addr=$(cat $f/address)
    operstate=$(cat $f/operstate)
    printf "%10s [%s]: %10s (%s)\n" "$dev" "$addr" "$driver" "$operstate"
done

Sample output:

$ ~/what_eth_drivers.sh 
      eth0 [52:54:00:aa:bb:cc]: virtio_net (up)
      eth1 [52:54:00:dd:ee:ff]: virtio_net (up)
      eth2 [52:54:00:99:88:77]: virtio_net (up)
        lo [00:00:00:00:00:00]:            (unknown)
share|improve this answer
1  
I much prefer reading links to finding/grepping. Nice solution. – Chris Mendez Jan 29 '16 at 16:24
    
Thanks! Way better than the unreliable 'dmesg|grep' (ring buffer...) – Dominik R Feb 10 '16 at 15:04
    
I'd like to find solution which would find also veth and other virtual drivers. IMHO the only solution is to use ethtool or lshw. – pevik Jul 20 '17 at 21:37

You can use the lsmod command to get the status of loaded modules / devices drivers in the Linux Kernel.

For a specific device, you can use dmesg |grep <device-name> to get the details too.

share|improve this answer
1  
Thanks. But if i loaded two drivers for a device with same major no and different minor no ,and if only one driver is being used for the device ,how can I find which driver is used for that device? – Deepu Jun 27 '12 at 6:34
    
perhaps this SO question can help you further. – gkris Jun 27 '12 at 6:49
    
If your system has not been online so long that the ring buffer has re-started, sure dmesg | grep <device-name> will work ; this doesn't work on any of my routers, however. – cjac Mar 7 '16 at 1:47

sudo lspci -v will show it. like this:

$ sudo lspci -v
00:01.0 VGA compatible controller: Advanced Micro  Devices, Inc......
...
Kernel driver in use: radeon
Kernel modules: radeon

You can also combine it with grep like this:

$ sudo lspci -v | grep -A 20 VGA
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.