
If you’re doing a lot of network tinkering, configuring LAN-based apps and server tools, or just curious, you’ve likely noticed that there’s a difference between the localhost the local IP address. Read on to learn the differences.
如果您要进行大量的网络修补,配置基于LAN的应用程序和服务器工具,或者只是出于好奇,您可能已经注意到localhost和本地IP地址之间存在差异。 继续阅读以了解差异。
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。
问题 (The Question)
SuperUser reader Diogo wants to know why the ping command treats the localhost and the local IP address differently when, on the surface, they appear to be the same thing:
超级用户阅读器Diogo想知道当表面上看起来是同一回事时,为什么ping命令为何将本地主机和本地IP地址区别对待:
Using cmd and ping on Windows gave me the following results:
在Windows上使用cmd和ping可得到以下结果:
Pinging “localhost”:
ping“本地主机”:
![]()
Pinging “192.168.0.10” (local IP address):
ping“ 192.168.0.10”(本地IP地址):
![]()
Aren’t both situations exactly the same?
两种情况不是完全一样吗?
I mean, I’m pinging the same interface, the same machine and the same address. Why do I get such different results?
我的意思是,我正在ping相同的接口,相同的计算机和相同的地址。 为什么会得到如此不同的结果?
Obviously there is a difference of some sort, but what exactly is going on when you switch between the two?
显然存在某种差异,但是当您在两者之间切换时究竟发生了什么?
答案 (The Answer)
SuperUser contributor Tom Wijsman offers the following insight into the subtle differences between the two:
超级用户贡献者Tom Wijsman对以下两者之间的细微差别提供了以下见解:
You are not pinging the same interface, without any physical interfaces you still have a “local host”.
您没有对同一接口执行ping操作 ,没有任何物理接口,您仍然拥有“本地主机”。
Your
localhost
is used to refer to your computer from its “internal” IP, not from any “external” IPs of your computer. So, the ping packets don’t pass through any physical network interface; only through a virtual loop back interface which directly sends the packets from port to port without any physical hops.
localhost
用于从其“内部” IP而不是从计算机的任何“外部” IP引用计算机。 因此,ping数据包不通过任何物理网络接口。 仅通过虚拟环回接口,该接口直接将数据包从一个端口发送到另一个端口,而无需任何物理跃点。You might still wonder why
localhost
is resolving to::1
, while traditionally we would expect it to resolve to the IPv4 address127.0.0.1
. Note that.localhost
is traditionally a TLD (see RFC 2606) which points back to the loop back IP address (for IPv4, see RFC 3330, especially 127.0.0.0/8).您可能仍然想知道为什么
localhost
解析为::1
,而传统上我们希望它解析为IPv4地址127.0.0.1
。 请注意,.localhost
是一个TLD(请参阅RFC 2606 ),它指向回送IP地址(对于IPv4,请参阅RFC 3330 ,尤其是127.0.0.0/8)。Looking up
localhost
usingnslookup
gives us:使用
nslookup
查找localhost
给我们:
nslookup localhost
nslookup localhost
... Name: localhost Addresses: ::1 127.0.0.1
... Name: localhost Addresses: ::1 127.0.0.1
Thus Windows prefers to use the IPv6 loop back IP address
::1
(see RFC 2373) as it is listed first.因此,Windows更喜欢使用IPv6环回IP地址
::1
(请参阅RFC 2373 ),因为它首先列出。Okay, so, where does it come from, let’s look at the hosts file.
好吧,那么它来自哪里,让我们看一下hosts文件。
type %WINDIR%\System32\Drivers\Etc\Hosts
type %WINDIR%\System32\Drivers\Etc\Hosts
... # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost ...
Hmm, we have to look at the DNS settings of Windows.
嗯,我们必须看看Windows的DNS设置。
This KB article tells us about a setting that affects what Windows prefers, emphasized in bold:
这篇知识库文章向我们介绍了一种会影响Windows偏好设置的设置,以粗体强调:
In Registry Editor, locate and then click the following registry subkey:
在注册表编辑器中,找到并单击下面的注册表子项:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters
Double-click DisabledComponents to modify the DisabledComponents entry.Note: If the DisabledComponents entry is unavailable, you must create it. To do this, follow these steps:
双击“ DisabledComponents”以修改“ DisabledComponents”条目。注:如果“ DisabledComponents”条目不可用,则必须创建它。 为此,请按照下列步骤操作:
- In the Edit menu, point to New, and then click DWORD (32-bit) Value. 在“编辑”菜单中,指向“新建”,然后单击“ DWORD(32位)值”。
- Type DisabledComponents, and then press ENTER. 键入DisabledComponents,然后按Enter。
- Double-click DisabledComponents. 双击DisabledComponents。
Type any one of the following values in the Value data: field to configure the IPv6 protocol to the desired state, and then click OK:
在“数值数据:”字段中键入以下任意一个值,以将IPv6协议配置为所需的状态,然后单击“确定”:
Type
0
to enable all IPv6 components. (Windows default setting)键入
0
以启用所有IPv6组件。 (Windows默认设置)Type
0xffffffff
to disable all IPv6 components, except the IPv6 loopback interface. This value also configures Windows to prefer using Internet Protocol version 4 (IPv4) over IPv6 by modifying entries in the prefix policy table. For more information, see Source and Destination Address Selection.键入
0xffffffff
以禁用除IPv6环回接口以外的所有IPv6组件。 通过修改前缀策略表中的条目,此值还将Windows配置为优先使用Internet协议版本4(IPv4)而不是IPv6。 有关更多信息,请参见源和目标地址选择。Type
0x20
to prefer IPv4 over IPv6 by modifying entries in the prefix policy table.键入
0x20
以通过修改前缀策略表中的条目来使IPv40x20
于IPv6。Type
0x10
to disable IPv6 on all nontunnel interfaces (on both LAN and Point-to-Point Protocol [PPP] interfaces).键入
0x10
以禁用所有非隧道接口上的IPv6(在LAN和点对点协议[PPP]接口上)。Type
0x01
to disable IPv6 on all tunnel interfaces. These include Intra-Site Automatic Tunnel Addressing Protocol (ISATAP), 6to4, and Teredo.键入
0x01
以在所有隧道接口上禁用IPv6。 这些包括站点内自动隧道寻址协议(ISATAP),6to4和Teredo。Type
0x11
to disable all IPv6 interfaces except for the IPv6 loopback interface.键入
0x11
以禁用除IPv6回送接口之外的所有IPv6接口。- Restart the computer for this setting to take effect. 重新启动计算机,此设置才能生效。
What is this prefix policy table?
此前缀策略表是什么?
netsh interface ipv6 show prefixpolicies
(orprefixpolicy
on earlier versions)
netsh interface ipv6 show prefixpolicies
(或早期版本上的prefixpolicy
)Precedence Label Prefix ---------- ----- -------------------------------- 50 0 ::1/128 45 13 fc00::/7 40 1 ::/0 10 4 ::ffff:0:0/96 7 14 2002::/16 5 5 2001::/32 1 11 fec0::/10 1 12 3ffe::/16 1 10 ::/96
This table decides what prefixes get precedence over other prefixes during DNS resolves.
该表确定在DNS解析期间哪些前缀优先于其他前缀。
Ah, so using that KB we could add entries here that denote that IPv4 has higher precedence than IPv6.
嗯,因此使用该KB我们可以在此处添加条目,以表示IPv4的优先级高于IPv6。
Note: There is no reason to override this behavior, unless you are experiencing compatibly problems. Changing this setting on our Windows Server broke our mail server, so it should be handled with care…
注意:除非遇到兼容问题,否则没有任何理由要覆盖此行为。 在Windows Server上更改此设置会破坏我们的邮件服务器,因此应谨慎处理…
There’s nothing we like more than a thorough and informative answer with linked support documents to spare. Clearly the localhost and local IP address are distinct entities, serve different purposes, and now we all know why.
除了提供链接的支持文档之外,我们提供的详尽而翔实的答案是我们所需要的。 显然,本地主机和本地IP地址是不同的实体,具有不同的用途,现在我们都知道为什么。
Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.
有什么补充说明吗? 在评论中听起来不对。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程 。
翻译自: https://www.howtogeek.com/140943/whats-the-difference-between-the-locahost-and-local-ip-address/