15

So what I'm looking for is an interface index (or name) but the real one. if_nametoindex(3) and ioctl(2) don't seem to help me here. Now the only way I found how to do this is via /sys. Let me demonstrate on example what I need:

# cat /sys/class/net/bond0.1007/ifindex 
26
# cat /sys/class/net/bond0.1007/iflink
23                                  <-- I need either this or a reference to bond0
# cat /sys/class/net/bond0/ifindex 
23

Is there a way to do it with a function call or the only way is sysfs?

6
  • Look at what ip link show does (probably using the netlink api) Aug 14, 2014 at 14:01
  • See also /proc/net/vlan/config or /proc/net/vlan/bond0.1007 if it's a vlan interface. Aug 14, 2014 at 14:02
  • @StéphaneChazelas Thanks. I decided to keep using /sys. /proc or /sys are more or less the same in that sense. I just wanted to check if there is a syscall to get this kind of info, it turns out there is not.
    – UVV
    Aug 15, 2014 at 10:39
  • 1
    the syscalls would be the sendmsg/recvmsg using the netlink socket API. Aug 15, 2014 at 10:52
  • 1
    rtnetlink documentation: man7.org/linux/man-pages/man7/rtnetlink.7.html - issue RTM_GETLINK message and look for IFLA_LINK attribute in reply.
    – ecatmur
    Mar 23, 2015 at 19:50

1 Answer 1

4

The command ip link show is probably what you want. It will display the index in front of the interface information. Example below from a system with bonded NICs.

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 9214 qdisc mq master bond0 portid 000f53000000 state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff
3: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 9214 qdisc mq master bond0 portid 000f53000001 state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 9214 qdisc noqueue state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff

You must log in to answer this question.

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