I need to read the master of a network interface from a bash script.

For example, I have these two interfaces...

$ ip link show tap0
4: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master br0 state DOWN mode DEFAULT group default qlen 1000
    link/ether c6:3f:96:13:02:25 brd ff:ff:ff:ff:ff:ff
$ ip link show br0
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 30:9c:23:75:ad:b1 brd ff:ff:ff:ff:ff:ff

Given the name of an interface I need to find the master of that interface, so tap0 -> "br0" and br0 -> ""

I can parse the output of ip link show...

ip link show tap0 | head -n1 | sed 's/.*master \([^ ]*\).*/\1/' | grep -v " "

which outputs br0 (or nothing if I replace tap0 with a device that has no master)

But I would prefer a more robust way of reading this information. Is there some kind of procfs style way to read this? Or a command that can output just the master of an interface?


You can write a script that gathers this information by doing the following (which is probably what ip and ifconfig do)

  • Go to /sys/class/net

  • There are symbolic links to all network adapters in the sysfs filesystem. Ones corresponding to physical hardware are symlinked to ../../devices/pciXXXX:XX/..., others are symlinked to ../../devices/virtual.

  • If you cd to one of the virtual ones, there is a master symlink that points to that interface's master. If there is no master this probably won't exist.

  • Exactly what I needed, thanks. Could add an example of how to get the master name in bash. Perhaps: interface_master(){ [ -e /sys/class/net/$1/master ] && basename $(readlink /sys/class/net/$1/master); } which gives the desired mapping - as per my example; $ interface_master tap0 # prints br0 $ interface_master br0 # prints nothing – DanJAB Jul 25 '18 at 14:17

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.