/etc/resolv.conf文件中的search项作用

时间:2022-08-10 23:10:11

resolv.conf文件中有search项时,主机名解析规则顺序: 
DNS配置文件如下:

# vi /etc/resolv.conf
# Generated by NetworkManager
search openstack.local dev.com example.local
nameserver 192.168.122.21

例1:查询主机名,因为主机名后面没有点,就认为是主机名,所以先添加search里的每一项依次组成FQDN(完全合格域名)来查询,完全合格域名查询未找到,就再认为主机名是完全合格域名来查询。

# host -a centos7-bind-1
Trying "centos7-bind-1.openstack.local"
Trying "centos7-bind-1.dev.com"
Trying "centos7-bind-1.example.local"
Trying "centos7-bind-1"
;; connection timed out; no servers could be reached

例2:查询主机名,因为主机名中有点(不是末尾有点),就认为是完全合格域名,先用它来查询,查询失败就把它当成是主机名来进行,添加search里的每一项组成FQDN(完全合格域名)来查询。

# host -a centos7-bind-1.com
Trying "centos7-bind-1.com"
Received 109 bytes from 192.168.122.21#53 in 177 ms
Trying "centos7-bind-1.com.openstack.local"
Trying "centos7-bind-1.com.dev.com"
Trying "centos7-bind-1.com.example.local"
Host centos7-bind-1.com not found: 3(NXDOMAIN)
Received 125 bytes from 192.168.122.21#53 in 55 ms

例3:查询主机名,因为主机名中末尾有点,则认为是完全合格域名,只用它来查询(不会再添加search里的每一项)。查询次数会与search里项域名个数有关。

# host -a centos7-bind-1.
Trying "centos7-bind-1"
;; connection timed out; trying next origin
Trying "centos7-bind-1"
;; connection timed out; trying next origin
Trying "centos7-bind-1"
;; connection timed out; trying next origin
Trying "centos7-bind-1"
;; connection timed out; no servers could be reached

DNS查询工具:host命令介绍 
用法:host [-a] FQDN [server] 
host -l domain [server] 
-a参数:显示查询的所有详细信息。 
-l参数:如果后面的domain允许allow-transfer时,则列出该domain所管理的所有主机数据。 
server参数:这个参数可有可无,当想要利用非 /etc/resolv.conf 内的DNS主机,来查询主机名与 IP 的对应时,就可以利用这个参数了。

host命令使用例(列出该domain所管理的所有主机数据):

# host -l example.local  192.168.122.84
Using domain server:
Name: 192.168.122.84
Address: 192.168.122.84#53
Aliases: example.local name server ns.example.local.
dns.example.local has address 192.168.123.10
ns.example.local has address 192.168.122.84
ns.example.local has IPv6 address ::1
sss.example.local has address 192.168.123.9
www.example.local has address 192.168.123.8