I have a setup with a bunch of vlan interfaces on a physical interface.
Physical interface: eth1
VLANS on top of this: vlan1, vlan2, vlan3
Now, I want to know which is the parent interface of my vlan (for example, here eth1 is the parent interface of these vlans).
I can get this information by running "ip addr show vlan-name" and then in output, I will get vlan1@eth1, but I need to parse the output of this command or by looking at my network config file, parsing it and interpreting it.
Is there another way by which I can get this information without any parsing logic? For example, for bonded interfaces, the information is present in /sys/class/net/ directory and one can simply read files there.
# cat /sys/class/net/bond0/bonding/slaves
eth0 eth1
Is there a similar path/file available for vlan tagged interfaces? I couldn't figure out if there is some file I can just read without any parsing and extract this information or any command/utility that just gives the parent interface name.
Kindly do let me know if there are other alternatives to this.
Thanks.
Best Answer
I didn't find a way without any parsing to get the underlying interface, so I give 5 (sometimes just slightly) different ways to get this information. The method 5 added last should be preferred: it uses the JSON output of the
ip
command.There's a symlink having the name of the physical interface:
$ ls -l /sys/class/net/vlan2
[...]
lrwxrwxrwx. 1 root root 0 nov. 10 02:12 lower_eth1 -> ../eth1
[...]
-rw-r--r--. 1 root root 4096 nov. 10 01:49 uevent
Method 1
There's
uevent
that is handy too:Method 2
As root, there are entries in
/proc/net/vlan
:# cat /proc/net/vlan/vlan2
[...]
Device: eth1
[...]
Method 3
Method 4
Note that in /sys the other direction is also possible with
upper_*
:[...]
Since 2017
ip
(which uses the (rt)netlink socket interface to communicate with the kernel) got a JSON output for many of its subcommands to ease its scriptability. Together with thejq
command this is probably the best choice:Method 5