11

with logical I mean everything legal in the command ip link as in, for instance:

ip link add link dum0 name dum0.200 type vlan protocol 802.1Q id 200

where the logical type would be "vlan". All valid types are, to quote the man page:

vlan | veth | vcan | dummy | ifb | macvlan | macvtap | can | bridge | ipoib | ip6tnl | ipip | sit | vxlan |gre | gretap | ip6gre | ip6gretap | vti

Note that this clearly is not the physical device type (like ethernet, wifi, ppp etc.) as asked in this question, which does contain a gem of a reference to the physical type which led me to test for it :

find /sys/class/net ! -type d | xargs --max-args=1 realpath | 
  while read d; do 
    b=$(basename $d) ; n=$(find $d -name type) ; echo -n $b' ' ; cat $n;
 done
dum0.200 1
dum0.201 1
dum1.300 1
dum1.301 1
dummy0 1
ens36 1
ens33 1
lo 772
dum0 1
dum1 1
wlan0 1

But which apparently finds both dummy, vlan and wlan devices to be of type ARPHRD_ETHER.

Does somebody know more? Thanks in advance.

5
  • In case of a vlan, /sys/class/net/$devname/uevent contains the entry DEVTYPE=vlan, but that is not a complete answer, since for example loopback (lo), point-to-point, and wired ethernet-devices (eth0) do not contain that. However, my wireless device has DEVTYPE=wlan. Apr 4, 2016 at 8:53
  • First lead and goes in a strange direction as a logical type wlan is not found in the ip commands.
    – JdeHaan
    Apr 4, 2016 at 9:13
  • I don't quite understand the question. Do you want to determine if a particular network device represents a non-physical device? And if so, which kind?
    – Otheus
    Apr 6, 2016 at 22:15
  • @Otheus : I want to determine the type of network devices on my machines to understand and influence their settings. Installing apps that intervene deep into Linux, like VMware give me a lot of possibilities but I want more, like testing bonding, vlans, iscsi, virtual switches. Combine that with KVM, which is even more unlimited, and the apps interface becomes a restriction and you have to do things by hand. Wanting to do that first requires knowledge about what the damn things are, then you can decide what and how to change settings.
    – JdeHaan
    Apr 7, 2016 at 7:34
  • refer: stackoverflow.com/questions/4475420/…
    – Jichao
    Jul 23, 2019 at 2:56

2 Answers 2

20

A simpler solution:

ip -details link show

For virtual devices, device type is shown on the third line.

3
  • 2
    ip -details -j link show | jq '.[] | .ifname, .link_type, .linkinfo.info_data.type' # using stedolan.github.io/jq
    – JdeHaan
    Apr 26, 2021 at 8:41
  • For some reason, an interface "vboxnet0", which was added on my host by Virtualbox for Host-only networking does not show any type when I invoke ip -details link show. I believe it should show up as a bridge connection. How to verify that?
    – mnj
    May 9, 2021 at 17:26
  • @JdeHaan: Interesting. For me, your output writes null after "eth0" "ether", after "lo" "loopback" and after "wlan0" "ether", and after "tun0" it writes "none", "tun". So if I look for the second line to determine the type then tun-device will be "none"; if I look for the third line then ethernet devices will be null. May 13, 2021 at 12:15
6
+25

There is a way looping over all available types, and showing all interfaces per type (using ip link show type <type>). From this, one can collect the interfaces for all types, and then parse for the interface one want's to know about. It's not elegant, but works:

Using bash:

#!/bin/bash

# Arguments: $1: Interface ('grep'-regexp).

# Static list of types (from `ip link help`). NOTE: On my machine, not all types are listed there, e.g. the type `tun`. And the list of types may change over time. So do not ultimately rely on this list here!:
TYPES=(bareudp bond bond_slave bridge can dummy erspan geneve gre gretap hsr ifb ip6erspan ip6gre ip6gretap ip6tnl ipip ipoib ipvlan ipvtap lowpan macsec macvlan macvtap netdevsim nlmon rmnet sit tap tun vcan veth vlan vrf vti vxcan vxlan xfrm)

iface="$1"

for type in "${TYPES[@]}"; do
  ip link show type "${type}" | grep -E '^[0-9]+:' | cut -d ':' -f 2 | sed 's|^[[:space:]]*||' | while read _if; do
    echo "${_if}:${type}"
  done | grep "^${iface}"
done

Save this to a file, make it executable, and run it with your interface you want to know about as argument.

For the example of dum0.200 beeing of type vlan over the link eth0 (created with ip link add link eth0 name dum0.200 type vlan protocol 802.1Q id 200), the output would be dum0.200@eth0:vlan, indicating that it is of type vlan. Note that the @eth0 comes from ip link show and could be parsed away if one wants to.

Since the argument to this script is interpreted as a grep-regexp, specifying nothing lists all which ip link show type <type> outputs, or specifiying just a prefix lists some, etc.

Note: (added 2021-05-12): "Normal" ethernet devices and also the loopback device seem to not have any type. So the script will not list them. If it is desired to also list devices without a type, it needs to be extended or re-written.

5
  • 1
    Hum, this does not work on Ubuntu 14.04.4 : me@laptop:/home/me >ip link show type dummy Error: either "dev" is duplicate, or "dummy" is a garbage. me@laptop:/home/me >dpkg -l iproute2 ii iproute2 3.12.0-2ubuntu1 amd64 networking and traffic control tools me@laptop:/home/me >
    – JdeHaan
    Apr 4, 2016 at 14:53
  • 1
    But it does on 16.04b2 with iproute2 4.3.0-1ubuntu2 ! I'll check out limitations but it looks like we got at least a partial winner ;-)
    – JdeHaan
    Apr 4, 2016 at 15:15
  • I executed it. Although ip link show shows me 9 devices, your script identifies only 4 of them.
    – mnj
    May 9, 2021 at 17:29
  • @Loreno: Normal ethernet devices do not seem to have a type and so the scripts does not care about them. I updated my answer accordingly. Also, the list of types seems to change over time; I updated the list of types in my script. Does maybe any of those two have affected you? (See ip -details link show for the type of devices, if they have any.) May 12, 2021 at 10:53
  • RIght now I have just 7 interfaces (I have no idea where the other 2 have gone...). The script still identifies 4 devices. However, the other 3 are: lo, eno1, wlp58s0 - so it matches what you've added in your answer :) However, that's weird that these interfaces do not have any type. Why not have them as "physical", or "lo" for the lo interface.
    – mnj
    May 12, 2021 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.