localhost意為本地主機,指這台計算機,是給回路網絡接口的標准主機名,對應的IP地址為127.0.0.1,可訪問本地服務器的web項目(http://localhost)。
那么它們有什么區別呢?
localhost不通過網卡傳輸,不受防火牆和網卡限制;而127.0.0.1則依賴於網卡,會受到防火牆和網卡的限制。
localhost訪問時帶着本機當前用戶的權限;而用IP訪問時,是通過網絡再去訪問主機,涉及到網絡用戶權限。
因為用localhost訪問時不會解析成IP,也就不會占用網絡資源,因此一般情況下用localhost更方便快捷。
無法訪問本地的localhost怎么辦呢?
- 打開http://127.0.0.1/ 看是否能訪問IIS(互聯網信息服務)的默認網頁,能訪問則說明IIS已經成功開啟,否則檢查IIS是否在本機安裝且開啟;
- 開始--命令(cmd)--ping localhost,看返回的數據是否為127.0.0.1,如果是則說明可以解析localhost,否則打開C:\WINDOWS\system32\drivers\etc\host文件看里面有沒有127.0.0.1 localhost,添加好后再去訪問localhost;
- 關閉電腦上的防火牆試試,或檢查電腦上是否有惡意插件;
- 實在不行將IIS卸載掉,重新安裝一次。
localhost指向的IP是127.0.0.1這個關系可以修改嗎?
答案是肯定的,可以在Hosts文件中設置,以下是在兩種操作系統中的設置方法。
- Windows中設置:
兩種方法找到localhost設置:
1、運行 drivers 打開etc文件下hosts 就看到了127.0.0.1 localhost;
2、打開C:\WINDOWS\system32\drivers\etc\host,用記事本打開打開如下:
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 (這里不讓顯示) # source server
# 38.25.63.10 (這里不讓顯示) # x client host
127.0.0.1localhost
最下面的:127.0.0.1 localhost,這兩個參數不可以隨意改。格式是IP空格域名
- 在Linux中設置:
在Linux系統中,系統管理員在為用戶建立新帳號時賦給用戶一個用戶名和一個初始的口令。
另外,Linux系統給計算機賦予一個主機名。
主機名用於在網絡上識別獨立的計算機(即使用戶的計算機沒有聯網,也應該有一個主機名)。TurboLinux系統給出的缺省主機名為:localhost。
在Linux系統成功啟動后,屏幕會提示:localhost login ,意思就是系統要求輸入用戶名,接着要求輸入口令。否則將無法登錄系統,也就無法使用。
