Linux 中的环回适配器名称
可以安全地假设 Linux 系统上的环回网络适配器将始终被称为“lo”吗?这只是可能不遵守的命名约定,还是必须始终如此?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了
lo
之外,我不知道还有哪个 Linux 系统具有环回接口。如果我编写特定于系统的脚本,我会依赖此命名约定,但在编写可移植程序时则不会。例如,OSX 中的环回是lo0
。C 中一种可靠的方法是在套接字上调用
SIOCGIFCONF
ioctl
,迭代接口,调用SIOCGIFFLAGS
ioctl
在每个接口上,检查哪些接口设置了IFF_LOOPBACK
标志(请参阅/usr/include/linux/if.h
)。SIOCGIFCONF
还将为您提供接口名称。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 islo0
.A reliable way in C is calling a
SIOCGIFCONF
ioctl
on a socket, iterating over the interfaces, callingSIOCGIFFLAGS
ioctl
on each one, and checking which interfaces have aIFF_LOOPBACK
flag set (see/usr/include/linux/if.h
).SIOCGIFCONF
will also give you interface names.根据我的经验,这是一个常见的名称,尽管您不应该总是相信它是如此。也许枚举接口并查找地址为 127.0.0.1 的接口是一种可行的方法?
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?
这是一个相当古老的惯例,事实上,我还没有见过不称其为“lo”的 Linux 盒子/发行版。
然而,*nix 系统中的设备名称非常多样化,可以假设它们会发生变化。如果您想要可移植性,请使用标准(在本例中为 127.0.0.1)。
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).
接口可以重命名为您想要的任何内容 - 但任何重命名环回接口的人都非常愚蠢,应该拥有一个无法正常工作的系统:)
是的,您可以枚举接口并获取它们的名称。但也许也很容易假设它会“lo”。
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".
使用 127.0.0.1 可能是解决此问题的故障安全方法。
Using 127.0.0.1 is probably the failsafe way to go about it.
RFC3330 定义
127.0.0.0/8
始终为环回子网。但是,
localhost
的使用在 Windows 上定义在c:\windows\system32\drivers\etc\hosts
中,在 Linux 上定义在/etc/hosts
中纯粹是约定。此外,名称lo
是 Linux 中本地主机接口的典型名称。如果您必须绝对确定,请使用
127.0.0.1
。RFC3330 defines
127.0.0.0/8
to always be the loopback subnet.The use of
localhost
however, defined on Windows inc:\windows\system32\drivers\etc\hosts
and Linux in/etc/hosts
is purely convention. Furthermore the namelo
is the typical name given to the localhost interface in Linux.If you must be absolutely certain, use
127.0.0.1
.