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標準指定為“專用”。 ...
...主機名,隨後會顯示大量資訊,包括主機名、郵政編碼和本地時間。Geotool還使用Google地圖來描述起始IP地址的地理位置,以及顯示經緯度。 ...
... 顯示本地新聞。 突出顯示與讀者位置相關的資訊(可能是與網站定位相對應的電視節目)。 針對讀者提供特定於其位置的服務。 切...
... 專用IP地址(或本地IP地址)標識您自己網路上的裝置。一個網路上只有一個裝置可以有一個特定的地址,但這個地址也可以在其他專用網路上使用。 ...
... 127.0.0.1在IP地址中是特殊的。通常,IP地址對於本地網路和internet上的每臺計算機都是唯一的。但是,127.0.0.1始終指向您當前使用的計算機,無論發生什麼情況。 ...
...來達到很好的效果。您可以測試您的計算機是否可以訪問本地網路上的其他裝置(如路由器),或者是否可以訪問Internet上的裝置。這可以幫助您確定網路問題是在本地網路上的某個地方,還是在其他地方。資料包返回到您的時...
...數路由器都有基於web的配置頁,只要您與路由器在同一個本地網路上,就可以在web瀏覽器中訪問這些頁。要訪問路由器的web介面,首先需要找到路由器的本地IP地址。一般來說,您只需開啟網路連線的設定並查詢“預設閘道器”...