Flowmix Leonsio
Flowmix Leonsio

Reputation: 91

How to find physical interfaces on linux (without bridges, aliases, vlans ..)

i need to identify physical interfaces on (Debian) Linux if show into /sys/class/net i see all interfaces including bridges and vlans.

or it is possible with bash to check if one interface exists and if its physical or virtual?

Upvotes: 1

Views: 3560

Answers (5)

raspi
raspi

Reputation: 6122

One way is to match symbolic link paths in /sys/class/net/:

$ ls -la /sys/class/net/

lrwxrwxrwx  1 root root 0 18. 5. 04:51 docker0 -> ../../devices/virtual/net/docker0
lrwxrwxrwx  1 root root 0 18. 5. 04:51 eno1 -> ../../devices/pci0000:00/0000:00:19.0/net/eno1
lrwxrwxrwx  1 root root 0 18. 5. 04:51 enp9s0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:09:00.0/net/enp9s0
lrwxrwxrwx  1 root root 0 18. 5. 04:51 lo -> ../../devices/virtual/net/lo
lrwxrwxrwx  1 root root 0 18. 5. 19:17 macvtap0 -> ../../devices/virtual/net/macvtap0

/devices/virtual/net/ path is present for all virtual interfaces.

So you can use find to match all links that are not containing */devices/virtual/net/*:

$ find /sys/class/net/ -type l ! -lname '*/devices/virtual/net/*'
/sys/class/net/eno1
/sys/class/net/enp9s0

This can be turned to script:

#!/bin/bash

for iface in $(find /sys/class/net/ -type l ! -lname '*/devices/virtual/net/*' -printf '%f '); 
do
  echo "$iface is not virtual"
done 

Example output:

# ./ifs.sh 
eno1 is not virtual
enp9s0 is not virtual

Upvotes: 3

Flowmix Leonsio
Flowmix Leonsio

Reputation: 91

extended solution, works on pi and other devices linke tinker board/rock64

systemctl -a | grep sys-devices-platform | grep '\-net-' | awk '{n=split($1,A,"-"); print A[n]}' | cut -d"." -f1

Upvotes: 0

Flowmix Leonsio
Flowmix Leonsio

Reputation: 91

better solution with systemd (example on raspberry pi with vlans/bridges)

systemctl -a | grep net-devices | grep -v "/sys/subsystem"

Upvotes: 0

Flowmix Leonsio
Flowmix Leonsio

Reputation: 91

two possible solution

  1. show for device Symlink in /sys/class/net/< interface >/ folder, it only appears on physical interfaces

  2. ip -d show link you can find the different types of the interface

    if  [[ ! `ip -d link show ${int_name}  2>/dev/null >/dev/null` ]]; then
        echo "Interface ${int_name} does not exists"
    elif [[ `ip -d link show ${int_name} | tail -n +2 | grep loopback` ]] ; then
        echo is_local
    elif [[ `ip -d link show ${int_name} | tail -n +2 | grep vlan` ]] ; then
        echo is_vlan
    elif [[ `ip -d link show ${int_name} | tail -n +2 | grep bridge` ]] ; then
        echo is_bridge
    else
        echo is_physic
    fi
    

Upvotes: 0

heemayl
heemayl

Reputation: 42007

Check for the DEVTYPE parameter in the uevent file residing in /sys/class/net/<interface>/uevent.

In my bridge interface:

$ cat /sys/class/net/br0/uevent 
DEVTYPE=bridge
INTERFACE=br0
IFINDEX=3

While the real physical interface over which the bridge is created does not have the parameter:

$ cat /sys/class/net/eth0/uevent
INTERFACE=eth0
IFINDEX=2

Upvotes: 4

Related Questions