借用百度百科的解釋
Hosts是一個沒有擴展名的系統文件,可以用記事本等工具打開,其作用就是將一些常用的網址域名與其對應的IP地址建立一個關聯“數據庫”,當用戶在瀏覽器中輸入一個需要登錄的網址時,系統會首先自動從Hosts文件中尋找對應的IP地址,一旦找到,系統會立即打開對應網頁,如果沒有找到,則系統會再將網址提交DNS域名解析服務器進行IP地址的解析。
文件位置
C:\Windows\System32\Drivers\etc\hosts
內容
# 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
文件里面已經說得很清晰了,主要的一點,添加ip跟域名的時候至少要有一個空格隔開。
比如添加一個域名,指向本機,個人習慣用tab分隔。
127.0.0.1 sn.cn
測試是否生效,打開命令行,ping
常見問題
1、沒有權限修改hosts文件:http://jingyan.baidu.com/article/ab69b270d963542ca6189f75.html
2、修改之后不生效,可能原因
1). dns緩存沒刷新
2). dnsclient服務影響,原因看windows自己的說明,關了就行了,再不行禁用。也不影響其它功能。比較快的啟動方式:運行services.msc
3). 360等安全軟件把hosts文件鎖定了,在360面板的功能大全找到流量防火牆,修復它,這個問題印象很深刻,不解決的話前面的操作都沒用。