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.
今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。
超级用户读者Diogo想知道为什么ping命令对localhost和本地IP地址的处理方式不同,而从表面上看,它们似乎是相同的:
Using cmd and ping on Windows gave me the following results:
Pinging “localhost”:
Pinging “192.168.0.10” (local IP address):
Aren’t both situati*** 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?
很明显,两者之间存在某种差异,但当你在两者之间切换时,到底发生了什么?
超级用户贡献者Tom Wij**an对两者之间的细微差别提供了以下见解:
You are not pinging the same interface, without any physical interfaces you still have a “local host”.
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.
You might still wonder why localhost is resolving to ::1, while traditionally we would expect it to resolve to the IPv4 address 127.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).
Looking up localhost using nslookup gives us:
nslookup localhost
... Name: localhost Addresses: ::1 127.0.0.1Thus Windows prefers to use the IPv6 loop back IP address ::1 (see RFC 2373) as it is listed first.
Okay, so, where does it come from, let’s look at the hosts file.
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.
This KB article tells us about a setting that affects what Windows prefers, emphasized in bold:
What is this prefix policy table?
netsh interface ipv6 show prefixpolicies (or prefixpolicy on earlier versi***)
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 ::/96This table decides what prefixes get precedence over other prefixes during DNS resolves.
Ah, so using that KB we could add entries here that denote that IPv4 has higher precedence than 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…
我们最喜欢的就是一个完整的、信息丰富的答案,并提供相关的支持文件。显然,localhost和localip地址是不同的实体,服务于不同的目的,现在我们都知道了原因。
有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。
...中包含称为名称服务器的分支。当请求域名转换时,如果本地DNS名称服务器没有某个域的记录,它将向位于全球的13个根DNS服务器之一发送请求。根DNS服务器然后联系相应的tldns服务器(org、com等)以获取给定域名的缓存记录。...
...是其他的电脑不能作为回报。这怎么可能?这是因为您的本地网络被IPv4标准指定为“专用”。 ...
... 显示本地新闻。 突出显示与读者位置相关的信息(可能是与网站定位相对应的电视节目)。 针对读者提供特定于其位置的服务。 切...
... 专用IP地址(或本地IP地址)标识您自己网络上的设备。一个网络上只有一个设备可以有一个特定的地址,但这个地址也可以在其他专用网络上使用。 ...
... 127.0.0.1在IP地址中是特殊的。通常,IP地址对于本地网络和internet上的每台计算机都是唯一的。但是,127.0.0.1始终指向您当前使用的计算机,无论发生什么情况。 ...
...来达到很好的效果。您可以测试您的计算机是否可以访问本地网络上的其他设备(如路由器),或者是否可以访问Internet上的设备。这可以帮助您确定网络问题是在本地网络上的某个地方,还是在其他地方。数据包返回到您的时...
...设备上找到这些信息。 在本教程中,我们将讨论如何在本地网络上查找设备的IP地址,通常称为专用IP地址。您的本地网络可能使用路由器连接到Internet。该路由器也将有一个公共IP地址一个在公共互联网上标识它的地址。要找...
网络上的每个设备都有一个只有本地网络上的其他设备才能看到的专用IP地址。但是您的ISP为您分配了一个公共IP地址,Internet上的其他设备可以看到该地址。下面是它的工作原理以及如何找到这些IP地址。 IP地址(或Internet协议...
如果你想通过互联网与朋友分享你的本地雷击游戏,这比仅仅按下一个按钮要复杂一些。让我们看看幕后的设置,你必须调整,以连接两个远程雷击机玩家在一起。 为什么要分享你的游戏? Minecraft是一个沙盒游戏,把你的朋...