Read the hosts file in one article

What are hosts?

Hosts-Baidu Encyclopedia

HostsIt is one 没有扩展名的系统文件, which can be 记事本opened with other tools, which 作用is to create a "database" 网址域名corresponding to some commonly used onesIP地址关联

When the user enters a web address that needs to be logged in in the browser, the system will first automatically find the corresponding IP address from the Hosts file. Once found, the system will immediately open the corresponding webpage. If it is not found, the system will submit the web address again DNS域名解析服务器. Resolution of IP addresses.

It should be noted that the mapping of 静态the Hosts file configuration is yes , if the computer on the network changes, please update the IP address in time, otherwise it will not be accessible.
Insert picture description here

Generally speaking, when the user enters in the computer browser 入网址(域名), the system needs to query the network DNS 服务器, 将域名 (比如chromecj.com) 解析成对应的 IP 地址and the browser IP 地址can find 服务器and visit the website after passing it .

But 操作系统的 hosts 文件it has “类似本地 DNS 服务器”a role, and it has more than DNS on the Internet 更高的优先级.

If you configure the hosts inside 某个域名and IPthe 对应关系(such as 127.0.0.1 iplaysoft.com), then when you visit iplaysoft.com in the "machine", the system will first resolve the 127.0.0.1 from hosts inside the IP, and 不会再去query network On the DNS server.

Note: The hosts file 仅对本机生效does not cause any impact on other computers and the website itself.

What is the effect of modifying hosts?

According to the above characteristics, the hosts file can be used in many scenarios, such as the following examples:

  • 强制指定域名的 IP, 加快域名解析(The step of querying DNS on the Internet is omitted), and it can also bypass DNS pollution and hijacking.
  • Configure a "web address alias" for certain IP machines in the local area network to facilitate your own memory and access. For example, configure a nas.com to access the NAS in the local area network; company to access the company website and so on.
  • Point the domain name to it to 不可访问的IP地址achieve 屏蔽the effect of unhealthy websites and blocking spam URLs; the same can be done 禁止系统、软件、网站访问某些指定的网址;
  • When developing or testing applications hosts 将域名临时指向到测试服务器IP, you can use it to facilitate your own testing without affecting others and online applications.

There are many different uses for hosts, but the more common ones are mainly used 屏蔽指定网址by programmers in the development and testing phases 配置服务器IP.

How to modify hosts?

Different operating systems have different ways to modify the hosts file, and the path where it is located is also different. The location path of the hosts file of a common operating system is:

WindowsHosts file system path: C:\Windows\System32\drivers\etc\hosts
Mac 系统hosts file path: /etc/hosts
Linux 系统The hosts file is in general: /etc/hosts
Android 系统the hosts file path: /system/etc/hosts (需要Root权限修改)
Because hostspart of the file system, it is necessary 管理员权限to modify the content. Generally, any text editor can be used to open it to modify the content,

一行一个IP地址和域名的对应关系, The format is 「IP地址+空格+域名」that the "domain name" here does not necessarily require xx.com, it can also be, for 一个单词example, the system preset "localhost" or a custom "nas". You can add or modify the content of hosts according to your needs.

Examples of the contents of the hosts file are as follows:

127.0.0.1 example.com
14.215.177.38 www.baidu.com
192.168.1.100 nas
# localhost name resolution is handled within DNS itself.
 
# ::1 localhost

# 127.0.0.1 edmp.local
# 47.244.110.154 bjb.yntcib.com

127.0.0.1	activate.navicat.com

127.0.0.1 localhost dev.edm3.edm.zaobang.com
192.168.0.102 windows10.microdone.cn

# 井号开头的则是注释

You can modify it according to your own needs. If you only modify it once or twice, you can modify it manually. But for friends who often need to modify hosts (such as developers), it is very tiring and error-prone to manually modify it back and forth every time. Then you need 一键快速切换 hoststhe tools!

How to quickly modify, apply/switch Hosts?

There are many gadgets related to Hosts, and the one that everyone needs the most is definitely hosts 切换工具. For example Host Switch Plus: fast switching Domain-IPand SwitchHosts.

These gadgets have not been used yet, and the Hosts file is only changed once for a long time.
Insert picture description here

What should I do if the modified hosts does not take effect?

Whether it is manually modifying hosts or using software to modify, many people will encounter 修改 hosts 后不生效situations. In fact, this is generally due to DNS 缓存the causes.

Because the system in order 加快用户打开网站的速度, in 首次after visiting the site successful, will DNS 解析的结果temporarily stored in the local cache (called DNS cache) 里一小段时间,如果浏览器在“这段时间里”再次打开同一个网址,则会自动从DNS cache 里取出结果,而不会请求远程的 DNS 服务器,will not query the hosts file `to save time and improve the speed of opening.

Therefore, users will encounter situations where sometimes the hosts file is modified but does not take effect. Generally, we can find a way at this time 清空 (刷新) 一下系统的 DNS 缓存.

How to clear the DNS cache?

In Windowsline execute the command:ipconfig /flushdns
Insert picture description here

In macOScase execute the command: sudo killall -HUP mDNSResponder
If you are using Chrome 浏览器, you can go to: chrome://net-internals/#dns, and then click the " Clear host cache" button to clear the browser's DNS cache.
Insert picture description here

If this doesn't take effect, you can only try 重启大法(重启电脑)again, and it's generally OK. If this is still not possible, it may be that your hosts are written incorrectly.

to sum up:

Hosts are not mysterious. Simply understand it 电脑本机的「IP地址<=>域名」的对应表. The system or any software will check whether 优先先检查there is a corresponding configuration in the hosts, and then it will check the Internet DNS 服务器.

So you can use hosts to do many interesting and useful things. And with SwitchHosts! 工具, you can easily 备份hosts, but also 快速切换different configuration of hosts, or even use a remote hosts file, etc., it is very convenient!


谢谢你阅读到了最后~
期待你关注、收藏、评论、点赞~

Guess you like

Origin blog.csdn.net/weixin_42752574/article/details/109301438