Linux 中的环回适配器名称

发布于 2024-08-09 02:43:18 字数 67 浏览 5 评论 0原文

可以安全地假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

染墨丶若流云 2024-08-16 02:43:18

除了 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 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.

何以畏孤独 2024-08-16 02:43:18

根据我的经验,这是一个常见的名称,尽管您不应该总是相信它是如此。也许枚举接口并查找地址为 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?

梦里兽 2024-08-16 02:43:18

这是一个相当古老的惯例,事实上,我还没有见过不称其为“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).

策马西风 2024-08-16 02:43:18

接口可以重命名为您想要的任何内容 - 但任何重命名环回接口的人都非常愚蠢,应该拥有一个无法正常工作的系统:)

是的,您可以枚举接口并获取它们的名称。但也许也很容易假设它会“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".

Bonjour°[大白 2024-08-16 02:43:18

使用 127.0.0.1 可能是解决此问题的故障安全方法。

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

清眉祭 2024-08-16 02:43:18

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文