views:

316

answers:

6

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?

+1  A: 

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?

ZombieSheep
A: 

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.

Matt Joiner
RFC3330 defines 127.0.0.0/8 as the subnet for loopback addresses. All it says about 127.0.0.1 is that it is ordinarily used as the loopback address. You could be using 127.0.1.1 as a loopback address if you want. -1 for not reading the RFC you linked to and then giving wrong advice.
camh
+6  A: 

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.

Alex B
This appears to have the advantage of not requiring any IPv4 ("127.0.0.1") address to be configured on the system.
ndim
+1  A: 

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).

Jurily
+1  A: 

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".

MarkR
A: 

Using 127.0.0.1 is probably the failsafe way to go about it.

Amit