1. What is the hosts file? This file is in what?
hosts file (DNS file) is to map host names to a plain text file IP address, the original name is HOSTS.TXT (IP, Internet Protocol, Internet Protocol)
As a plain text file, you can choose to use the system comes with Notepad or Wordpad to open the file. hosts is a file without an extension of the system, the basic role is to establish an association "database" Some of the popular web site domain names and corresponding IP addresses. When a user enters a URL in a browser to log in, the system will first automatically find the corresponding IP address from the hosts file, once found, the system will open the corresponding page immediately, if not found, the system will submit URLs DNS DNS server IP address resolution.
File Location:
If you are using windows system, then, is this normal position:
Windows XP/2000/Vista/7/8/8.1/10: C:\windows\system32\drivers\etc\
Under other operating systems here:
Linux and other Unix-like operating systems: / etc
Android:/system/etc/
IPhone OS:/etc/
ails/82846903
Role 2 hosts file
2.1 speed up DNS
For frequently visited website, you can configure the mapping between domain names and IP in the hosts file, increase the speed to resolve domain names. Thanks to the mapping relationship, when we enter the domain name, the computer will be able to quickly resolve the IP address instead of requesting DNS server on the network.
Normally, an IP mapping a domain name may be an IP corresponding to a plurality of domain names.
In use them, filling in IP domain reloading for example: 127.0.0.1 www.baidu.com
How to get the domain name it? Under normal circumstances, we browse a Web page, the browser address bar will show his domain. (Note that when you fill in the domain name in the hosts file, the domain name of the protocol (http / https) are not required to write up)
Similarly, we can adopt the following two simple ways to get IP
1. Using the ping command to obtain IP way
Note: If your home computer can not ping the site, you can skip Method 2
We can get the IP address of the domain name by the ping command:
After opening the cmd, we can get a DOS command window, type above us:
ping www.baidu.com -n 1
Then press Enter (note that if you copy and paste the domain name, then right-click and paste, you can not use ctrl + V to paste)
2. Use the website IP
Open website http://ip.chinaz.com, enter the domain name after the IP address of the Web site can query (of course, you can also use similar sites, such as ip138, ipip etc.)
Use the hosts file to map domain names to IP is actually the domain name redirection (Redirect), so that the domain name to jump to the specified IP address.
It is worth mentioning that, if something in the hosts file record too much, there may only slow down the speed of the Internet, because the computer in front of a name, will first traverse the hosts file, the corresponding record is not found, again DNS ask
2.2 mapping relationship building
In many units, they will have their own local area network, but also have different servers available to members of the company's use. However, due to the local area network rarely set up DNS servers, so access to these servers, you need to enter the IP address difficult to remember, this is pretty troubling for everyone. Therefore, we were taken to these servers can be an easy to remember name, and then establish IP mapping in the hosts file, so that when later visit, just type the name of the server on OK!
2.3 shield spam
There are many sites without our consent at the time, it will be a wide variety of plug-in installed to our computers, many of them viruses and Trojans. For these sites, you can use the hosts file to the site's domain name is mapped to a wrong IP address or IP local computer, so that you can achieve the purpose of prohibiting access!
The principle is carried out by mapping a domain name and can not browse the web IP address, when the user visits the site, appeared DNS error, so that the user can not access the page, this method can be used to block web pages advertising (but today's general advertising by professional advertising companies such as doubleclick to traffic, you can not modify the hosts file to replace professional advertising software, but can play a supporting role)
要想使得域名解析错误,通常将域名和127.0.0.1或者是0.0.0.0这两个IP地址进行映射.(127.0.0.1是回送地址,指本地机,别名是lolocalhost,0.0.0.0是一个不可路由的元地址)
举个例子,如果在hsots文件上写127.0.0.1 www.baidu.com,那就会无法访问www.baidu.com
如果想体验下手工屏蔽广告的话,可以使用到wireshark等捉包软件进行捉包来得到广告商的域名。然后再通过修改hosts文件来进行屏蔽。
在这里,我收集了一些广告商的域名,只要将域名和127.0.0.1或0.0.0.0进行映射即可
以下是我找到的一些投放广告商的域名:
stats.g.doubleclick.net
bx.optimix.asia
bx01.optimix.asia
cm.g.doubleclick.net
cms.tanx.com
当然,因为是映射同一个IP,所以也可以写成
0.0.0.0 stats.g.doubleclick.net bx.optimix.asia …(注意不同域名之间要用空格隔开,IP地址和域名之间也有空格)
注意:
在hosts文件中,“#”后面的任何代码都不会生效,一般使用“#”进行注释
3 修改hosts文件的方法
由于 hosts 文件属性系统文件,因此需要管理员权限才能对其进行修改。
第一种方法:先将权限修改成管理员权限,然后在对其进行修改。
第二种方法:先将hosts文件复制到桌面,这时就不需要管理员权限了,因此可以对其进行修改了,等修改之后,在将其拖回原目录,替换就可以啦!
在 iOS 系统中中,hosts文件的位置为:~/private/etc
在 Windows 系统中,hosts文件的位置为:C:\Windows\System32\drivers\etc
总结:
hosts文件是什么:将主机名映射到IP地址的一个纯文本文件
hosts文件可以用任何打开纯文本的应用程序来打开,如记事本
位置:
Windows XP/2000/Vista/7/8/8.1/10: C:\windows\system32\drivers\etc\
其他的操作系统下的位置:
Linux及其他类Unix操作系统:/etc
Android:/system/etc/
IPhone OS:/etc/
作用:
1.加快域名解析,当用户浏览特定的域名时,不需要向DNS查询,直接将域名解析为IP从而达到加速解析的效果
2.通过不相应的IP与域名映射使得域名解析错误,从而无法访问网站
注意:
在该文件中,“#”后面的任何代码都不会生效,一般使用“#”进行注释
加速解析的用法:
1.ip 域名 如 183.232.231.173 www.baidu.com
2.ip 域名 域名 …
屏蔽网站的用法: 0.0.0.0 域名 或 127.0.0.0 域名