5

I see some good explanation for fetching information about network cards and their statistics in ubuntu on this page. This gave a nice output as mentioned on the page. I tried reading other documentations too but could not find a flag or something similar where I can differentiate between the real and virtual network cards on my system.

Is there are way to differentiate ? Thanks.

1
  • 1
    Note that for HVM devices there may be no way of telling as hypervisors emulate a real device (usually Intel or Realtek). May 30, 2017 at 12:03

2 Answers 2

6

Check the /sys/class/net/<device_name> symlink. If it points into /sys/devices/virtual/, then it is a virtual interface. If it points to a "real" device (e.g. into /sys/devices/pci0000:00/), then it is not.

Edit:

From code, you can use readlink to check if the device is virtual. Here is a very dummy sample code to do so:

#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
    char theLink[128];
    char thePath[128];

    strcpy(thePath,"/sys/class/net/");
    memset(theLink,0,128);

    if (argc>1) {
        strcat(thePath,argv[1]);
    } else {
        printf("Gimme device\n");
        return 1;
    }

    if (readlink(thePath, theLink, 127)==-1) {
        perror(argv[1]);
    } else {
        if (strstr(theLink,"/virtual")) {
            printf("%s is a virtual device\n",argv[1]);
        } else {
            printf("%s is a physical device\n",argv[1]);
        }
    }
}
7
  • sounds promising, anything similar for virtual disks also ? And it seems like I need to do ls -l for confirming this. Any way from code ?
    – Gagan93
    May 30, 2017 at 7:40
  • For disks, check the symlinks in /sys/class/block. Also, I added a primitive sample to check if the device is virtual.
    – Lacek
    May 30, 2017 at 8:22
  • The sample works excellent for EC2 instances also, for the /sys/class/block link, probably I used the wrong word (disk). I need for mount points (/root, /mnt, etc). Is there a local in system for these also ? @Lacek
    – Gagan93
    May 30, 2017 at 9:58
  • I think mount points can only be extracted from /proc/mounts (on Linux, this is a symlink to /proc/self/mounts). Parsing that file is a bit more difficult though.
    – Lacek
    May 30, 2017 at 10:57
  • @Gagan93: What do you mean by virtual mount points? Every since the big VFS re-write ~20 years ago, I don't think that makes much sense. The current Linux VFS is very Plan9-like where everything is a (potential) file system, everything is a (potential) mount point, and every mount point is (in some sense) virtual. May 30, 2017 at 13:23
0

Other way is to check if the file /sys/class/net/<interface>/device exists. Usually docker and other virtual interfaces doesn't have that file (which is actually a symlink to the device). This should work:

for device in $(ls /sys/class/net/); do
  if [ -L /sys/class/net/$device/device ]; then
     echo "Device : $device"
  else
     echo "Virtual: $device"
  fi
done 

Output (example executed in my laptop):

Virtual: docker0
Virtual: docker_gwbridge
Device : enp0s25
Virtual: lo
Virtual: veth7a67a62
Device : wlo1
Virtual: ztmjfg2jdx

Tested in Ubuntu 22.04 not sure if the same applies to other distributions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .