Skip to content
Advertisement

How to get linux interface alias(IFLA_IFALIAS) programatically for an interface?

We can set alias to an interface using the command

ip link set eth0 alias somename

To see that;

ip link show eth0

It is set as an alias. Now I want to read this in a C program, and I am not having an idea. I did make a google search, and even tried to look into the iproute2 (provider of this cli), but to no avail. Can any of you help me on this?

Please refrain from giving cli output to file and regex solution.

Advertisement

Answer

If you want to do that in C, you’ll have to do some netlink programming:

https://www.infradead.org/~tgr/libnl/

See here about interfaces, and its section “3.5.11. IfAlias”.

Netlink sockets are a very generic, extensible and thus powerful mechanism, yet this comes with a price of complexity and steep learning curve. Documentation is quite, er, “raw”, and nothing close to a step-by-step extensive tutorial. You still can find some short tutos here and there, though for limited, simple use cases. Hence, expect quite a bit of work.

Another option would be to study the source code of iproute2, which do use netlink sockets, yet not relying on libnl* libraries but rather doing so all by itself, which 0/ (among other things) make it quite complicated 1/ does not help to create a re-usable outside of iproute2 implementation. I would not recommend this approach: as difficult if not more than using libnl*, plus libnl* knowledge is a reusable investment.

http://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/tree/

PS:

There is a (“netlink”)tag on SO[https://stackoverflow.com/questions/tagged/netlink].

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement