Linux本地解析文件/etc/hosts说明【原创】

windows的域名本地解析文件hosts是可以一个域名对多个IP,如果有一个IP有问题,可以去解析到其他IP

Linux的本地解析文件/etc/hosts,是否也可以这样呢。下面做了个测试

先看一下#解析器查询顺序配置文件

[root@hch ~]# cat /etc/host.conf
multi on

如果想设置解析顺序,如下内容

#表示先查询本地hosts文件,如果没有结果,再尝试查找BIND dns服务器

order hosts, bind 
multi on

以on或off做为选项。这决定了在/etc/hosts中的一个主机是否可以有几个IP地址,它通常指的是作为“多宿主的”。这个标志对DNSNIS请求是没有作用的。

 

编译hosts文件

[root@hch ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

10.72.16.60 api.weixin.qq.com
10.72.16.62 api.weixin.qq.com
101.226.212.27 api.weixin.qq.com

测试效果

[root@hch ~]# telnet api.weixin.qq.com 80
Trying 10.72.16.60...
telnet: connect to address 10.72.16.60: Connection refused
Trying 10.72.16.62...
telnet: connect to address 10.72.16.62: Connection timed out
Trying 101.226.212.27...
Connected to api.weixin.qq.com.
Escape character is '^]'.
Connection closed by foreign host.

可以看到10.72.16.60、62都无法访问,最后解析到101.226.212.27上。

但是会有个超时时间Connection timed out

结论:可见只要本地解析配置文件开启了multi on,是可以一个域名对多个IP的

配置Linux系统DNS服务器的配置文件

/etc/resolv.conf   #配置Linux系统DNS服务器的配置文件

#表示DNS服务器域名是shanghai.ABC.com,DNS服务器的地址是172.10.10.20

search shanghai.ABC.com

nameserver 172.10.10.20

哈哈,此时我们在本地机器ping sha-node-h,成功返回对应的IP地址172.20.30.1,此外,在应用服务器上ping www.baidu.com 成功返回解析的IP地址。

参考

http://blog.csdn.net/hello_yang213/article/details/49364527

https://zhidao.baidu.com/question/124826853.html

https://blog.xupeng.me/2013/05/15/multiple-ips-with-hosts/

转载请注明出处!

原文地址:https://www.cnblogs.com/paul8339/p/8432221.html