Is it safe to assume that the loopback network adapter on a Linux system will always be called 'lo' - is this just a naming convention that may not be adhered to, or must it always be the case?
In my experience it is a common name, although you shouldn't always trust in it being so. Maybe enumerating the interfaces and looking for the one with an address of 127.0.0.1 would be the way to go?
RFC3330 defines 127.0.0.0/8
to always be the loopback subnet.
The use of localhost
however, defined on Windows in c:\windows\system32\drivers\etc\hosts
and Linux in /etc/hosts
is purely convention. Furthermore the name lo
is the typical name given to the localhost interface in Linux.
If you must be absolutely certain, use 127.0.0.1
.
I don't know of any Linux system that has a loopback interface anything other than lo
. I would rely on this naming convention, if I write a system-specific script, but not when writing a portable program. For example loopback in OSX is lo0
.
A reliable way in C is calling a SIOCGIFCONF
ioctl
on a socket, iterating over the interfaces, calling SIOCGIFFLAGS
ioctl
on each one, and checking which interfaces have a IFF_LOOPBACK
flag set (see /usr/include/linux/if.h
).
SIOCGIFCONF
will also give you interface names.
It's a pretty old convention, in fact I have not seen a Linux box/distro yet that didn't call it 'lo'.
However, device names in *nix systems are so diverse it can be assumed they will change. Use the standards if you want portability (in this case, 127.0.0.1).
Interfaces can be renamed to anything you want - but anyone who renames the loopback interface is being extremely silly and deserves to have a nonworking system :)
Yes, you can enumerate the interfaces, and get their names. But perhaps it's just as easy to just assume it's going to be "lo".