为什么ping“ localhost”和ping“本地IP地址”之间有区别?

迪奥戈

在Windows上使用cmdping提供了以下结果:

  • ping“本地主机”:

在此处输入图片说明

  • ping“ 192.168.0.10”(本地IP地址):

在此处输入图片说明

两种情况不是完全一样吗?

我的意思是,我要对相同的接口,相同的计算机和相同的地址执行ping操作。为什么会得到如此不同的结果?

编辑:这是我的ipconfig /all屏幕:

在此处输入图片说明

塔玛拉·维斯曼(Tamara Wijsman)

您没有对同一接口执行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.

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

Thus 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:

  1. In Registry Editor, locate and then click the following registry subkey:

     HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters
    
  2. 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:

    1. In the Edit menu, point to New, and then click DWORD (32-bit) Value.

    2. Type DisabledComponents, and then press ENTER.

    3. Double-click DisabledComponents.

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

    • Type 0 to enable all IPv6 components. (Windows default setting)
    • 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.
    • Type 0x20 to prefer IPv4 over IPv6 by modifying entries in the prefix policy table.
    • Type 0x10 to disable IPv6 on all nontunnel interfaces (on both LAN and Point-to-Point Protocol [PPP] interfaces).
    • 键入0x01以在所有隧道接口上禁用IPv6。其中包括站点内自动隧道寻址协议(ISATAP),6to4和Teredo。
    • 键入0x11以禁用除IPv6环回接口以外的所有IPv6接口。
  4. 重新启动计算机,此设置才能生效。

此前缀策略表是什么?

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

该表确定在DNS解析期间哪些前缀优先于其他前缀。

嗯,因此使用该KB我们可以在此处添加条目,这些条目表示IPv4的优先级高于IPv6。

注意:除非遇到兼容问题,否则没有任何理由要覆盖此行为。在Windows Server上更改此设置会破坏我们的邮件服务器,因此应谨慎处理...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么ping IP与ping网站不同?

来自分类Dev

ping localhost和ping 127.0.0.1有什么区别?

来自分类Dev

ping localhost和ping 127.0.0.1有什么区别?

来自分类Dev

为什么Ping和Dig提供的IP地址与nslookup不同?

来自分类Dev

为什么ping这么慢?

来自分类Dev

为什么可以ping IP地址而不是“ traceroute”呢?

来自分类Dev

为什么域的ping显示错误的IP

来自分类Dev

为什么我不能ping通Mac上的本地IP?

来自分类Dev

为什么ping终止后会有ping输出?

来自分类Dev

为什么主机名和ping报告的网络地址之间存在差异?

来自分类Dev

ping -w和ping -W有什么区别?

来自分类Dev

ping -w和ping -W有什么区别?

来自分类Dev

为什么ping成功但nmap失败?

来自分类Dev

为什么“ ping -j”不起作用?

来自分类Dev

我为什么不能ping herokuapp

来自分类Dev

为什么ping需要setuid位?

来自分类Dev

为什么ping -f会这样?

来自分类Dev

公司为什么要阻止“ ping”?

来自分类Dev

为什么ping需要setuid权限?

来自分类Dev

iptables为什么不拒绝ping?

来自分类Dev

ping ping localhost是什么?

来自分类Dev

为什么iptables规则会影响ping localhost?

来自分类Dev

为什么我无法从本地PC ping Azure VM

来自分类Dev

为什么ping程序中的内核未删除IP层

来自分类Dev

为什么ping程序中的内核未删除IP层

来自分类Dev

为什么“ping undefinedhost”和“curl undefinedhost”(例如libc“gethostbyname”)ping/连接到本地主机而不是失败?

来自分类Dev

为什么mount,ping和su设置了粘性位?

来自分类Dev

为什么通过VPN ping的速度比没有VPN更快?

来自分类Dev

为什么通过主机名执行ping操作比通过IP地址花费更多时间?

Related 相关文章

热门标签

归档