https://blog.csdn.net/CJF_iceKing/article/details/7702694
hosts文件位於" C:\Windows\System32\drivers\etc "目錄下,用於轉換名字與IP地址的轉換。
在瀏覽器中通過域名訪問網站,首先查看hosts文件中是否存在域名與IP的地址轉換,如果存在則直接根據IP地址進行訪問;否則向DNS服務器發送請求,根據返回結果中的IP進行訪問。
那么hosts文件帶來了哪些用途呢?
1. 提升了網站的訪問速度
如果在hosts中配置了常用的網址和IP的映射關系,就省去了向DNS服務器發送請求獲取IP的過程,從而更快的訪問網站速度。
2. 屏蔽網站
在hosts文件中添加
127.0.0.1 www.baidu.com
則訪問 "www.baidu.com" 實則訪問 "127.0.0.1" 這個本地換回地址,從而達到屏蔽網站的效果。
3. 記錄常用IP地址
通常情況下,通過在瀏覽器中輸入常用網址來判斷是否連上外網,但這種方法只能判斷在DNS服務器地址有效的情況;本人經常使用 "ping 202.102.24.35"這條命令來判斷本機是否連上外網("202.102.24.35"是一個外網地址),針對IP地址比較難記錄,可以在hosts文件中添加"network 202.102.24.35":
# Copyright (c) 1993-2009 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 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 202.102.24.35 network
注意:要以管理員權限打開記事本,然后在記事本中打開host進行修改。
注銷時,以同樣的方式打開,在修改的前面加上# 然后關閉即可。