linux域名解析
/etc/hosts 配置一个域名对应多个IP地址
- 通常,hosts文件用于将域名解析为IP地址,但是,hosts文件不支持一个域名对应多个IP地址,这是因为hosts文件是一个简单的静态文本文件,不支持复杂的DNS记录或规则
- 一个域名可以配置多个ip地址,若多个ip地址均有效,则解析到第1个ip地址就返回结果(不再解析下面的ip地址)
#.场景1:假设 10.30.4.44 与 10.30.4.45 均已开机
[root@localhost ~]# cat /etc/hosts
10.30.4.44 xxx.com #.up
10.30.4.45 xxx.com #.up
[root@localhost ~]# ping xxx.com -c 2
64 bytes from xxx.com (10.30.4.44): icmp_seq=1 ttl=127 time=0.233 ms
64 bytes from xxx.com (10.30.4.44): icmp_seq=2 ttl=127 time=0.225 ms
- 一个域名可以配置多个ip地址,若第1个ip地址失效,则域名解析失败(不再解析下面的ip地址)
#.场景2:假设 10.30.4.44 已开机,而 10.30.4.45 已关机
[root@localhost ~]# cat /etc/hosts
10.30.4.45 xxx.com #.down
10.30.4.44 xxx.com #.up
[root@localhost ~]# ping xxx.com -c 2
访问不通
访问不通
- 注:windows亦如此(若第1个ip地址失效,则域名解析失败)
echo 10.30.4.45 xxx.com xxx.com > C:\Windows\system32\drivers\etc\hosts
echo 10.30.4.44 xxx.com xxx.com >> C:\Windows\system32\drivers\etc\hosts
type C:\Windows\system32\drivers\etc\hosts
ping xxx.com
/etc/host.conf
- host.conf 一般很少动,不过也是很重要的配置文件,一般配置如下:
[root@localhost ~]# cat /etc/host.conf
order hosts,bind,nis #.先解析/etc/hosts文件,然后DNS,再是NIS
multi on #.on表示允许主机指定多个IP地址
/etc/resolv.conf 配置多个dns服务器
- 通常 /etc/resolv.conf 用于存储dns服务器的配置信息,系统会使用该文件中的dns服务器地址来解析域名
- 如果同时存在多个DNS服务器,则会按照配置文件中的上下顺序依次尝试连接这些dns服务器,直到成功解析出域名为止(第1个dns解析失败,则会尝试下一个dns),所以建议将常用的DNS服务器放在前面(这也是为什么windows要配置首选dns、备用dns)
search domain1 domain2 用来指定多个域名,当访问的域名不能被DNS解析时,resolver会将该域名加上search指定的参数并重新请求dns,直到被正确解析或试完search指定的域名列表为止
[root@localhost ~]# cat /etc/resolv.conf
nameserver 223.5.5.5
nameserver 114.114.114.114
search baidu.com qq.com
[root@localhost ~]# ping news -c 2
PING news.n.shifen.com (180.101.49.131) 56(84) bytes of data.
64 bytes from 180.101.49.131 (180.101.49.131): icmp_seq=1 ttl=49 time=9.73 ms
64 bytes from 180.101.49.131 (180.101.49.131): icmp_seq=2 ttl=49 time=9.19 ms
domain mydomain.com 用于定义本地域名,在没有设置search的情况下,search默认为domain的值
- search和domain不能共存,如果同时存在,以最后出现的为准
optinons 指定其他的dns解析选项,比如 timeout:[n] 设置等待dns返回的超时时间(默认5秒),而 attempts:[n] 设置向dns发起域名解析的请求次数(默认2次),而 no-check-names 禁止对传入的主机名和邮件地址进行无效字符检查(比如下划线_)
[root@localhost ~]# cat /etc/resolv.conf
nameserver 223.5.5.5
nameserver 114.114.114.114
search baidu.com qq.com
options single-request-reopen
options no-check-names
options attempts:1
options timeout:1
- 重启 network 服务会清理dns缓存,若网卡配置了dns服务器,则每次重启network服务,resolv.conf 都会被网卡dns所覆盖
[root@localhost ~]# cat /etc/resolv.conf
nameserver 223.5.5.5
nameserver 114.114.114.114
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep DNS
DNS1=10.30.3.10
[root@localhost ~]# systemctl restart network
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.30.3.10
模拟多个dns的工作机制
- 场景1:第1个dns地址
223.5.5.5 为阿里DNS,此次解析过程很快(仅1秒),说明第一个dns解析成功就立刻返回结果了
[root@localhost ~]# systemctl restart network
[root@localhost ~]# cat /etc/resolv.conf
nameserver 223.5.5.5
nameserver 1.2.3.4
[root@localhost ~]# time ping www.baidu.com -c 2
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=1 ttl=49 time=8.44 ms
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=2 ttl=49 time=8.57 ms
real 0m1.021s
user 0m0.000s
sys 0m0.002s
- 场景2:第1个dns地址
1.2.3.4 非dns服务器且ping不通,此次解析过程很慢(约15秒),说明尝试第一个dns发现解析失败耗费了很多时间
[root@localhost ~]# systemctl restart network
[root@localhost ~]# cat /etc/resolv.conf
nameserver 1.2.3.4
nameserver 223.5.5.5
#.2次解析返回的ip不一样,说明该网站配置了多个ip的负载均衡
[root@localhost ~]# time ping www.baidu.com -c 2
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=1 ttl=49 time=9.69 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=2 ttl=49 time=9.72 ms
real 0m15.049s
user 0m0.003s
sys 0m0.002s
- 场景3:基于场景2,调整了dns超时时间为10秒(默认5秒),此次解析过程更慢(约25秒),说明尝试第一个dns发现解析失败且调整超时时间,耗费了更多的时间
[root@localhost ~]# systemctl restart network
[root@localhost ~]# cat /etc/resolv.conf
nameserver 1.2.3.4
nameserver 223.5.5.5
options timeout:10
[root@localhost ~]# time ping www.baidu.com -c 2
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=1 ttl=49 time=9.67 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=2 ttl=49 time=9.95 ms
real 0m25.101s
user 0m0.002s
sys 0m0.002s
推荐的公共dns
| 公共dns提供商 |
主DNS |
辅DNS |
ping延迟[实测] |
丢包率[实测] |
备注 |
| 阿里公共DNS |
223.5.5.5 |
223.6.6.6 |
5ms |
0% |
推荐使用 |
| 114 DNS |
114.114.114.114 |
114.114.115.115 |
11ms |
0% |
国内广泛使用 |
| 字节跳动 |
180.184.1.1 |
180.184.2.2 |
7ms |
0% |
国内优秀 |
| 百度 |
180.76.76.76 |
- |
11ms |
0% |
- |
| 腾讯云 |
119.29.29.29 |
182.254.116.116 |
8ms |
0.24% |
- |
| 中国电信 |
101.226.4.6 |
218.30.118.6 |
6ms(测试主机为电信宽带) |
0% |
广告弹窗及网站拦截比较严重 |
| CNNIC |
1.2.4.8 |
210.2.4.8 |
33ms |
48.39% |
中国互联网络信息中心公共DNS服务器 |
| 中华电信 |
168.95.192.1 |
168.95.1.1 |
49ms |
0.12% |
台湾地区比较好用 |
| 谷歌 |
8.8.8.8 |
8.8.4.4 |
134ms |
1.46% |
适用于需要快速解析海外域名的用户 |
- 注:上述ping延迟及丢包率的实测结果,以上海电信的宽带ip、1000次ping采样为例