深度解析 DNS服务:从基础原理到自建 DNS 服务器完整指南

作者:coder4_日期:2025/10/22

目录

一、DNS服务基础知识

1、DNS

2、域名

①域名组成

②域名管理与结构

3、DNS服务工作原理

①主机中DNS客户端配置

②DNS服务解析域名流程

4、DNS查询类型

①递归查询

②迭代查询

5、DNS解析类型

6、hosts文件

7、完整的域名查询请求流程

二、DNS检测工具

1、dig命令

2、host命令

3、nslookup命令

4、rndc命令

5、whois命令

三、自建DNS服务

1、ubuntu中的systemd-resolved服务

2、bind安装和配置

3、相关配置说明

①bind配置文件及解析流程

主配置文件

选项配置文件

中间配置文件

allow访问控制指令

acl地址集合

view视图

具体解析规则

Resource Recode定义

②A、AAAA记录

③CNAME别名记录

④MX记录

⑤NS记录

⑥PTR记录

⑦SOA记录

⑧TXT记录

4、实现DNS自解析主机

5、实现DNS自解析的主从

6、实现DNS自解析的反向解析

7、实现DNS缓存

8、实现DNS自解析的子域

9、实现DNS自解析的视图

四、智能DNS解析

1、GLSB全局负载均衡

2、内容分发网络CDN


一、DNS服务基础知识

1、DNS

设计背景:

⚪DNS:Domain Name System (域名系统) 是互联网上解决网络中机器命名的一种系统。在网络中,一台主机去访问另外一台主机时,必须要知道目标主机的IP地址,虽然网络上的节点都可以用IP地址来标识,并且可以通过IP地址被访问。但对于人而言,一组组由数字组成的IP地址,是难以被理解和记忆的。于是设计出了域名系统,用一组有意义的字符来表示某个特定主机或某个网络,再将网络中主机IP地址和域名之间做成一个映射表。

定义及功能:
⚪DNS 就是负责维护主机IP地址和域名映射关系的服务,同时也提供域名解析服务,即人类通过域名访问某个主机或节点时,由DNS服务将人类可读的域名转换为机器可读的IP地址,再通过IP地址找到对应的主机。

⚪在互联网时代,日常的工作和生活,只要涉及到网络,都己经离不开DNS服务了。

2、域名

域名 (Domain Name),又称网域,是一个域的名称。 是一串用点号分隔的字符,可以用来标识网络中某台主机或某个节点,由DNS服务维护域名和主机IP地址之间的映射关系,当我们在网络中访问某个域名时,实际上访问的是该域名对应的IP地址所标识的主机。

1#将域名转换为IP地址进行通信
2
3[root@ubuntu-158 ~]# ping www.baidu.com -c1
4PING www.a.shifen.com (220.181.111.232) 56(84) bytes of data.
564 bytes from 220.181.111.232: icmp_seq=1 ttl=128 time=9.06 ms
6
7--- www.a.shifen.com ping statistics ---
81 packets transmitted, 1 received, 0% packet loss, time 0ms
9rtt min/avg/max/mdev = 9.061/9.061/9.061/0.000 ms
10

FQDN(Fully Qualified Domain Name)全限定域名,域名是一个域的名称,一个网域或一个节点,可以有多台主机,所以为了精确表示域里面的某台主机,在使用域名时,还需要加上主机名,FQDN指的就是同时带有主机名和域名的名称。

例如:www.baidu.com,主机名:www,二级域名:baidu,一级域名:.com,根域:.,通常情况下根域会被省略,DNS系统自动补全,域名:baidu.com。

①域名组成

⚪域名由英文字母,数字和英文连字符(-) 组成,且不区分大小写。

⚪域名是分层次的,允许定义子域,子域名与上级域名之间用 . (点号)分隔,最上层节点的域名称为顶级域名(TLD,Top-Level Domain),第二层节点的域名称为二级域名,依此类推。级别最低的域名写在最左边,而级别最高的域名写在最右边。每一层级的域名长度不能超过63个字符,多级域名加起来不能超过255个字符。域名最多可以有127级。

⚪实际上,在顶级域名之上,还有一个域名叫根域,只不过在使用中一般省略不写。

②域名管理与结构

域名由承担域名系统管理、IP 地址分配等多项职能的非盈利机构 ICANN 管理,其为不同国家或地区设置通常由两个英文字母组成的顶级域名,如.uk 代表英国等、.fr代表法国,中国顶级域名为.cn,且.cn 下域名由 CNNIC 管理 。

⚪根域:全球共有13台IPV4根域名服务器,不负责直接解析负责的委托顶级域名。
⚪一级域名:又称顶级域名,可分为三类,一类代表国家和地区(cn,hk...),一类代表各类组织(com,edu...),以及反向域。
⚪二级域名:某个具体组织,单位,机构,商业公司或个人使用,需要向域名管理机构申请(付费)才能获得使用权。
⚪二级域名以下的域名,由使用该域名的组织自行分配。

13台IPV4根域名服务器,并不是说只有13台服务器,而是指有13个IP地址向外提供一级域名的DNS解析服务,每个IP地址对应的,都是多机集群。

像根服务器这种公共DNS,一般都是使用任播(Anycast)技术来实现的。其原理比较复杂,简单来说,将多台服务器对外广播为同一个IP地址,然后网络上的主机在请求这个公共IP地址时,在路由过程中会被路径上最近的拥有该IP的服务器收到,如此,用户的请求总是会分配给最近的服务器。


为什么只有 13台服务器?

DNS服务工作于C/S模式下,使用UDP协议在客户端和服务端传送数据,为了更好的性能,DNS服务需要将服务端的响应内容限制在一个响应报文内返回给客端,而网络中的设备繁多,不同的设备设定的MTU值(传输控制单元)可能也是不一样的,为了保证在经过不同设备时,DNS的响应报文不会被重组(ipv4协议规定ip层的最小重组缓冲区大小为576),所以在DNS响应报文中,真正能用来传输数据的的空间大小为512字节,这个大小,限制了根服务器的数量和名字。

当我们查询根域(.)的NS记录时,512字节只够返回包含13个由A-M命名的根域名服务器的NS记录和A记录的响应。NS记录在回答区段中,A记录在额外信息区段中,A记录用于帮助你接下来向根域名服务器进一步查询。

实际上DNS同时占据了UDP和TCP的53端口,TCP 的53端口主要用于主从DNS之间的数据同步。


3、DNS服务工作原理

①主机中DNS客户端配置

当一台主机接入互联网时,在配置网络时,通常有两种方式,一种是从DHCP服务自动获得IP地址,另外一种是手动指定静态地址。

从DHCP服务自动获取IP时,通常也会自动获得DNS服务器地址,手动配置静态地址时,也需要手动配置DNS服务器地址。

当用户使用主机 发送/接收 邮件,或浏览网页时,就是由主机上配置的DNS服务器地址负责将域名转换成对应的IP地址。

1#Rocky8-手动配置DNS服务器地址
2[root@rocky8-153 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth1
3TYPE=Ethernet
4NAME=eth1
5IPADDR=10.0.0.163
6PREFIX=24
7DEVICE=eth1
8GATEWAY=10.0.0.2
9DNS1=10.0.0.2                #高可用DNS1故障则DNS2工作
10DNS2=114.114.114.114
11
12#查看DNS
13[root@rocky8-153 ~]#cat /etc/resolv.conf 
14# Generated by NetworkManager
15search localdomain
16nameserver 10.0.0.2         
17nameserver 114.114.114.114
1#ubuntu
2[root@ubuntu-158 netplan]# cat eth0.yaml 
3network:
4  version: 2
5  ethernets:
6    eth0:
7      #dhcp4: true
8      addresses: [10.0.0.158/24]
9      gateway4: 10.0.0.2
10      nameservers:
11        addresses: [223.5.5.5,8.8.8.8]
12
13#查看DNS
14[root@ubuntu-158 netplan]# resolvectl status 
15Global
16         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
17  resolv.conf mode: stub
18
19Link 2 (eth0)
20    Current Scopes: DNS
21         Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
22       DNS Servers: 223.5.5.5 8.8.8.8
23     Default Route: yes
24
25[root@ubuntu-158 netplan]# cat /etc/resolv.conf 
26...
27nameserver 127.0.0.53
28options edns0 trust-ad
29search localdomain

②DNS服务解析域名流程

1、当客户端主机决定访问 https://www.hhaoo.com 这个域名时,首先会查询本机缓存。

2、如果本机缓存没有解析记录,则会向其配置的DNS服务器发起解析请求。

3、DNS代理解析服务器会先查询其缓存是否有这条解析记录,如果有,则直接返回,如果没有,则继续向上解析。

4、DNS代理解析服务器会向其中一个根域名服务器发起解析请求,根域名服务器返回com 域名的DNS地址。(每个根域名服务器管理的域名树结构完全一致,每一个DNS服务都知道根域名在哪系统内置)

5、DNS代理解析服务器继续向 com 域名服务器发起解析请求,com 域名服务器返回 hhaoo.com 域名服务器DNS地址。

6、DNS代理解析服务器继续向 hhaoo.com 域名服务器发起解析请求,hhaoo.com 域名服务器返回 www.hhaoo.com 主机的IP。

7、DNS代理解析服务器将 www.hhaoo.com 的IP地址存入本机缓存,再读取缓存,将 IP地址发送给客户端主机。每一级都会有缓存。

8、客户端主机通过IP地址顺利访问 https://www.hhaoo.com。

注:DNS服务只负责域名解析,也就是说,DNS服务,只负责返回与域名对应的IP地址,但该IP地址在网络上是否是可达的,并不由DNS决定。

4、DNS查询类型

①递归查询

指DNS服务器在收到用户发起的请求时,必须向用户返回一个准确的查询结果。如果DNS服务器本地没有存储与之对应的信息,则该服务器需要询问其他服务器,并将返回的查询结果提交给用户。

一般客户机和本地DNS服务器之间属于递归查询,即当客户机向DNS服务器发出请求后,若DNS服务器本身不能解析,则会向另外的DNS服务器发出查询请求,得到最终的肯定或否定的结果后转交给客户机。此查询的源和目标保持不变,为了查询结果只需要发起一次查询。

递归算法:客户端向LocalDNS发起域名查询-->localDNS不知道域名对应的IP-->但它知道谁知道->他代为帮客户端去查找-->最后再返回最终结果。

②迭代查询

指DNS服务器在收到用户发起的请求时,并不直接回复查询结果,而是告诉另一台DNS服务器的地址,用户再向这台DNS服务器提交请求,这样依次反复,直到返回查询结果。

一般情况下(有例外)本地的DNS服务器向其它DNS服务器的查询属于迭代查询,如:若对方不能返回权威的结果,则它会向下一个DNS服务器(参考前一个DNS服务器返回的结果)再次发起进行查询,直到返回查询的结果为止。此查询的源不变,但查询的目标不断变化,为查询结果一般需要发起多次查询。

迭代算法:客户端向LocalDNS发起域名查询-->localDNS不知道域名对应的IP-->但它知道谁知道并推荐客户端应该找谁-->客户端自己去找它。

③DNS缓存

DNS缓存是将解析数据存储在靠近发起请求的客户端的位置,也可以说DNS数据是可以缓存在任意位置,最终目的是以此减少递归查询过程,可以更快的让用户获得请求结果。

1#Windows系统显示DNS缓存
2C:\Users\HhAosocool>ipconfig/displaydns
3
4Windows IP 配置
5
6    www.baidu.com
7    ----------------------------------------
8    记录名称. . . . . . . : www.baidu.com
9    记录类型. . . . . . . : 5
10    生存时间. . . . . . . : 25
11    数据长度. . . . . . . : 8
12    部分. . . . . . . . . : 答案
13    CNAME 记录  . . . . . : www.a.shifen.com
14
15
16    记录名称. . . . . . . : www.a.shifen.com
17    记录类型. . . . . . . : 1
18    生存时间. . . . . . . : 25
19    数据长度. . . . . . . : 4
20    部分. . . . . . . . . : 答案
21    A (主机)记录  . . . . : 220.181.111.1
22    ...
23
24#Windows系统清除DNS缓存
25C:\Users\HhAosocool>ipconfig/flushdns
26
27Windows IP 配置
28
29已成功刷新 DNS 解析缓存。
1#Centos系统查看DNS缓存
2[root@rocky8-153 ~]#nscd -g
3
4#Centos系统清空DNS缓存
5[root@rocky8-153 ~]#nscd -g hosts
6
7#Ubuntu系统查看DNS缓存
8[root@ubuntu2204-150 ~]# resolvectl statistics
9
10#Ubuntu系统清空DNS缓存
11[root@ubuntu2204-150 ~]# resolvectl reset-statistics

5、DNS解析类型

DNS服务器里面有两个区域,即 正向查找区域 和 反向查找区域。

正向解析:将域名解析成IP地址。
反向解析:根据IP地址得到该IP地址指向的域名。

例如:www.hhaoo.com——解析——5.5.5.5 ,正向解析,该配置并不能提供反写解析,并不能互相通用。

6、hosts文件


hosts文件是引入DNS服务之前的解析方案

早期的 internet 网络规模非常小,一个hosts文件就可以定义所有的域名与IP地址的映射关系,管理机构提供该文件下载地址,并定期更新文件内容,以此来反映网络上的变化。但随着时间的推移,接入 internet 的主机和域名越来越多,通过一个中心文件管理所有域名与IP地址映射关系己经不再可取,于是便引入了DNS服务。


hosts文件是一个没有扩展名的系统文件,可以直接用文本编辑工具修改,在该文件中直接定义域名与IP地址的对应关系,当主机访问某个域名时,会先从hosts文件中寻找与该域名对应的IP地址,如果找到,则直接请求该IP地址,如果找不到,才会将该域名提交DNS服务请求解析该域名对应的IP地址。

注:文件只负责返回与域名对应的IP地址,但该IP地址在网络上是否是可达的,并不由DNS决定。

操作系统规定,在进行DNS请求以前,先检查系统自己的hosts文件中是否有这个域名和IP的映射关系。如果有,则直接访问这个IP地址指定的网络位置,如果没有,再向已知的DNS服务器提出域名解析请求。也就是说Hosts的IP解析优先级比DNS要高。

1#Windows系统中的hosts文件
2%windir%\System32\drivers\etc\hosts
3
4#%windir% 是windows 系统中的环境变量写法,表示 Windows 安装目录,上述路径一般是
5C:\Windows\System32\drivers\etc
6
1#Linux系统中的hosts文件
2[root@rocky8-153 ~]#cat /etc/hosts
3127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
4::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
5
6[root@ubuntu2204-150 ~]# cat /etc/hosts
7127.0.0.1 localhost
8127.0.1.1 ubuntu
9
10# The following lines are desirable for IPv6 capable hosts
11::1     ip6-localhost ip6-loopback
12fe00::0 ip6-localnet
13ff00::0 ip6-mcastprefix
14ff02::1 ip6-allnodes
15ff02::2 ip6-allrouters
16
17[root@ubuntu2204-150 ~]# ping ubuntu
18PING ubuntu (127.0.1.1) 56(84) bytes of data.
1964 bytes from ubuntu (127.0.1.1): icmp_seq=1 ttl=64 time=0.971 ms
2064 bytes from ubuntu (127.0.1.1): icmp_seq=2 ttl=64 time=0.036 ms
21^C
22--- ubuntu ping statistics ---
232 packets transmitted, 2 received, 0% packet loss, time 1002ms
24rtt min/avg/max/mdev = 0.036/0.503/0.971/0.467 ms
25
26
1#直接在hosts中指定域名的IP地址
2[root@ubuntu2204-150 ~]# ping www.baidu.com 
3PING www.a.shifen.com (220.181.111.1) 56(84) bytes of data.
464 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=1 ttl=128 time=22.6 ms
5^C
6--- www.a.shifen.com ping statistics ---
71 packets transmitted, 1 received, 0% packet loss, time 0ms
8rtt min/avg/max/mdev = 22.552/22.552/22.552/0.000 ms
9
10[root@ubuntu2204-150 ~]# vim /etc/hosts
115.5.5.5 www.baidu.com    #添加
12
13
14[root@ubuntu2204-150 ~]# ping www.baidu.com 
15PING www.baidu.com (5.5.5.5) 56(84) bytes of data.
16^C
17--- www.baidu.com ping statistics ---
181 packets transmitted, 0 received, 100% packet loss, time 0ms
19
20

7、完整的域名查询请求流程

1、主机请求一个域名时,先查询本地 hosts 文件,是否有对应IP地址,如果有,则直接访问该IP地址,域名解析服务结束。

2、如果本地 hosts 文件中没有对应IP地址,则查询本地DNS缓存,如果本地DNS缓存中有对应IP地址,则直接访问该IP地址,域名解析服务结束。

3、如果没有本地DNS缓存,或本地DNS缓存中没有该域名的IP地址,则请求主机配置的DNS服务器来解析该域名。

4、DNS服务器经过迭代查询,将查询结果返回给主机,主机根据返回结果访问对应IP地址。

二、DNS检测工具

1、dig命令

dig(Domain Information Groper)域名信息查询工具,dig 命令不会查询本地 hosts文件中定义的域名和IP对应关系。

1#安装
2[root@ubuntu2204-150 ~]# apt install bind9
3
4#查询来源
5[root@ubuntu2204-150 ~]# dpkg -S /usr/bin/dig
6bind9-dnsutils: /usr/bin/dig
7
8#命令格式            
9dig [@global-server] [domain] [q-type] [q-class] {q-opt}
10           {global-d-opt} host [@local-server] {local-d-opt}
11           [ host [@local-server] {local-d-opt} [...]]      
12            
13dig [-t type] name [@SERVER] [query options]            
14            
15@global-server     #指定DNS服务器
16domain             #要查询的域名
17q-type             #要查询的记录类型(a,any,mx,ns,soa,hinfo,axfr,txt,...),默认a
18q-class            #要查询的解析类型(in|hs|ch),默认 in
19q-opt              #选项
20d-opt              #查询选项
21
22#q-opt 项
23-h                 #显示帮助
24-v                 #显示版本号
25-4                 #仅查询IPV4的DNS服务器
26-6                 #仅查询IPV6的DNS服务器
27-b address[#port]  #使用指定客户端IP去查询DNS
28-c class           (specify query class)
29-f filename        #从文件中获取要查询的域名
30-i                 (use IP6.INT for IPv6 reverse lookups)
31-k keyfile         (specify tsig key file)
32-m                 (enable memory usage debugging)
33-p port            #指定DNS服务查询端口
34-q name            (specify query name)
35-r                 (do not read ~/.digrc)
36-t type            #指定要查询的资源记录类型A|NS|AAA|PTR|...
37-u                 #以微秒显示打印时间
38-x dot-notation    #反向解析
39-y [hmac:]name:key (specify named base64 tsig key)
40
41#d-opt 项
42+[no]trace         #是否追踪查询过程
43+[no]cmd           #是否在查询结果中显示头信息 
44+[no]recurse       #是否进行递归解析查询
45+[no]all           #是否显示所有信息,如果否,要指明具体显示内容
46+[no]answer        #是否显示answer部份
47+[no]question      #是否显示question部份
48+[no]authority     #是否显示authority部份
49+[no]comment       #是否显示comment部份
50+[no]stat          #是否显示status部份
51+[no]short         #是否只显示关键信息
52
53#常用组合
54dig domain
55dig @dns-erver domain | dig domain @dns-server
56dig -t q-type domain | dig domain q-type
57git -x IP | dig -t ptr reverseIP.in-addr.arpa #reverseIP 表示将要查询的IP倒序输出
1[root@ubuntu2204-150 ~]# dig
2
3#dig命令版本和参数
4; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>>        #此处查询参数为空
5;; global options: +cmd            #默认选项,此项表示显示头部软件版本和参数信息
6
7#查询结果
8;; Got answer:
9;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34552      #QUERY 表示是执行查询操作,NOERROR 表示解析成功
10;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 16
11
12# opcode: QUERY     执行查询操作
13# status:           NOERROR 没有错误,解析成功
14# id: 34552         此次查询的ID,在dns协议中,通过ID编号匹配查询请求和返回结果
15# flags: qr rd ra   标志位
16#
17# qr    (query,查询标志,代表查询操作) 
18# rd    (recursion desired, 表示客户端希望进行递归查询)
19# ra    (recursive available, 表示DNS服务器支持递归查询)
20# aa    (Authoritative Answer, 权威回复,如果查询结果由管理域名的域名服务器而不是缓存服务器提供的,则称为权威回复)
21#
22#QUERY: 1            查询数,表示1个查询,对应下面 QUESTION SECTION中的记录数
23#ANSWER: 13          查询结果,表示有13个查询结果,对应下面 ANSWER SECTION 中的记录数
24#AUTHORITY: 0        权威域名服务器记录数量,此处表示有0个权威域名服务器
25#ADDITIONAL: 16      额外记录数量,此处表示有16个额外记录,此处缺失该部份内容
26
27#选项
28;; OPT PSEUDOSECTION:
29; EDNS: version: 0, flags:; udp: 65494
30
31#EDNS: Extended DNS 扩展用户数据报文协议
32#version:0 协议版本为 0
33#flag:; 标记位为空
34#udp:65494 数据包大小
35
36#查询域名,此处表示查根域
37;; QUESTION SECTION:
38;.				IN	NS
39
40#具体查询结果
41#第一列是要要询的域名
42#第二列是TTL(time to live),表示该记录的缓存时间,单位是秒
43#第三列是要查询的信息类型,IN代表类别为IP协议,即Internet
44#第四列是要查询的记录类型,NS表示name server,即域名服务器
45#第五列表示查询得到的值
46;; ANSWER SECTION:
47.			5	IN	NS	k.root-servers.net.
48.			5	IN	NS	m.root-servers.net.
49.			5	IN	NS	c.root-servers.net.
50.			5	IN	NS	g.root-servers.net.
51.			5	IN	NS	i.root-servers.net.
52.			5	IN	NS	f.root-servers.net.
53.			5	IN	NS	h.root-servers.net.
54.			5	IN	NS	b.root-servers.net.
55.			5	IN	NS	j.root-servers.net.
56.			5	IN	NS	e.root-servers.net.
57.			5	IN	NS	d.root-servers.net.
58.			5	IN	NS	a.root-servers.net.
59.			5	IN	NS	l.root-servers.net.
60
61;; ADDITIONAL SECTION:
62a.root-servers.net.	5	IN	A	198.41.0.4
63b.root-servers.net.	5	IN	A	170.247.170.2
64c.root-servers.net.	5	IN	A	192.33.4.12
65d.root-servers.net.	5	IN	A	199.7.91.13
66e.root-servers.net.	5	IN	A	192.203.230.10
67f.root-servers.net.	5	IN	A	192.5.5.241
68g.root-servers.net.	5	IN	A	192.112.36.4
69h.root-servers.net.	5	IN	A	198.97.190.53
70i.root-servers.net.	5	IN	A	192.36.148.17
71j.root-servers.net.	5	IN	A	192.58.128.30
72k.root-servers.net.	5	IN	A	193.0.14.129
73l.root-servers.net.	5	IN	A	199.7.83.42
74m.root-servers.net.	5	IN	A	202.12.27.33
75a.root-servers.net.	5	IN	AAAA	2001:503:ba3e::2:30
76b.root-servers.net.	5	IN	AAAA	2801:1b8:10::b
77
78#本次查询统计信息
79;; Query time: 7 msec                            #本次查询消耗时长
80;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)       #DNS服务器为10.0.0.2 端口是53
81;; WHEN: Sat Oct 18 08:28:37 UTC 2025            #查询时间
82;; MSG SIZE  rcvd: 503                           #返回内容长度为503字节

查询DNS解析,使用默认DNS服务器

1[root@ubuntu2204-150 ~]# dig www.baidu.com
2
3; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.baidu.com
4;; global options: +cmd
5;; Got answer:
6;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26413
7;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 10
8
9;; OPT PSEUDOSECTION:
10; EDNS: version: 0, flags:; udp: 65494
11;; QUESTION SECTION:
12;www.baidu.com.			IN	A
13
14;; ANSWER SECTION:
15www.baidu.com.		5	IN	CNAME	www.a.shifen.com.
16www.a.shifen.com.	5	IN	A	220.181.111.232
17www.a.shifen.com.	5	IN	A	220.181.111.1
18
19;; AUTHORITY SECTION:
20a.shifen.com.		5	IN	NS	ns4.a.shifen.com.
21a.shifen.com.		5	IN	NS	ns1.a.shifen.com.
22a.shifen.com.		5	IN	NS	ns5.a.shifen.com.
23a.shifen.com.		5	IN	NS	ns2.a.shifen.com.
24a.shifen.com.		5	IN	NS	ns3.a.shifen.com.
25
26;; ADDITIONAL SECTION:
27ns5.a.shifen.com.	5	IN	A	180.76.76.95
28ns1.a.shifen.com.	5	IN	A	110.242.68.42
29ns2.a.shifen.com.	5	IN	A	220.181.33.32
30ns3.a.shifen.com.	5	IN	A	36.155.132.12
31ns3.a.shifen.com.	5	IN	A	153.3.238.162
32ns4.a.shifen.com.	5	IN	A	14.215.177.229
33ns4.a.shifen.com.	5	IN	A	111.20.4.28
34ns5.a.shifen.com.	5	IN	AAAA	240e:940:603:a:0:ff:b08d:239d
35ns5.a.shifen.com.	5	IN	AAAA	240e:bf:b801:1006:0:ff:b04f:346b
36
37;; Query time: 79 msec
38;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
39;; WHEN: Sat Oct 18 08:55:23 UTC 2025
40;; MSG SIZE  rcvd: 359
41
42#短格式
43[root@ubuntu2204-150 ~]# dig www.baidu.com +short
44www.a.shifen.com.
45220.181.111.232
46220.181.111.1

指定DNS服务器,指定本机请求DNS服务的IP

1[root@ubuntu2204-150 ~]# dig @114.114.114.114 www.jose-404.com -b 10.0.0.150
2
3; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> @114.114.114.114 www.jose-404.com -b 10.0.0.150
4; (1 server found)
5;; global options: +cmd
6;; Got answer:
7;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62841
8;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
9
10;; QUESTION SECTION:
11;www.jose-404.com.		IN	A
12
13;; ANSWER SECTION:
14www.jose-404.com.	600	IN	A	47.94.245.255
15
16;; Query time: 263 msec
17;; SERVER: 114.114.114.114#53(114.114.114.114) (UDP)
18;; WHEN: Sat Oct 18 09:08:09 UTC 2025
19;; MSG SIZE  rcvd: 50

反向解析

1[root@ubuntu2204-150 ~]# dig -x 47.94.245.255 +nocmd
2;; Got answer:
3;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 33985
4;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
5
6;; OPT PSEUDOSECTION:
7; EDNS: version: 0, flags:; udp: 65494
8;; QUESTION SECTION:
9;255.245.94.47.in-addr.arpa.	IN	PTR
10
11;; AUTHORITY SECTION:
1294.47.in-addr.arpa.	5	IN	SOA	rdns1.alidns.com. dnsmgr.alibaba-inc.com. 2015011340 1800 600 1814400 300
13
14;; Query time: 35 msec
15;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
16;; WHEN: Sat Oct 18 09:08:36 UTC 2025
17;; MSG SIZE  rcvd: 126
18
19[root@ubuntu2204-150 ~]# dig -t ptr 255.245.94.47.in-addr.arpa +nocmd
20...

从文件中获取要查询的域名

1[root@ubuntu2204-150 1018]# vim domain.txt
2[root@ubuntu2204-150 1018]# cat domain.txt
3www.baidu.com
4[root@ubuntu2204-150 1018]# dig -f domain.txt +short
5www.a.shifen.com.
6220.181.111.1
7220.181.111.232
8

只查询别名解析

1[root@ubuntu2204-150 1018]# dig -t cnmae www.baidu.com
2...
3
4[root@ubuntu2204-150 1018]# dig www.jd.com in cname
5...

2、host命令

host 命令可以根据域名查询得到对应的服务器IP地址,host 命令不会查询本地 hosts文件中定义的域名和IP对应关系。

1host [option] hostname [server]
2
3#常用选项
4-a         #显示所有信息
5-c         #指定查询类型 HS|CH|IN
6-C         #查询SOA
7-d         #同 -v
8-i IP6.INT reverse lookups
9-l lists all hosts in a domain, using AXFR
10-m set memory debugging flag (trace|record|usage)
11-N changes the number of dots allowed before root lookup is done
12-p         #指定端口
13-r         #不递归查询
14-R specifies number of retries for UDP packets
15-s a SERVFAIL response should stop query
16-t         #指定查询类型 CNAME|NS|SOA|TXT|DNSKEY|AXFR|...
17-T         #使用TCP进行DNS查询
18-U         #使用UDP进行DNS查询
19-v         #显示执行过程
20-V         #显示命令版本
21-w         #如果没有查询结果,则阻塞,一直等待
22-W N       #等待N秒后超时
23-4         #仅查询IPV4的DNS server
24-6         #仅查询IPV4的DNS server
1[root@ubuntu2204-150 1018]# host www.baidu.com
2www.baidu.com is an alias for www.a.shifen.com.
3www.a.shifen.com has address 220.181.111.1
4www.a.shifen.com has address 220.181.111.232
5www.a.shifen.com has IPv6 address 2408:871a:2100:1b23:0:ff:b07a:7ebc
6www.a.shifen.com has IPv6 address 2408:871a:2100:186c:0:ff:b07e:3fbc

指定DNS服务器

1[root@ubuntu2204-150 1018]# host www.baidu.com 114.114.114.114
2Using domain server:
3Name: 114.114.114.114
4Address: 114.114.114.114#53
5Aliases: 
6
7www.baidu.com is an alias for www.a.shifen.com.
8www.a.shifen.com has address 220.181.111.1
9www.a.shifen.com has address 220.181.111.232
10www.a.shifen.com has IPv6 address 240e:83:205:381:0:ff:b00f:96a2
11www.a.shifen.com has IPv6 address 240e:83:205:1cd:0:ff:b0b8:dee9
12

显示所有信息

1[root@ubuntu2204-150 1018]# host -a www.baidu.com 114.114.114.114
2Trying "www.baidu.com"
3Using domain server:
4Name: 114.114.114.114
5Address: 114.114.114.114#53
6Aliases: 
7
8;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24726
9;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0
10
11;; QUESTION SECTION:
12;www.baidu.com.			IN	ANY
13
14;; ANSWER SECTION:
15www.baidu.com.		658	IN	CNAME	www.a.shifen.com.
16www.a.shifen.com.	88	IN	AAAA	240e:83:205:1cd:0:ff:b0b8:dee9
17www.a.shifen.com.	88	IN	AAAA	240e:83:205:381:0:ff:b00f:96a2
18www.a.shifen.com.	88	IN	A	220.181.111.232
19www.a.shifen.com.	88	IN	A	220.181.111.1
20
21Received 146 bytes from 114.114.114.114#53 in 159 ms
22

3、nslookup命令

nslookup:(name server lookup),一个命令行下的网络工具,主要用来查询DNS记录,查看域名解析是否正常,也可用来诊断网络问题。
nslookup 支持交互式和非交互式两种执行方式,在Windows系统中和Linux系统中都可以使用。
nslookup 命令不会查询本地 hosts文件中定义的域名和IP对应关系,也不能查询dns的递归或者迭代。

1nslookup [-option] [name | -] [server]
2
3#非交互式选项
4-type              #指定查询类型 A|AAAA|CNAME|...
5-query 
6
7set
8server IP/DNS Name #指定DNS服务器,可以用IP地址或域名
9root
10-a
11-d
12-t type

交互式、非交互式查询

1[root@ubuntu2204-150 1018]# nslookup
2> www.baidu.com
3Server:		127.0.0.53            #本次查询使用的DNS服务器
4Address:	127.0.0.53#53         #DNS服务器IP地址以及监听端口
5
6Non-authoritative answer:
7www.baidu.com	canonical name = www.a.shifen.com.
8Name:	www.a.shifen.com
9Address: 220.181.111.1
10Name:	www.a.shifen.com
11Address: 220.181.111.232
12Name:	www.a.shifen.com
13Address: 240e:83:205:1cd:0:ff:b0b8:dee9
14Name:	www.a.shifen.com
15Address: 240e:83:205:381:0:ff:b00f:96a2
16> 
17
18[root@ubuntu2204-150 1018]# nslookup www.baidu.com
19Server:		127.0.0.53
20Address:	127.0.0.53#53
21
22Non-authoritative answer:
23www.baidu.com	canonical name = www.a.shifen.com.
24Name:	www.a.shifen.com
25Address: 220.181.111.1
26Name:	www.a.shifen.com
27Address: 220.181.111.232
28Name:	www.a.shifen.com
29Address: 2408:871a:2100:186c:0:ff:b07e:3fbc
30Name:	www.a.shifen.com
31Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc
32

Windos系统中使用

1#非交互式
2C:\Users\HhAosocool>nslookup www.baidu.com
3服务器:  UnKnown
4Address:  59.67.168.252
5
6非权威应答:
7名称:    www.a.shifen.com
8Addresses:  2408:871a:2100:1b23:0:ff:b07a:7ebc
9          2408:871a:2100:186c:0:ff:b07e:3fbc
10          220.181.111.1
11          220.181.111.232
12Aliases:  www.baidu.com
13
14#交互式
15C:\Users\HhAosocool>nslookup
16默认服务器:  UnKnown
17Address:  59.67.168.252
18
19> www.baidu.com
20服务器:  UnKnown
21Address:  59.67.168.252
22
23非权威应答:
24名称:    www.a.shifen.com
25Addresses:  2408:871a:2100:186c:0:ff:b07e:3fbc
26          2408:871a:2100:1b23:0:ff:b07a:7ebc
27          220.181.111.232
28          220.181.111.1
29Aliases:  www.baidu.com
30
31>

指定NDS服务器

1[root@ubuntu2204-150 1018]# nslookup www.taobao.com 114.114.114.114
2Server:		114.114.114.114
3Address:	114.114.114.114#53
4
5Non-authoritative answer:
6www.taobao.com	canonical name = www.taobao.com.danuoyi.tbcache.com.
7Name:	www.taobao.com.danuoyi.tbcache.com
8Address: 106.8.159.210
9Name:	www.taobao.com.danuoyi.tbcache.com
10Address: 106.8.159.211
11Name:	www.taobao.com.danuoyi.tbcache.com
12Address: 240e:b1:a830:0:3::26
13Name:	www.taobao.com.danuoyi.tbcache.com
14Address: 240e:b1:a830:0:3::25
15
16[root@ubuntu2204-150 1018]# nslookup 
17> server 223.5.5.5
18Default server: 223.5.5.5
19Address: 223.5.5.5#53
20> www.taobao.com
21Server:		223.5.5.5
22Address:	223.5.5.5#53
23
24Non-authoritative answer:
25www.taobao.com	canonical name = www.taobao.com.danuoyi.tbcache.com.
26Name:	www.taobao.com.danuoyi.tbcache.com
27Address: 106.8.159.210
28Name:	www.taobao.com.danuoyi.tbcache.com
29Address: 106.8.159.211
30Name:	www.taobao.com.danuoyi.tbcache.com
31Address: 240e:b1:a830:0:3::26
32Name:	www.taobao.com.danuoyi.tbcache.com
33Address: 240e:b1:a830:0:3::25
34> 
35

查看默认配置和选项

1[root@ubuntu2204-150 1018]# nslookup
2> set all
3Default server: 127.0.0.53
4Address: 127.0.0.53#53
5
6Set options:
7  novc			nodebug		nod2
8  search		recurse
9  timeout = 0		retry = 3	port = 53	ndots = 1
10  querytype = A       	class = IN
11  srchlist = localdomain
12> 
13

指定查询类型

1[root@ubuntu2204-150 1018]# nslookup -type=cname www.baidu.com
2Server:		127.0.0.53
3Address:	127.0.0.53#53
4
5Non-authoritative answer:
6www.baidu.com	canonical name = www.a.shifen.com.
7
8Authoritative answers can be found from:
9baidu.com	nameserver = ns3.baidu.com.
10baidu.com	nameserver = ns7.baidu.com.
11baidu.com	nameserver = dns.baidu.com.
12baidu.com	nameserver = ns2.baidu.com.
13baidu.com	nameserver = ns4.baidu.com.
14ns7.baidu.com	internet address = 180.76.76.92
15dns.baidu.com	internet address = 110.242.68.134
16ns3.baidu.com	internet address = 153.3.238.93
17ns3.baidu.com	internet address = 36.155.132.78
18ns2.baidu.com	internet address = 220.181.33.31
19ns4.baidu.com	internet address = 111.45.3.226
20ns4.baidu.com	internet address = 14.215.178.80
21ns7.baidu.com	has AAAA address 240e:bf:b801:1002:0:ff:b024:26de
22ns7.baidu.com	has AAAA address 240e:940:603:4:0:ff:b01b:589a
23dns.baidu.com	has AAAA address 240e:bf:b801:1002:0:ff:b024:26de
24ns2.baidu.com	has AAAA address 240e:940:603:4:0:ff:b01b:589a
25

4、rndc命令

rndc 是 bind 程序的客户端工具,默认使用 TCP的 953 端口连接 bind 服务器,用于对 DNS 服务器(named 进程)进行安全管理。

1[root@ubuntu2204-150 1018]# which rndc
2/usr/sbin/rndc
3
4#格式
5rndc [-b address] [-c config] [-s server] [-p port] [-k key-file ] [-y key] [-r] [-V] command
6
7#常用子命令
8status              #查看BIND服务状态
9reload              #重载主配置文件和区域解析库文件
10reload zonename     #重载区域解析库文件
11retransfer zonename #手动启动区域传送,而不管序列号是否增加
12notify zonename     #重新对区域传送发通知
13reconfig            #重载主配置文件
14querylog            #开启或关闭查询日志文件/var/log/message
15trace               #递增debug一个级别
16trace LEVEL         #指定使用的级别
17notrace             #将调试级别设置为 0
18flush               #清空DNS服务器的所有缓存记录

5、whois命令

whois 是一个用于查询域名或 IP 地址注册信息的命令行工具,能够获取域名所有者、注册商、注册日期、过期日期、DNS 服务器等详细信息。

1#安装
2[root@ubuntu ~]# apt install whois -y
3
4#格式
5whois [OPTION]... OBJECT...
6
7#常用选项
8-H                #不显示相关法律声明             
9--help            #显示帮助信息
10--version         #显示版本信息
11
12#下列选项需要 whois.ript.net 和 RIPE-like 服务器支持
13-x                 #精确匹配
14-b                 #输出简略内容
15-a                 #搜索所有数据库镜像
16-s SOURCE[,SOURCE] #从指定数据库中查询
17-q [version|sources|types]     #查询服务器信息
1[root@ubuntu2204-150 1018]# whois baidu.com
2   Domain Name: BAIDU.COM
3   Registry Domain ID: 11181110_DOMAIN_COM-VRSN
4   Registrar WHOIS Server: whois.markmonitor.com
5   Registrar URL: http://markmonitor.com
6   Updated Date: 2025-04-08T00:48:16Z
7   Creation Date: 1999-10-11T11:05:17Z
8   Registry Expiry Date: 2028-10-11T11:05:17Z
9   Registrar: MarkMonitor Information Technology (Shanghai) Co., Ltd.
10   Registrar IANA ID: 3838
11   Registrar Abuse Contact Email: [email protected]
12   Registrar Abuse Contact Phone: +1.2083895740
13   ...

三、自建DNS服务

1、ubuntu中的systemd-resolved服务

在 ubuntu 系统中,虽然在网卡中配置了 DNS 服务器的IP地址,但在使用相关命令进行 DNS 解析时,默认的 DNS 服务器使用的是 127.0.0.53,而并不是我们在网卡上配置的DNS 服务器地址。

1[root@ubuntu-158 netplan]# cat eth0.yaml 
2network:
3  version: 2
4  ethernets:
5    eth0:
6      #dhcp4: true
7      addresses: [10.0.0.158/24]
8      gateway4: 10.0.0.2
9      nameservers:
10        addresses: [223.5.5.5,8.8.8.8]
11
12[root@ubuntu-158 netplan]# resolvectl status 
13Global
14         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
15  resolv.conf mode: stub
16
17Link 2 (eth0)
18    Current Scopes: DNS
19         Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
20Current DNS Server: 223.5.5.5
21       DNS Servers: 223.5.5.5 8.8.8.8
22     Default Route: yes
23
24[root@ubuntu-158 netplan]# nslookup www.baidu.com
25Server:		127.0.0.53
26Address:	127.0.0.53#53
27
28Non-authoritative answer:
29www.baidu.com	canonical name = www.a.shifen.com.
30Name:	www.a.shifen.com
31Address: 220.181.111.1
32Name:	www.a.shifen.com
33Address: 220.181.111.232
34Name:	www.a.shifen.com
35Address: 240e:83:205:381:0:ff:b00f:96a2
36Name:	www.a.shifen.com
37Address: 240e:83:205:1cd:0:ff:b0b8:dee9
38

Ubuntu 默认自带 systemd-resolved 为本地应用程序提供了域名解析服务,系统通过它对外进行 dns 请求,而不是传统意义上的独立 DNS 服务器。

1[root@ubuntu-158 ~]# ll /etc/resolv.conf 
2lrwxrwxrwx 1 root root 39 Apr 16  2025 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
3
4[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
5nameserver 127.0.0.53        #默认DNS配置
6options edns0 trust-ad
7search .
8
9#修改DNS解析配置文件
10[root@ubuntu-158 ~]# vim /etc/resolv.conf 
11[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
12nameserver 10.0.0.2
13options edns0 trust-ad
14search .
15
16#测试
17[root@ubuntu-158 ~]# nslookup www.baidu.com
18Server:		10.0.0.2
19Address:	10.0.0.2#53
20
21Non-authoritative answer:
22www.baidu.com	canonical name = www.a.shifen.com.
23Name:	www.a.shifen.com
24Address: 110.242.69.21
25...
26
27#重启网络服务
28[root@ubuntu-158 ~]# netplan apply
29
30#查看
31[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
32nameserver 127.0.0.53       #文件内容恢复默认
33options edns0 trust-ad
34search .
35

直接修改 /etc/resolv.conf 文件,重启网络服务或系统后,该文件会被还原。这是因为 systemd-resolved 服务会动态管理 DNS 配置,并重新生成 /etc/resolv.conf。

1#-方法1
2#修改软链接文件指向,保证永久生效
3[root@ubuntu-158 ~]# ll /etc/resolv.conf 
4lrwxrwxrwx 1 root root 39 Apr 16  2025 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
5
6[root@ubuntu-158 ~]# rm -rf /etc/resolv.conf 
7
8[root@ubuntu-158 ~]# ls /run/systemd/resolve/
9io.systemd.Resolve  io.systemd.Resolve.Monitor  resolv.conf  stub-resolv.conf
10#resolv.conf:传统DNS配置
11#stub-resolv.conf:systemd-resolved存根配置(动态生成),不可直接编辑由服务管理
12
13[root@ubuntu-158 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
14'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
15
16[root@ubuntu-158 ~]# cat /etc/resolv.conf 
17...
18nameserver 223.5.5.5
19nameserver 8.8.8.8
20search .
21
22[root@ubuntu-158 ~]# nslookup www.baidu.com
23Server:		223.5.5.5
24Address:	223.5.5.5#53
25
26Non-authoritative answer:
27www.baidu.com	canonical name = www.a.shifen.com.
28Name:	www.a.shifen.com
29Address: 220.181.111.232
30...

如果不在网卡配置文件中设置DNS

1#修改网卡配置,注释掉DNS配置
2[root@ubuntu-158 ~]# vim /etc/netplan/eth0.yaml
3[root@ubuntu-158 ~]# cat /etc/netplan/eth0.yaml 
4network:
5  version: 2
6  ethernets:
7    eth0:
8      #dhcp4: true
9      addresses: [10.0.0.158/24]
10      gateway4: 10.0.0.2
11      nameservers:
12        search: [baidu.com]
13        #addresses: [223.5.5.5,8.8.8.8]
14
15#重启网络服务
16[root@ubuntu-158 ~]# netplan apply 
17
18#查看DNS
19...
20# No DNS servers known.
21search baidu.com
22
23#无法使用DNS解析服务
24[root@ubuntu-158 ~]# ping www.baidu.com
25ping: www.baidu.com: Temporary failure in name resolution
26
27[root@ubuntu-158 ~]# nslookup www.baidu.com
28;; communications error to ::1#53: connection refused
29;; communications error to ::1#53: connection refused
30;; communications error to ::1#53: connection refused
31;; communications error to 127.0.0.1#53: connection refused
32;; no servers could be reached

设置全局DNS

默认情况下,systemd-resolved 会让 /etc/resolv.conf 指向 127.0.0.53(本地存根 DNS),所有 DNS 查询先经过 systemd-resolved,再由它转发给上游 DNS(如设置的 223.5.5.5)。

1#此方法可以不用删除软连接
2[root@ubuntu2204-150 netplan]# cat 50-cloud-init.yaml 
3network:
4    ethernets:
5        ens33:
6            dhcp4: true
7    version: 2
8
9[root@ubuntu2204-150 netplan]# cat /etc/resolv.conf 
10...
11nameserver 127.0.0.53
12options edns0 trust-ad
13search localdomain
14
15[root@ubuntu2204-150 ~]# vim /etc/systemd/resolved.conf 
16...
17DNS=223.5.5.5 223.6.6.6
18DNSStubListener=no            #禁用DNS存根
19
20#重启服务
21[root@ubuntu2204-150 ~]# systemctl start systemd-resolved.service
22
23#查看
24[root@ubuntu2204-150 ~]# cat /etc/resolv.conf
25......
26nameserver 223.5.5.5
27nameserver 223.6.6.6
28nameserver 10.0.0.2           #网卡配置中DHCP自动分配
29search localdomain
30
31#测试
32[root@ubuntu2204-150 ~]# nslookup www.baidu.com
33Server:		223.5.5.5
34Address:	223.5.5.5#53
35
36Non-authoritative answer:
37www.baidu.com	canonical name = www.a.shifen.com.
38Name:	www.a.shifen.com
39Address: 220.181.111.232
40...

网卡设备配置注释dhcp手动添加

1[root@ubuntu2204-150 netplan]# cat 50-cloud-init.yaml 
2network:
3    ethernets:
4        ens33:
5          #dhcp4: true
6          addresses: [10.0.0.150/24]
7          gateway4: 114.114.114.114
8          nameservers:
9            addresses: [8.8.8.8]
10    version: 2
11
12#重启网络服务
13[root@ubuntu2204-150 netplan]# netplan  apply
14
15#查看
16[root@ubuntu2204-150 netplan]# cat /etc/resolv.conf 
17...
18nameserver 223.5.5.5
19nameserver 223.6.6.6
20nameserver 8.8.8.8
21search .
22

2、bind安装和配置

bind:(Berkeley Internet Name Domain) ,是一款实现DNS服务的开放源码软件,由伯克利大学开发,能够提供双向解析,转发,子域授权,view 等功能,使用广泛,目前Internet上半数以上的DNS服务器都是由bind来实现的。

named(Name Daemon)是bind核心守护进程,named默认会占用TCP和UDP的53端口, 953是给管理工具使用的。

1[root@ubuntu-157 ~]# ss -tlpn
2State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process                                                  
3LISTEN  0       4096     127.0.0.53%lo:53          0.0.0.0:*      users:(("systemd-resolve",pid=520,fd=15))               
4LISTEN  0       4096           0.0.0.0:22          0.0.0.0:*      users:(("sshd",pid=1526,fd=3),("systemd",pid=1,fd=97))  
5LISTEN  0       4096        127.0.0.54:53          0.0.0.0:*      users:(("systemd-resolve",pid=520,fd=17))               
6LISTEN  0       128          127.0.0.1:6010        0.0.0.0:*      users:(("sshd-session",pid=1640,fd=9))                  
7LISTEN  0       128              [::1]:6010           [::]:*      users:(("sshd-session",pid=1640,fd=8))                  
8LISTEN  0       4096              [::]:22             [::]:*      users:(("sshd",pid=1526,fd=4),("systemd",pid=1,fd=98)) 
9
10#安装
11[root@ubuntu-157 ~]# apt install bind9 -y
12
13#启动服务
14[root@ubuntu-157 ~]# systemctl start named.service 
15
16#查看
17[root@ubuntu-157 ~]# ss -tlpn
18State  Recv-Q  Send-Q                      Local Address:Port   Peer Address:Port Process                                                  
19LISTEN 0       4096                        127.0.0.53%lo:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=15))               
20LISTEN 0       4096                              0.0.0.0:22          0.0.0.0:*     users:(("sshd",pid=1526,fd=3),("systemd",pid=1,fd=253)) 
21LISTEN 0       10                              127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=32))                        
22LISTEN 0       10                              127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=30))                        
23LISTEN 0       4096                           127.0.0.54:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=17))               
24LISTEN 0       5                               127.0.0.1:953         0.0.0.0:*     users:(("named",pid=2231,fd=47))                        
25LISTEN 0       128                             127.0.0.1:6010        0.0.0.0:*     users:(("sshd-session",pid=1640,fd=9))                  
26LISTEN 0       10                             10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=37))                        
27LISTEN 0       10                             10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=36))                        
28LISTEN 0       128                                 [::1]:6010           [::]:*     users:(("sshd-session",pid=1640,fd=8))                  
29LISTEN 0       4096                                 [::]:22             [::]:*     users:(("sshd",pid=1526,fd=4),("systemd",pid=1,fd=254)) 
30LISTEN 0       5                                   [::1]:953            [::]:*     users:(("named",pid=2231,fd=48))                        
31LISTEN 0       10                                  [::1]:53             [::]:*     users:(("named",pid=2231,fd=40))                        
32LISTEN 0       10                                  [::1]:53             [::]:*     users:(("named",pid=2231,fd=41))                        
33LISTEN 0       10       [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=44))                        
34LISTEN 0       10       [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=45))

测试

1#网卡中配置
2[root@ubuntu-157 ~]# cat /etc/netplan/50-cloud-init.yaml 
3network:
4  version: 2
5  ethernets:
6    ens33:
7      #dhcp4: true
8      addresses: [10.0.0.157/24]
9      gateway4: 10.0.0.2
10      nameservers:
11        addresses: [127.0.0.1]
12
13[root@ubuntu-157 ~]# cat /etc/resolv.conf 
14...
15nameserver 127.0.0.53
16options edns0 trust-ad
17search .
18
19[root@ubuntu-157 ~]# rm -rf /etc/resolv.conf 
20[root@ubuntu-157 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
21'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
22[root@ubuntu-157 ~]# ll /etc/resolv.conf 
23lrwxrwxrwx 1 root root 32 Oct 18 20:02 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
24
25#重启网络服务
26[root@ubuntu-157 ~]# netplan apply 
27
28//去掉全局配置,如果有修改则在该文件中注释掉DNS行
29//[root@ubuntu-157 ~]# vim /etc/systemd/resolved.conf 
30//systemctl restart systemd-resolved.service
31
32#查看
33[root@ubuntu-157 ~]# cat /etc/resolv.conf 
34...
35nameserver 127.0.0.1
36search .
37
38[root@ubuntu-157 ~]# ss -unlp
39State  Recv-Q Send-Q                    Local Address:Port   Peer Address:Port Process                                    
40UNCONN 0      0                            10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=35))          
41UNCONN 0      0                            10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=34))          
42UNCONN 0      0                             127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=29))          
43UNCONN 0      0                             127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=28))          
44UNCONN 0      0                            127.0.0.54:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=16)) 
45UNCONN 0      0                         127.0.0.53%lo:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=14)) 
46UNCONN 0      0                             127.0.0.1:323         0.0.0.0:*     users:(("chronyd",pid=991,fd=5))          
47UNCONN 0      0                                 [::1]:53             [::]:*     users:(("named",pid=2231,fd=38))          
48UNCONN 0      0                                 [::1]:53             [::]:*     users:(("named",pid=2231,fd=39))          
49UNCONN 0      0      [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=42))          
50UNCONN 0      0      [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=43))          
51UNCONN 0      0                                 [::1]:323            [::]:*     users:(("chronyd",pid=991,fd=6)) 
52
53#ubuntu测试
54[root@ubuntu-157 ~]# nslookup www.baidu.com
55Server:		127.0.0.1
56Address:	127.0.0.1#53
57
58Non-authoritative answer:
59www.baidu.com	canonical name = www.a.shifen.com.
60Name:	www.a.shifen.com
61Address: 110.242.69.21
62...
63
64[root@ubuntu-157 ~]# dig www.baidu.com @10.0.0.157
65
66; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> www.baidu.com @10.0.0.157
67;; global options: +cmd
68;; Got answer:
69;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4726
70;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
71
72;; OPT PSEUDOSECTION:
73; EDNS: version: 0, flags:; udp: 1232
74; COOKIE: ac883733eda129f40100000068f386e0ca9b14d098b487e3 (good)
75;; QUESTION SECTION:
76;www.baidu.com.			IN	A
77
78;; ANSWER SECTION:
79www.baidu.com.		1200	IN	CNAME	www.a.shifen.com.
80www.a.shifen.com.	120	IN	A	110.242.70.57
81www.a.shifen.com.	120	IN	A	110.242.69.21
82
83;; Query time: 422 msec
84;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
85;; WHEN: Sat Oct 18 20:24:00 CST 2025
86;; MSG SIZE  rcvd: 129
87
88[root@ubuntu-158 ~]# dig www.baidu.com @10.0.0.157
89
90; <<>> DiG 9.20.4-3ubuntu1.2-Ubuntu <<>> www.baidu.com @10.0.0.157
91;; global options: +cmd
92;; Got answer:
93;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18291
94;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
95
96;; OPT PSEUDOSECTION:
97; EDNS: version: 0, flags:; udp: 1232
98; COOKIE: 694b1d7f1aa01bbb0100000068f387263255b8670ea96b6b (good)
99;; QUESTION SECTION:
100;www.baidu.com.			IN	A
101
102;; ANSWER SECTION:
103www.baidu.com.		1130	IN	CNAME	www.a.shifen.com.
104www.a.shifen.com.	50	IN	A	110.242.70.57
105www.a.shifen.com.	50	IN	A	110.242.69.21
106
107;; Query time: 2 msec
108;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
109;; WHEN: Sat Oct 18 20:25:09 CST 2025
110;; MSG SIZE  rcvd: 129
111
112#rocky测试
113#配置DNS解析服务
114[root@rocky-154 ~]#cat /etc/resolv.conf 
115# Generated by NetworkManager
116search localdomain
117nameserver 10.0.0.2
118
119[root@rocky8-153 ~]#vim /etc/resolv.conf 
120nameserver 10.0.0.157
121
122[root@rocky8-153 ~]#nslookup www.baidu.com
123Server:		10.0.0.157
124Address:	10.0.0.157#53
125
126Non-authoritative answer:
127www.baidu.com	canonical name = www.a.shifen.com.
128Name:	www.a.shifen.com
129Address: 110.242.69.21
130...

例如:ubuntu 10.0.0.157主机/etc/resolv.conf文件配置是dns 127.0.0.53:53——systemd-resolved.service服务管理,如果停止服务,127.0.0.53:53没有服务监听,则dig www.baidu.com默认找/etc/resolv.conf文件下配置的dns请求解析则失败,bind9服务监听127.0.0.1:53、10.0.0.157:53端口,要将/etc/resolv.conf文件中的配置切换至127.0.0.1、10.0.0.157,但是文件不受网卡控制,因此需要删除文件重新做软链接处理。对应的我们在其他主机上/etc/resolv.conf文件下配置10.0.0.157同样也可以实现DNS解析。

bind内置13个根域名服务器地址,写在文件中

1[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones 
2// prime the server with knowledge of the root servers
3zone "." {
4	type hint;
5	file "/usr/share/dns/root.hints";
6};
7...
8
9[root@ubuntu-157 ~]# cat /usr/share/dns/root.hints | grep -v '^;'
10.                        3600000      NS    A.ROOT-SERVERS.NET.
11A.ROOT-SERVERS.NET.      3600000      A     198.41.0.4
12A.ROOT-SERVERS.NET.      3600000      AAAA  2001:503:ba3e::2:30
13.                        3600000      NS    B.ROOT-SERVERS.NET.
14B.ROOT-SERVERS.NET.      3600000      A     170.247.170.2
15B.ROOT-SERVERS.NET.      3600000      AAAA  2801:1b8:10::b
16.                        3600000      NS    C.ROOT-SERVERS.NET.
17C.ROOT-SERVERS.NET.      3600000      A     192.33.4.12
18C.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2::c
19.                        3600000      NS    D.ROOT-SERVERS.NET.
20D.ROOT-SERVERS.NET.      3600000      A     199.7.91.13
21D.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2d::d
22.                        3600000      NS    E.ROOT-SERVERS.NET.
23E.ROOT-SERVERS.NET.      3600000      A     192.203.230.10
24E.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:a8::e
25.                        3600000      NS    F.ROOT-SERVERS.NET.
26F.ROOT-SERVERS.NET.      3600000      A     192.5.5.241
27F.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2f::f
28.                        3600000      NS    G.ROOT-SERVERS.NET.
29G.ROOT-SERVERS.NET.      3600000      A     192.112.36.4
30G.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:12::d0d
31.                        3600000      NS    H.ROOT-SERVERS.NET.
32H.ROOT-SERVERS.NET.      3600000      A     198.97.190.53
33H.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:1::53
34.                        3600000      NS    I.ROOT-SERVERS.NET.
35I.ROOT-SERVERS.NET.      3600000      A     192.36.148.17
36I.ROOT-SERVERS.NET.      3600000      AAAA  2001:7fe::53
37.                        3600000      NS    J.ROOT-SERVERS.NET.
38J.ROOT-SERVERS.NET.      3600000      A     192.58.128.30
39J.ROOT-SERVERS.NET.      3600000      AAAA  2001:503:c27::2:30
40.                        3600000      NS    K.ROOT-SERVERS.NET.
41K.ROOT-SERVERS.NET.      3600000      A     193.0.14.129
42K.ROOT-SERVERS.NET.      3600000      AAAA  2001:7fd::1
43.                        3600000      NS    L.ROOT-SERVERS.NET.
44L.ROOT-SERVERS.NET.      3600000      A     199.7.83.42
45L.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:9f::42
46.                        3600000      NS    M.ROOT-SERVERS.NET.
47M.ROOT-SERVERS.NET.      3600000      A     202.12.27.33
48M.ROOT-SERVERS.NET.      3600000      AAAA  2001:dc3::35
49

其他机器DNS指向本机

1[root@rocky-154 ~]#hostname -I
210.0.0.154 
3
4[root@rocky-154 ~]#cat /etc/resolv.conf 
5# Generated by NetworkManager
6search localdomain
7nameserver 10.0.0.157
8
9[root@rocky-154 ~]#dig www.baidu.com
10
11; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.baidu.com
12;; global options: +cmd
13;; Got answer:
14;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21290
15;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
16
17;; OPT PSEUDOSECTION:
18; EDNS: version: 0, flags:; udp: 1232
19; COOKIE: 8d73d3552b84bed60100000068f39a70039beadd5ee9208f (good)
20;; QUESTION SECTION:
21;www.baidu.com.			IN	A
22
23;; ANSWER SECTION:
24www.baidu.com.		1152	IN	CNAME	www.a.shifen.com.
25www.a.shifen.com.	120	IN	A	220.181.111.1
26www.a.shifen.com.	120	IN	A	220.181.111.232
27
28;; Query time: 811 msec
29;; SERVER: 10.0.0.157#53(10.0.0.157)
30;; WHEN: Sat Oct 18 21:47:28 CST 2025
31;; MSG SIZE  rcvd: 129
32

3、相关配置说明

①bind配置文件及解析流程

1[root@ubuntu-157 ~]# dpkg -L bind9
2......
3/etc/bind
4/etc/bind/bind.keys
5/etc/bind/db.0                     #db.* 名具体解析规则文件
6/etc/bind/db.127
7/etc/bind/db.255
8/etc/bind/db.empty
9/etc/bind/db.local
10/etc/bind/named.conf               #主配置文件
11/etc/bind/named.conf.default-zones #中间配置文件,该文件中定义了域名和具体解析规则文件的对应关系
12/etc/bind/named.conf.local         #中间配置文件,引用/etc/bind/zones.rfc1918,被注释
13/etc/bind/named.conf.options       #bind配置项
14/etc/bind/zones.rfc1918            #中间配置文件,该文件中定义了域名和具体解析规则文件的对应关系

主配置文件
1[root@ubuntu-157 ~]# cat /etc/bind/named.conf
2// This is the primary configuration file for the BIND DNS server named.
3//
4// Please read /usr/share/doc/bind9/README.Debian for information on the
5// structure of BIND configuration files in Debian, *BEFORE* you customize
6// this configuration file.
7//
8// If you are just adding zones, please do that in /etc/bind/named.conf.local
9
10include "/etc/bind/named.conf.options";
11include "/etc/bind/named.conf.local";                #不关注
12include "/etc/bind/named.conf.default-zones";
13
14#帮助
15man named.conf
16
17[root@ubuntu-157 ~]# cat /etc/bind/named.conf.local
18//
19// Do any local configuration here
20//
21
22// Consider adding the 1918 zones here, if they are not used in your
23// organization
24//include "/etc/bind/zones.rfc1918";
选项配置文件
1[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options
2options {
3	directory "/var/cache/bind";
4
5	// If there is a firewall between you and nameservers you want
6	// to talk to, you may need to fix the firewall to allow multiple
7	// ports to talk.  See http://www.kb.cert.org/vuls/id/800113
8
9	// If your ISP provided one or more IP addresses for stable 
10	// nameservers, you probably want to use them as forwarders.  
11	// Uncomment the following block, and insert the addresses replacing 
12	// the all-0's placeholder.
13
14	// forwarders {
15	// 	0.0.0.0;
16	// };
17
18	//========================================================================
19	// If BIND logs error messages about the root key being expired,
20	// you will need to update your keys.  See https://www.isc.org/bind-keys
21	//========================================================================
22	dnssec-validation auto;
23
24	listen-on-v6 { any; };
25};
26
1[root@rocky8-153 ~]#cat /etc/named.conf 
2//
3// named.conf
4//
5// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
6// server as a caching only nameserver (as a localhost DNS resolver only).
7//
8// See /usr/share/doc/bind*/sample/ for example named configuration files.
9//
10
11options {
12	listen-on port 53 { 127.0.0.1; };
13	listen-on-v6 port 53 { ::1; };
14	directory 	"/var/named";
15	dump-file 	"/var/named/data/cache_dump.db";
16	statistics-file "/var/named/data/named_stats.txt";
17	memstatistics-file "/var/named/data/named_mem_stats.txt";
18	secroots-file	"/var/named/data/named.secroots";
19	recursing-file	"/var/named/data/named.recursing";
20	allow-query     { localhost; };
21
22	/* 
23	 - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
24	 - If you are building a RECURSIVE (caching) DNS server, you need to enable 
25	   recursion. 
26	 - If your recursive DNS server has a public IP address, you MUST enable access 
27	   control to limit queries to your legitimate users. Failing to do so will
28	   cause your server to become part of large scale DNS amplification 
29	   attacks. Implementing BCP38 within your network would greatly
30	   reduce such attack surface 
31	*/
32	recursion yes;
33
34	dnssec-enable yes;
35	dnssec-validation yes;
36
37	managed-keys-directory "/var/named/dynamic";
38
39	pid-file "/run/named/named.pid";
40	session-keyfile "/run/named/session.key";
41
42	/* https://fedoraproject.org/wiki/Changes/CryptoPolicy */
43	include "/etc/crypto-policies/back-ends/bind.config";
44};
45
46logging {
47        channel default_debug {
48                file "data/named.run";
49                severity dynamic;
50        };
51};
52
53zone "." IN {
54	type hint;
55	file "named.ca";
56};
57
58include "/etc/named.rfc1912.zones";
59include "/etc/named.root.key";
60

该文件要包括以下几部分内容,默认只有全局配置部分

配置配置字段备注
全局配置options{};全局配置选项
日志子系统配置logging{};运行日志
网络自定义集合acl将某个网段或某个具体IP地址定义在一个集合里面
视图view配合acl将不同的请求来源用不同的解析规则返回,实现智能DNS
1#常用全局配置选项
2options {
3
4 #此配置表示DNS服务只监听了本机127.0.0.1的53端口,如果对外提供DNS服务,可以将此行注释或值改成any
5 listen-on port 53 { 127.0.0.1; }; 
6 
7 #监听IPV6的53端口,配置方法同上
8 listen-on-v6 port 53 { ::1; };
9 
10 #监听本机所有IPV6地址,不想监听IPV6地址,可以将 any 改成 none
11 listen-on-v6 { any; };
12 
13 #此配置表示仅本机可以使用DNS服务的解析查询功能,如果对外提供DNS服务,可以将此行注释或值改成any
14 allow-query     { localhost; }; 
15
16 #是否启用加密验证,在使用转发的时候,将此项改为 no
17 dnssec-validation auto;
18
19 #转发服务器
20 forwarders { 10.0.0.207; }; 
21    
22 #转发策略
23 forward first; 
24
25};
中间配置文件
1zone "ZONE_NAME" IN {                     #IN 可以省略不写
2    type {master|slave|hint|forward};     #类型 master,slave 用于DNS主从,forward表示转发
3    file "file_path";                     #具体解析规则文件路径
4};
5
1#该文件中定义了要解析的域名与具体解析规则之间的对应关系
2
3[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones
4// prime the server with knowledge of the root servers
5zone "." {
6	type hint;
7	file "/usr/share/dns/root.hints";
8};
9
10// be authoritative for the localhost forward and reverse zones, and for
11// broadcast zones as per RFC 1912
12
13zone "localhost" {
14	type master;
15	file "/etc/bind/db.local";
16};
17
18zone "127.in-addr.arpa" {
19	type master;
20	file "/etc/bind/db.127";
21};
22
23zone "0.in-addr.arpa" {
24	type master;
25	file "/etc/bind/db.0";
26};
27
28zone "255.in-addr.arpa" {
29	type master;
30	file "/etc/bind/db.255";
31};
32
allow访问控制指令

在named配置中有四个allow开头的字段,主要用来实现访问控制。

字段说明
allow-query{};允许查询本DNS的主机,白名单,注释就代表所有主机都可使用本机当DNS
allow-transfer{};允许区域传送的主机,白名单,注释代表所有,一般用在主从DNS配置时指定从节点
allow-recursion{};允许递归的主机,建议全局使用
allow-update{};允许可以远程更新解析规则的主机
acl地址集合

ACL:将一个或多个网段(或具体IP地址)定义在一个集合里面,并通过统一的名称进行调用。

ACL 只能先定义后调用,因此一般放在配置文件的最上面,在 options 之前定义。

ACL值说明
none排除所有主机
any所有主机
localhost本机
localnet本机IP同子网掩码运算后得到的网段
1#ACL格式
2acl ACL_NAME{
3    IP;
4    IP;
5    NET/NETMAST;
6    NET/NETMAST;
7    ......
8};
9
10#范例
11acl test_net{
12    192.168.0.0/24;
13    172.16.0.0/24;
14};
15acl prod_net{
16    10.0.0.0/24;
17};
18acl other_net{
19    any;
20};
view视图

view:视图,将ACL和具体的解析规则对应起来,实现根据条件解析,实现智能DNS,相同的一个域名,根据客户端来源,可以解析到不同的IP地址。

1、每个view绑定一个ACL。

2、一个bind服务可以定义多个view,每个view 中可定义一个或多个zone。
3、不同的view中可以对同一个域名进行解析,返回不同的解析结果。
4、如果定义了view,则所有的zone规则都要写在view中,不能再直接写在/etc/named.conf 。
5、客户端请求到达时,是自上而下检查每个view所对应的ACL的,如果请求被命中,就进入解析,不再向后匹配。

1#格式 
2view VIEW_NAME{
3    match-clients { acl_name; };
4    zone "domain" IN {
5    type mater;
6    file "domain.zone";
7    };
8    include "/etc/named.rfc1912.zones.domain"
9};
10
11#范例
12view prod_view{
13    match-clients { prod_net; };
14    include "/etc/named.rfc1912.zones";
15    include "/etc/named.rfc1912.zones.prod";
16};
17view other_view{
18    match-clients {other_net;};
19    include "/etc/named.rfc1912.zones";
20};
21
具体解析规则

该文件定义域名的具体解析规则,该文件有多条资源记录组成,每一行都是一条资源记录,在RFC文档中,DNS解析记录被称为Resource Recode(资源记录),缩写为 RR。

1#例子
2[root@ubuntu-157 ~]# cat /etc/bind/db.0
3;
4; BIND reverse data file for "this host on this network" zone
5;
6$TTL	604800
7@	IN	SOA	localhost. root.localhost. (
8			      1		; Serial
9			 604800		; Refresh
10			  86400		; Retry
11			2419200		; Expire
12			 604800 )	; Negative Cache TTL
13;
14@	IN	NS	localhost.
15
Resource Recode定义
1#RR格式
2NAME   TTL   CLASS   TYPE   VALUE
3
4#字段说明
5NAME          #资源记录名称,根据TYPE不一样,写法会有不同
6TTL           #缓存有效期,默认单位是秒,也可以加单位{ M(分), H(时), D(天), W(周) }
7CLASS         #资源记录类别
8TYPE          #记录类型,具体值见下表
9VALUE         #此记录对应的具体值,根据TYPE不一样,写法会有不同
10

CLASS字段说明

说明
IN标准的IPV4/6 Dns记录,常用
CSCSNET,已被废弃
CHCHAOS,一种早期协议,查询DNS服务器元数据(如版本、主机信息等)
HSHesiod,目录服务协议

TYPE字段说明

说明
Ainternet Address,将FQDN解析成IPV4地址,这是最常用的一种解析类型
AAAAIPV6 internet Address,将FQDN解析成IPV6地址
CNAMEthe canonical name for an alias,记录别名,将域名解析到另一个域名
MXMail eXchanger,用于邮件交换,指定邮件服务
NSName Server,将解析指向另外一个DNS,表示由被指定的DNS解析当前记录
PTRa domain name pointer,域名指针,用IP地址反向解析域名
SOAa marks the start of a zone of authority,标记权威区域的开始
HINFOhost information,主机信息
KEY安全密钥记录
MD/MF被废弃,使用 MX
MINFOmailbox or mail list information,邮箱或邮件列表信息
SRVservice and protocol,服务和协议
TXTtext strings,文本字符串,此类型值长度限定不超过512个字符
WKSa well known service description/众所周知的服务描述

②A、AAAA记录

A 记录也称主机记录,是将主机域名解析到指定的物理机IPV4地址上,是DNS解析服务中最常见的资源记录。
AAAA 记录是IPV6地址的主机记录,是将主机域名解析到指定物理机的IPV6地址上。

1www.linux-magedu.com.   86400 IN A  10.0.0.210
2blog.linux-magedu.com.  86400 IN A  10.0.0.206 
3blog.linux-magedu.com.  86400 IN A  10.0.0.210     #如果有多条A记录,且有A记录的IP与DNS机器IP相同,则优先返回
4*.linux-magedu.com.     86400 IN A  10.0.0.210     #泛解析,匹配所有以 linuxmagedu.com结束的域名或主机名
5linux-magedu.com.       86400 IN A  10.0.0.167 
6@                       86400 IN A  10.0.0.167     #@代表域名,此条记录含义同上

③CNAME别名记录

记录别名,将域名解析到另一个域名。

abc.linux-magedu.com.   86400 IN CNAME def.magedu.com. #将 abc.linuxmagedu.com 解析到def.magedu.com

④MX记录

邮件交换 (MX) 记录将一个域的电子邮件定向到托管该域用户帐号的服务器(SMTP服务器)。

一个域可以定义多条MX记录,但每条MX记录的优先级不同,如果邮件通过最高优先级记录无法递送,则采用第二优先级,以此类推。
每个MX记录的value之前有一个数字(0-99),表示此服务器的优先级,数字越小优先级越高;对MX记录而言,任何一个MX记录后面的服务器名字,都应该在后续有一个A记录

1linux-magedu.com.      86400   IN MX  10 mx1.linux-magedu.com.
2linux-magedu.com.      86400   IN MX  20 mx2.linux-magedu.com.
3
4mx1.linux-magedu.com.  86400   IN A   10.0.0.206
5mx2.linux-magedu.com.  86400   IN A   10.0.0.208

⑤NS记录

域名服务器 (NS) 记录用于确定哪些服务器为一个网域提供DNS解析服务。一般来说,为了服务的安全可靠,一个域名,至少应该有两条NS记录,保证服务的冗余,防止出现单点失败。

1inux-magedu.com.        86400 IN NS dns1.linux-magedu.com.
2linux-magedu.com.       86400 IN NS dns2.linux-magedu.com.
3
4dns1.linux-magedu.com.  86400 IN A  10.0.0.206
5dns2.linux-magedu.com.  86400 IN A  10.0.0.208

⑥PTR记录

PTR记录是A记录的逆向记录,又称做IP反查记录或指针记录,负责将IP反向解析为域名。

1#A记录
2blog.linux-magedu.com.    86400 IN A   10.0.0.167
3
4#与其对应的PTR记录
5#IP地址反着写,后面跟 .in-addr.arpa.
6167.0.0.10.in-addr.arpa.  86400 IN PTR blog.linux-magedu.com.

⑦SOA记录

SOA记录是起始授权记录,用于设置当前DNS服务器的某些规则,SOA记录要放在整个文件的最开始。
SOA 记录表示此DNS是该域名的权威解析服务器,当在查询的过程中,各级缓存都没有要查询的内容时,最后会通过递归查询的方式到达此DNS服务器,并请求此域名的SOA记录。
SOA记录同样符合 NAME TTL CLASS TYPE VALUE 这五个字段的格式定义,但VALUE字段值较多。

VALUE字段

字段说明
DNS服务器名称描述性字段,表示当前DNS服务器名称
服务器管理员邮箱邮箱中的@要写成 .
当前数据库的版本号主从服务器要同步数据,此字段就是数据更新的标识
从服务器拉取数据的时间间隔从服务器拉取数据的时间间隔
从服务器同步失败后重试时间间隔上次同步失败后,间隔多久重试
从服务器同步失败超过多长时间从服务器失败同步失败时长超过此值,则认为从服务器数据无效
不存在的记录缓存时长当查询一个不存在的解析记录时,该记录在指定时间内直接返回不存在
1linux-magedu.com.   86400 IN SOA linux-dns. admin.linux-dns.com. (123 3H 15M 1D 1W)
2
3#服务器名 linux-dns
4#管理员邮箱   [email protected]
5#版本号 123
6#从服务器更新间隔 3H
7#失败重试间隔 15M
8#从服务器数据失效时长 1D
9#无效记录缓存时长     1W
10

⑧TXT记录

TXT记录是对域进行标识和说明的一种方式,一般用于验证记录,该记录的值可以是人工可读文本,也可以是机器可读文本,也可为空。

使用场景:做SPF(反垃圾邮件)记录,验证域名所有权。

1_dnsauth.linux-magedu.com.      86400 IN TXT 
22024dtetmvzwclwf6wsl0y6jcpvwga2wkibgyb1a103yd7re2

4、实现DNS自解析主机

主机IP角色备注
10.0.0.157-ubuntuDNS SERVER为客户端主机提供DNS解析服务
10.0.0.154-rocky客户端客户端主机将DNS配置为10.0.0.157
10.0.0.153-rockyWEB-提供web网站,解析域名

在DNS SERVER上实现域名解析

新增zones

1[root@ubuntu-157 bind]# vim /etc/bind/named.conf.default-zones 
2
3[root@ubuntu-157 bind]# cat /etc/bind/named.conf.default-zones 
4....
5zone "linux-magedu.com" IN {
6	type master;
7	file "/etc/bind/db.linux-magedu.com";        #具体解析规则文件
8};
9

设置具体解析规则

1[root@ubuntu-157 ~]# cd /etc/bind/
2[root@ubuntu-157 bind]# ls
3db.0    db.empty    named.conf.default-zones  rndc.key
4db.127  db.local    named.conf.local          zones.rfc1918
5db.255  named.conf  named.conf.options
6
7[root@ubuntu-157 bind]# vim /etc/bind/db.linux-magedu.com
8linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 123 3H 15M 1D 1W )
9
10linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
11linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.
12
13dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
14dns2.linux-magedu.com.  86400  IN  A   10.0.0.157
15
16www.linux-magedu.com.  86400  IN  A   10.0.0.153
17w.linux-magedu.com.    86400  IN  A   1.2.3.4
18bbs.linux-magedu.com.  86400  IN  A   11.11.11.11
19
20#修改权限,修改属主属组(可选)
21[root@ubuntu ~]# chmod 644 /etc/bind/db.linux-magedu.com 
22[root@ubuntu ~]# chown root.root /etc/bind/db.linux-magedu.com
23
24#语法检查
25[root@ubuntu-157 bind]# named-checkzone linux-magedu.com ./db.linux-magedu.com    #域名 对应解析规则文件
26zone linux-magedu.com/IN: loaded serial 123
27OK
28
29#重载生效
30[root@ubuntu-157 bind]# rndc reload
31server reload successful
32

服务端测试

1[root@ubuntu-157 bind]# dig www.linux-magedu.com
2
3; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> www.linux-magedu.com
4;; global options: +cmd
5;; Got answer:
6;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41337
7;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
8
9;; OPT PSEUDOSECTION:
10; EDNS: version: 0, flags:; udp: 1232
11; COOKIE: 90a348da1df4980b0100000068f4a05b47bd9983787466eb (good)
12;; QUESTION SECTION:
13;www.linux-magedu.com.		IN	A
14
15;; ANSWER SECTION:
16www.linux-magedu.com.	86400	IN	A	10.0.0.153
17
18;; Query time: 0 msec
19;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
20;; WHEN: Sun Oct 19 16:24:59 CST 2025
21;; MSG SIZE  rcvd: 93
22
23[root@ubuntu-157 bind]# dig w.linux-magedu.com
24
25; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> w.linux-magedu.com
26;; global options: +cmd
27;; Got answer:
28;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45549
29;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
30
31;; OPT PSEUDOSECTION:
32; EDNS: version: 0, flags:; udp: 1232
33; COOKIE: 380717e24e4d2d960100000068f4a0802440deb31b424730 (good)
34;; QUESTION SECTION:
35;w.linux-magedu.com.		IN	A
36
37;; ANSWER SECTION:
38w.linux-magedu.com.	86400	IN	A	1.2.3.4
39
40;; Query time: 0 msec
41;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
42;; WHEN: Sun Oct 19 16:25:36 CST 2025
43;; MSG SIZE  rcvd: 91
44
45[root@ubuntu-157 bind]# dig bbs.linux-magedu.com
46
47; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> bbs.linux-magedu.com
48;; global options: +cmd
49;; Got answer:
50;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1305
51;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
52
53;; OPT PSEUDOSECTION:
54; EDNS: version: 0, flags:; udp: 1232
55; COOKIE: b46e28297f5c3b5e0100000068f4a0874befd4097972c809 (good)
56;; QUESTION SECTION:
57;bbs.linux-magedu.com.		IN	A
58
59;; ANSWER SECTION:
60bbs.linux-magedu.com.	86400	IN	A	11.11.11.11
61
62;; Query time: 0 msec
63;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
64;; WHEN: Sun Oct 19 16:25:43 CST 2025
65;; MSG SIZE  rcvd: 93
66
67[root@ubuntu-157 bind]# ping www.linux-magedu.com 
68PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
6964 bytes from 10.0.0.153: icmp_seq=1 ttl=64 time=4.19 ms
7064 bytes from 10.0.0.153: icmp_seq=2 ttl=64 time=0.955 ms
7164 bytes from 10.0.0.153: icmp_seq=3 ttl=64 time=0.746 ms
7264 bytes from 10.0.0.153: icmp_seq=4 ttl=64 time=0.495 ms
73^C
74--- www.linux-magedu.com ping statistics ---
754 packets transmitted, 4 received, 0% packet loss, time 3024ms
76rtt min/avg/max/mdev = 0.495/1.597/4.194/1.507 ms
77[root@ubuntu-157 bind]# ping w.linux-magedu.com 
78PING w.linux-magedu.com (1.2.3.4) 56(84) bytes of data.
79^C
80--- w.linux-magedu.com ping statistics ---
8123 packets transmitted, 0 received, 100% packet loss, time 22559ms
82
83

在客户端上配置并测试

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.2
5
6#指定DNS服务器的IP地址
7[root@rocky-154 ~]#vim /etc/resolv.conf 
8# Generated by NetworkManager
9search localdomain
10nameserver 10.0.0.157
11
12[root@rocky-154 ~]#dig www.linux-magedu.com
13
14; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.linux-magedu.com
15;; global options: +cmd
16;; Got answer:
17;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52881
18;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
19
20;; OPT PSEUDOSECTION:
21; EDNS: version: 0, flags:; udp: 1232
22; COOKIE: 55815c97d605789a0100000068f4a0e7d39a4f07008f1eb3 (good)
23;; QUESTION SECTION:
24;www.linux-magedu.com.		IN	A
25
26;; ANSWER SECTION:
27www.linux-magedu.com.	86400	IN	A	10.0.0.153
28
29;; Query time: 4 msec
30;; SERVER: 10.0.0.157#53(10.0.0.157)
31;; WHEN: Sun Oct 19 16:27:18 CST 2025
32;; MSG SIZE  rcvd: 93
33
34[root@rocky-154 ~]#nslookup www.linux-magedu.com
35Server:		10.0.0.157
36Address:	10.0.0.157#53
37
38Name:	www.linux-magedu.com
39Address: 10.0.0.153
40
41[root@rocky-154 ~]#host bbs.linux-magedu.com
42bbs.linux-magedu.com has address 11.11.11.11
43
44[root@rocky-154 ~]#ping w.linux-magedu.com
45PING w.linux-magedu.com (1.2.3.4) 56(84) bytes of data.
46^C
47--- w.linux-magedu.com ping statistics ---
482 packets transmitted, 0 received, 100% packet loss, time 1056ms

在web服务主机上实现网站

1[root@rocky8-153 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.2
5
6[root@rocky8-153 ~]#yum install nginx
7
8[root@rocky8-153 ~]#systemctl restart nginx.service
9[root@rocky8-153 ~]#systemctl disable --now  firewalld
10
11[root@rocky8-153 ~]#cd /usr/share/nginx/html
12
13[root@rocky8-153 html]#ls
14404.html  50x.html  index.html  nginx-logo.png  poweredby.png
15[root@rocky8-153 html]#mv index.html index.html.bak
16
17[root@rocky8-153 html]# vim index.html
18<h1>this is www.linux-magedu.com</h1>
19
20#本机测试
21[root@rocky8-153 html]#curl 127.1
22<h1>this is www.linux-magedu.com</h1>
23
24#客户端测试
25[root@rocky-154 ~]#curl www.linux-magedu.com
26<h1>this is www.linux-magedu.com</h1>
27

在windows物理机上测试

网络"属性"——>更改适配器设置——>Vmnet网卡"属性"——>IPv4协议"属性"——>更改DNS服务器地址。

1C:\Users\HhAosocool>ping www.linux-magedu.com
2
3正在 Ping www.linux-magedu.com [10.0.0.153] 具有 32 字节的数据:
4来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
5来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
6来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
7来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
8
910.0.0.153 的 Ping 统计信息:
10    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失)11往返行程的估计时间(以毫秒为单位):
12    最短 = 0ms,最长 = 0ms,平均 = 0ms

5、实现DNS自解析的主从

在现实环境中,一台主机通常需要配置多个DNS服务器,如果只有单个DNS服务器,则有可能会出现因为DNS服务当机,而无法解析域名的情况出现。Windows系统中配置IPV4地址时,要求指定两个DNS服务器。

主从服务之间数据同步,有两种方式

1、主服务器向从服务器推送数据(PUSH)。
2、从服务器向主服务器拉取数据(PULL)。

主机IP角色备注
10.0.0.157-ubuntuMASTER DNS SERVER为客户端主机提供DNS解析服务
10.0.0.150-ubuntuSLAVE DNS SERVER为客户端主机提供DNS解析服务,同时从master同步解析数据
10.0.0.154-rocky客户端客户端主机将DNS配置为10.0.0.157
10.0.0.153-rockyWEB-提供web网站,解析域名

配置DNS主从

DNS SLAVE配置

1#安装
2[root@ubuntu2204-150 ~]# apt install bind9 -y
3
4#关闭systemd-resolved服务
5[root@ubuntu2204-150 ~]# systemctl stop systemd-resolved.service 
6
7#解决ubuntu中的systemd-resolved服务DNS相关问题
8[root@ubuntu2204-150 ~]# ll /etc/resolv.conf 
9lrwxrwxrwx 1 root root 39 Sep 11  2024 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
10[root@ubuntu2204-150 ~]# rm -rf /etc/resolv.conf 
11[root@ubuntu2204-150 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
12'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
13[root@ubuntu2204-150 ~]# ll /etc/resolv.conf 
14lrwxrwxrwx 1 root root 32 Oct 19 09:35 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
15
16[root@ubuntu2204-150 ~]# vim /etc/resolv.conf
17nameserver 127.0.0.1
18
19#新增主从同步配置
20[root@ubuntu2204-150 ~]# vim /etc/bind/named.conf.default-zones 
21...
22zone "linux-magedu.com" IN {
23	type slave;
24	masters {10.0.0.157;};
25	file "/etc/bind/slaves/db.linux-magedu.com";
26};
27
28#创建目录
29[root@ubuntu2204-150 ~]# mkdir /etc/bind/slaves
30[root@ubuntu2204-150 ~]# chmod g=rwx /etc/bind/slaves
31
32#/etc/apparmor.d/usr.sbin.named文件第19行后添加
33[root@ubuntu2204-150 ~]# vim /etc/apparmor.d/usr.sbin.named 
34/etc/bind/slaves/** rwk,
35/etc/bind/slaves/ rwk,
36
37#重启服务
38[root@ubuntu2204-150 ~]# systemctl restart apparmor.service 
39
40[root@ubuntu2204-150 ~]# systemctl restart named
41
42#解析文件同步
43[root@ubuntu2204-150 ~]# ls /etc/bind/slaves
44db.linux-magedu.com
45
46#文件格式与MASTER上不一样,不再是文本文件
47[root@ubuntu2204-150 ~]# file /etc/bind/slaves/db.linux-magedu.com 
48/etc/bind/slaves/db.linux-magedu.com: data
49

DNS MASTER配置

1#添加从DNS配置,此配置是保证能推送数据到从节点
2[root@ubuntu-157 ~]# cat /etc/bind/db.linux-magedu.com 
3linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 123 3H 15M 1D 1W )
4
5linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
6linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.
7
8dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
9dns2.linux-magedu.com.  86400  IN  A   10.0.0.150        #修改为从DNS服务器IP地址
10
11www.linux-magedu.com.  86400  IN  A   10.0.0.153
12w.linux-magedu.com.    86400  IN  A   1.2.3.4
13bbs.linux-magedu.com.  86400  IN  A   11.11.11.11
14

客户端测试

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.157
5
6#默认DNS返回
7[root@rocky-154 ~]#nslookup www.linux-magedu.com
8Server:		10.0.0.157
9Address:	10.0.0.157#53
10
11Name:	www.linux-magedu.com
12Address: 10.0.0.153
13
14#指定从DNS地址
15[root@rocky-154 ~]#dig bbs.linux-magedu.com @10.0.0.150
16
17; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> bbs.linux-magedu.com @10.0.0.150
18;; global options: +cmd
19;; Got answer:
20;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29632
21;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
22
23;; OPT PSEUDOSECTION:
24; EDNS: version: 0, flags:; udp: 1232
25; COOKIE: 9135d08c270822ff0100000068f4d7986dd5213e63826b0e (good)
26;; QUESTION SECTION:
27;bbs.linux-magedu.com.		IN	A
28
29;; ANSWER SECTION:
30bbs.linux-magedu.com.	86400	IN	A	11.11.11.11
31
32;; Query time: 1 msec
33;; SERVER: 10.0.0.150#53(10.0.0.150)
34;; WHEN: Sun Oct 19 20:20:39 CST 2025
35;; MSG SIZE  rcvd: 93
36
37[root@rocky-154 ~]#host w.linux-magedu.com
38w.linux-magedu.com has address 1.2.3.4
39
40#关闭DNS MASTER上的服务
41[root@ubuntu-157 bind]# systemctl stop named.service
42
43[root@ubuntu-157 bind]# systemctl is-active named
44inactive
45
46#测试
47[root@rocky-154 ~]#ping www.linux-magedu.com
48ping: www.linux-magedu.com: Name or service not known
49
50#默认DNS无法解析
51[root@rocky-154 ~]#nslookup bbs.linux-magedu.com
52;; connection timed out; no servers could be reached
53
54#客户端添加从DNS
55[root@rocky-154 ~]#vim /etc/resolv.conf 
56[root@rocky-154 ~]#cat /etc/resolv.conf
57# Generated by NetworkManager
58search localdomain
59nameserver 10.0.0.157
60nameserver 10.0.0.150
61
62#再次测试
63[root@rocky-154 ~]#ping www.linux-magedu.com
64PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
6564 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=1 ttl=64 time=0.905 ms
6664 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=2 ttl=64 time=0.499 ms
6764 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=3 ttl=64 time=0.637 ms
68^C
69--- www.linux-magedu.com ping statistics ---
703 packets transmitted, 3 received, 0% packet loss, time 2026ms
71rtt min/avg/max/mdev = 0.499/0.680/0.905/0.169 ms
72
73#DNS SLAVE能提供解析
74[root@rocky-154 ~]#nslookup bbs.linux-magedu.com
75Server:		10.0.0.150
76Address:	10.0.0.150#53
77
78Name:	bbs.linux-magedu.com
79Address: 11.11.11.11

主从DNS数据同步测试

在 BIND 的 DNS 区域解析规则文件中,版本号(Serial Number)是主从服务器同步的核心触发机制。它的作用类似于版本控制系统中的“提交ID”,用于标识区域文件的修改历史,确保从服务器仅在主服务器的版本号更新时才会同步数据。

1#在DNS MASTER增加一条db解析记录
2[root@ubuntu-157 bind]# cat db.linux-magedu.com 
3linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 124 3H 15M 1D 1W )
4
5linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
6linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.
7
8dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
9dns2.linux-magedu.com.  86400  IN  A   10.0.0.150
10
11www.linux-magedu.com.  86400  IN  A   10.0.0.153
12w.linux-magedu.com.    86400  IN  A   1.2.3.4
13bbs.linux-magedu.com.  86400  IN  A   11.11.11.11
14blog.linux-magedu.com. 86400  IN  A   6.6.6.6                #添加
15
16#查看文件修改时间
17[root@ubuntu-157 bind]# ll db.linux-magedu.com 
18-rw-r--r-- 1 root bind 502 Oct 19 20:37 db.linux-magedu.com
19
20[root@ubuntu-157 bind]# dig blog.linux-magedu.com
21
22; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> blog.linux-magedu.com
23;; global options: +cmd
24;; Got answer:
25;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2621
26;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
27
28;; OPT PSEUDOSECTION:
29; EDNS: version: 0, flags:; udp: 1232
30; COOKIE: fc865b82c15d437f0100000068f4dc0549e5eac6e4341ca1 (good)
31;; QUESTION SECTION:
32;blog.linux-magedu.com.		IN	A
33
34;; ANSWER SECTION:
35blog.linux-magedu.com.	86400	IN	A	6.6.6.6
36
37;; Query time: 0 msec
38;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
39;; WHEN: Sun Oct 19 20:39:33 CST 2025
40;; MSG SIZE  rcvd: 94
41
42#在DNS SLAVE测试 - 还未同步数据,解析失败
43[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com 
44-rw-r--r-- 1 bind bind 464 Oct 19 20:00 /etc/bind/slaves/db.linux-magedu.com
45
46[root@ubuntu2204-150 ~]# dig blog.linux-magedu.com
47
48; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> blog.linux-magedu.com
49;; global options: +cmd
50;; Got answer:
51;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 62014
52;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
53
54;; OPT PSEUDOSECTION:
55; EDNS: version: 0, flags:; udp: 1232
56; COOKIE: 7c3490c9cbb3fc8a0100000068f4dc68998a6748289824f5 (good)
57;; QUESTION SECTION:
58;blog.linux-magedu.com.		IN	A
59
60;; AUTHORITY SECTION:
61linux-magedu.com.	86400	IN	SOA	linux-dns.linux-magedu.com. admin.linux-magedu.com. 123 10800 900 86400 604800
62
63;; Query time: 3 msec
64;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
65;; WHEN: Sun Oct 19 20:41:12 CST 2025
66;; MSG SIZE  rcvd: 130
67
68#DNS MASTER更改版本号
69[root@ubuntu-157 bind]# vim db.linux-magedu.com
70linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 125 3H 15M 1D 1W )
71...
72
73[root@ubuntu-157 bind]# rndc reload
74server reload successful
75
76[root@ubuntu-157 bind]# ll db.linux-magedu.com 
77-rw-r--r-- 1 root bind 502 Oct 19 20:43 db.linux-magedu.com
78
79#DNS SLAVE 同步成功
80[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com 
81-rw-r--r-- 1 bind bind 464 Oct 19 20:44 /etc/bind/slaves/db.linux-magedu.com
82

TCP端口和UDP端口

在DNS服务中,TCP53端口主要用于主从之间的数据同步,UDP53端口主要用于对外提供域名解析服务,所以禁掉TCP53,并不会影响DNS服务对外提供查询服务。实事上UDP端口也要参与数据同步。

6、实现DNS自解析的反向解析

1[root@ubuntu-157 ~]# vim /etc/bind/named.conf.default-zones 
2[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones 
3...
4zone "0.0.10.in-addr.arpa" IN {
5	type master;
6	file "/etc/bind/db.0.0.10.in-addr.arpa";
7};
8
9#设置解析规则
10[root@ubuntu-157 ~]# vim /etc/bind/db.0.0.10.in-addr.arpa
11[root@ubuntu-157 ~]# cat /etc/bind/db.0.0.10.in-addr.arpa
120.0.10.in-addr.arpa.  86400  IN  SOA  ptr-test.  admin.linux-magedu.com.  ( 1 3H 15M 1D 1W )
130.0.10.in-addr.arpa.  86400  IN  NS   ptr.linux-magedu.com.
14
15153.0.0.10.in-addr.arpa.  86400  IN  PTR  www.linux-magedu.com.
16123.0.0.10.in-addr.arpa.  86400  IN  PTR  about.linux-magedu.com.
17
18#语法检查
19[root@ubuntu-157 ~]# named-checkzone 0.0.10.in-addr.arpa /etc/bind/db.0.0.10.in-addr.arpa
20zone 0.0.10.in-addr.arpa/IN: loaded serial 1
21OK
22
23[root@ubuntu-157 ~]# rndc reload
24server reload successful
25
26#客户端测试
27[root@rocky-154 ~]#dig -x 10.0.0.153 @10.0.0.157
28
29; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> -x 10.0.0.153 @10.0.0.157
30;; global options: +cmd
31;; Got answer:
32;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2247
33;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
34
35;; OPT PSEUDOSECTION:
36; EDNS: version: 0, flags:; udp: 1232
37; COOKIE: 20fb75d2520da50b0100000068f4eb3f00689718c6841cf9 (good)
38;; QUESTION SECTION:
39;153.0.0.10.in-addr.arpa.	IN	PTR
40
41;; ANSWER SECTION:
42153.0.0.10.in-addr.arpa. 86400	IN	PTR	www.linux-magedu.com.
43
44;; Query time: 1 msec
45;; SERVER: 10.0.0.157#53(10.0.0.157)
46;; WHEN: Sun Oct 19 21:44:30 CST 2025
47;; MSG SIZE  rcvd: 114
48
49[root@rocky-154 ~]#dig -x 10.0.0.123 @10.0.0.157
50
51; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> -x 10.0.0.123 @10.0.0.157
52;; global options: +cmd
53;; Got answer:
54;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33759
55;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
56
57;; OPT PSEUDOSECTION:
58; EDNS: version: 0, flags:; udp: 1232
59; COOKIE: d94e61f59dd337330100000068f4eb3e92c6bfc888e9dc8b (good)
60;; QUESTION SECTION:
61;123.0.0.10.in-addr.arpa.	IN	PTR
62
63;; ANSWER SECTION:
64123.0.0.10.in-addr.arpa. 86400	IN	PTR	about.linux-magedu.com.
65
66;; Query time: 1 msec
67;; SERVER: 10.0.0.157#53(10.0.0.157)
68;; WHEN: Sun Oct 19 21:44:29 CST 2025
69;; MSG SIZE  rcvd: 116
1#上述规则 其他写法
2
3$TTL 1D
4@    IN    SOA    ptr-test. admin.linux-magedu.com. (1 3H 15M 1D 1W)
5           NS     ptr.linux-magedu.com.
6153        PTR    www.linux-magedu.com.
7123        PTR    about.linux-magedu.com.
8

7、实现DNS缓存

在客户端实现DNS缓存,可以保证在DNS服务不可用时,还能使用DNS在本地的缓存数据。另外,本地缓存的存在,也加快了DNS的查询速度。

Rocky系统中设置DNS缓存

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.157
5
6[root@rocky-154 ~]#ping www.linux-magedu.com
7PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
864 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=0.732 ms
964 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.486 ms
1064 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.613 ms
1164 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.974 ms
12^C
13--- www.linux-magedu.com ping statistics ---
144 packets transmitted, 4 received, 0% packet loss, time 3045ms
15rtt min/avg/max/mdev = 0.486/0.701/0.974/0.180 ms
16
17#停止157上的DNS服务
18[root@ubuntu-157 bind]# systemctl stop named
19
20
21#客户端测试,无法解析域名
22[root@rocky-154 ~]#ping www.linux-magedu.com
23ping: www.linux-magedu.com: Name or service not known
24
25#启动,让客户端安装
26[root@ubuntu-157 bind]# systemctl start named
27
28#安装缓存软件,执行前保证客户机DNS能用,先启动DNS服务
29[root@rocky-154 ~]#yum install -y nscd
30
31[root@rocky-154 ~]#systemctl enable --now nscd
32Created symlink /etc/systemd/system/multi-user.target.wants/nscd.service → /usr/lib/systemd/system/nscd.service.
33Created symlink /etc/systemd/system/sockets.target.wants/nscd.socket → /usr/lib/systemd/system/nscd.socket.
34
35[root@rocky-154 ~]#ping www.linux-magedu.com
36PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
3764 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=1.26 ms
3864 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.486 ms
3964 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.535 ms
4064 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.414 ms
41^C
42--- www.linux-magedu.com ping statistics ---
434 packets transmitted, 4 received, 0% packet loss, time 4076ms
44rtt min/avg/max/mdev = 0.414/0.624/1.264/0.323 ms
45
46#再次停止157DNS服务
47[root@ubuntu-157 bind]# systemctl stop named
48
49#客户端继续可以解析域名
50[root@rocky-154 ~]#ping www.linux-magedu.com
51PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
5264 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=1.11 ms
5364 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.565 ms
5464 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.426 ms
5564 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.449 ms
56^C
57--- www.linux-magedu.com ping statistics ---
584 packets transmitted, 4 received, 0% packet loss, time 3085ms
59rtt min/avg/max/mdev = 0.426/0.636/1.107/0.278 ms
60
61#没有缓存到,无法解析
62[root@rocky-154 ~]#ping bbs.linux-magedu.com
63ping: bbs.linux-magedu.com: Name or service not known
64
65#启动DNS服务
66[root@ubuntu-157 bind]# systemctl start named
67
68[root@rocky-154 ~]#ping bbs.linux-magedu.com
69PING bbs.linux-magedu.com (11.11.11.11) 56(84) bytes of data.
70^C
71--- bbs.linux-magedu.com ping statistics ---
723 packets transmitted, 0 received, 100% packet loss, time 4122ms
73
74#再次关闭157DNS服务
75[root@ubuntu-157 bind]# systemctl stop named
76
77[root@rocky-154 ~]#ping bbs.linux-magedu.com
78PING bbs.linux-magedu.com (11.11.11.11) 56(84) bytes of data.
79^C
80--- bbs.linux-magedu.com ping statistics ---
813 packets transmitted, 0 received, 100% packet loss, time 2077ms

Rocky系统查看DNS缓存

1[root@rocky-154 ~]#nscd -g
2nscd configuration:
3
4              0  server debug level
5        13m  2s  server runtime
6              5  current number of threads
7             32  maximum number of threads
8              0  number of times clients had to wait
9             no  paranoia mode enabled
10           3600  restart internal
11              5  reload count
12...
13

Ubuntu系统中的DNS缓存

1#ubuntu自带DNS缓存,systemd-resolved服务提供
2#用一台新主机160,没有配置过bind程序的主机
3
4[root@ubuntu2204-160 ~]# cat /etc/resolv.conf 
5...
6nameserver 127.0.0.53
7options edns0 trust-ad
8search localdomain
9
10
11#无法PING通,因为该域名解析是配置在157主机上
12[root@ubuntu2204-160 ~]# ping www.linux-magedu.com -c1
13ping: www.linux-magedu.com: Name or service not known
14
15[root@ubuntu2204-160 ~]# dig www.linux-magedu.com
16
17; <<>> DiG 9.18.30-0ubuntu0.22.04.2-Ubuntu <<>> www.linux-magedu.com
18;; global options: +cmd
19;; Got answer:
20;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 30658
21;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
22
23;; OPT PSEUDOSECTION:
24; EDNS: version: 0, flags:; udp: 65494
25;; QUESTION SECTION:
26;www.linux-magedu.com.		IN	A
27
28;; AUTHORITY SECTION:
29com.			5	IN	SOA	a.gtld-servers.net. nstld.verisign-grs.com. 1760938999 1800 900 604800 900
30
31;; Query time: 476 msec
32;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
33;; WHEN: Mon Oct 20 05:43:34 UTC 2025
34;; MSG SIZE  rcvd: 122
35
36#指定DNS
37[root@ubuntu2204-160 ~]# dig www.linux-magedu.com @10.0.0.157
38
39; <<>> DiG 9.18.30-0ubuntu0.22.04.2-Ubuntu <<>> www.linux-magedu.com @10.0.0.157
40;; global options: +cmd
41;; Got answer:
42;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57569
43;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
44
45;; OPT PSEUDOSECTION:
46; EDNS: version: 0, flags:; udp: 1232
47; COOKIE: d0dab62d0195cd770100000068f5cc391efd2be593473270 (good)
48;; QUESTION SECTION:
49;www.linux-magedu.com.		IN	A
50
51;; ANSWER SECTION:
52www.linux-magedu.com.	86400	IN	A	10.0.0.153
53
54;; Query time: 3 msec
55;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
56;; WHEN: Mon Oct 20 05:44:25 UTC 2025
57;; MSG SIZE  rcvd: 93
58
59#修改网卡,指定DNS
60[root@ubuntu2204-160 ~]# cat /etc/netplan/50-cloud-init.yaml 
61...
62network:
63    ethernets:
64        ens33:
65           #dhcp4: true
66           addresses: [10.0.0.160/24]
67           routes: [{to: default,via: 10.0.0.2}]
68           nameservers:
69             addresses: [10.0.0.157]           #指定DNS10.0.0.157
70    version: 2
71
72[root@ubuntu2204-160 ~]# netplan apply 
73
74#测试-生效
75[root@ubuntu2204-160 ~]# host www.linux-magedu.com
76www.linux-magedu.com has address 10.0.0.153
77
78[root@ubuntu2204-160 ~]# ping www.linux-magedu.com -c1
79PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
8064 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=2.87 ms
81
82--- www.linux-magedu.com ping statistics ---
831 packets transmitted, 1 received, 0% packet loss, time 0ms
84rtt min/avg/max/mdev = 2.867/2.867/2.867/0.000 ms
85
86#关闭157 远程DNS服务
87[root@ubuntu-157 ~]# systemctl stop named
88
89#再次测试客户端,上次解析后有了缓存
90[root@ubuntu2204-160 ~]# ping www.linux-magedu.com 
91PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
9264 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=0.631 ms
9364 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.364 ms
9464 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.509 ms
95^C
96--- www.linux-magedu.com ping statistics ---
973 packets transmitted, 3 received, 0% packet loss, time 2004ms
98rtt min/avg/max/mdev = 0.364/0.501/0.631/0.109 ms

Ubuntu系统查看DNS缓存

1[root@ubuntu2204-160 ~]# resolvectl statistics 
2DNSSEC supported by current servers: no
3
4Transactions            
5Current Transactions: 0
6  Total Transactions: 48
7                        
8Cache                   
9  Current Cache Size: 2
10          Cache Hits: 5
11        Cache Misses: 6
12                        
13DNSSEC Verdicts         
14              Secure: 0
15            Insecure: 0
16               Bogus: 0
17       Indeterminate: 0

Windows中的DNS缓存

1C:\Users\HhAosocool>ipconfig/displaydns
2...
3www.linux-magedu.com
4    ----------------------------------------
5    记录名称. . . . . . . : www.linux-magedu.com
6    记录类型. . . . . . . : 1
7    生存时间. . . . . . . : 86373
8    数据长度. . . . . . . : 4
9    部分. . . . . . . . . : 答案
10    A (主机)记录  . . . . : 10.0.0.153

8、实现DNS自解析的子域

主域(Parent Domain):例如 example.com,由主 DNS 服务器(如 ns1.example.com)管理。
子域(Subdomain):例如 sub.example.com,原本由主域的 DNS 服务器解析,但通过委派,将解析权交给另一台独立的 DNS 服务器(如 ns1.sub.example.com)。

将子域委派给其它的DNS服务器解析,实现分布式DNS服务。即将一个主域(如 example.com)下的某个子域(如 sub.example.com)的解析权完全交给另一台独立的 DNS 服务器管理,实现负载均衡、区域隔离、权限下放,是构建大规模、高可用 DNS 服务的常见方法。

主机IP角色备注
10.0.0.157-ubuntuMASTER DNS SERVER为客户端主机提供DNS解析服务
10.0.0.150-ubuntuSLAVE DNS SERVER为客户端主机提供DNS解析服务,同时从master同步解析数据
10.0.0.160-ubuntuSubdomain DNS SERVER为子域提供解析服务
10.0.0.154-rocky客户端客户端主机将DNS配置为10.0.0.157
10.0.0.153-rockyWEB-提供web网站,解析域名

DNS MASTER 配置

1#添加bj子域配置,bj子域由bjdns服务解析,修改序列号
2[root@ubuntu-157 ~]# cat /etc/bind/db.linux-magedu.com 
3linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 127 3H 15M 1D 1W )
4
5linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
6linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.
7bj.linux-magedu.com.  86400  IN  NS  bj.linux-magedu.com.
8
9dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
10dns2.linux-magedu.com.  86400  IN  A   10.0.0.150
11bj.linux-magedu.com.    86400  IN  A   10.0.0.160
12
13www.linux-magedu.com.  86400  IN  A   10.0.0.153
14w.linux-magedu.com.    86400  IN  A   1.2.3.4
15bbs.linux-magedu.com.  86400  IN  A   11.11.11.11
16
17#重载生效
18[root@ubuntu-157 ~]# rndc reload
19server reload successful
20
21[root@ubuntu-157 ~]# ll /etc/bind/db.linux-magedu.com 
22-rw-r--r-- 1 root bind 563 Oct 20 14:43 /etc/bind/db.linux-magedu.com
23
24#查看从服务器,主从同步成功
25[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com
26-rw-r--r-- 1 bind bind  465 Oct 20 14.43 /etc/bind/db.linux-magedu.com

子域DNS配置

1[root@ubuntu2204-160 ~]# apt install bind9 -y
2
3#子域配置
4[root@ubuntu2204-160 ~]# cat /etc/bind/named.conf.default-zones 
5...
6zone "bj.linux-magedu.com" IN {
7	type master;
8	file "/etc/bind/db.bj.linux-magedu.com";
9};
10
11[root@ubuntu2204-160 ~]# cat /etc/bind/db.bj.linux-magedu.com
12bj.linux-magedu.com.  86400  IN  SOA  bj-dns.  admin.bj-dns.com ( 123 3H 15M 1D 1W )
13
14bj.linux-magedu.com.  86400  IN  NS   bj-sub.bj.linux-magedu.com.
15
16bj-sub.bj.linux-magedu.com.  86400  IN  A   10.0.0.160
17www.bj.linux-magedu.com.     86400  IN  A   11.22.33.44
18test.bj.linux-magedu.com.    86400  IN  A   55.66.77.88
19
20#生效
21[root@ubuntu2204-160 ~]# rndc reload
22server reload successful
23
24#测试
25#本机配置 标志位:权威aa
26[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @127.1
27
28; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @127.1
29;; global options: +cmd
30;; Got answer:
31;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6684
32;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
33
34;; OPT PSEUDOSECTION:
35; EDNS: version: 0, flags:; udp: 1232
36; COOKIE: 7d5520c1a2a50e4f0100000068f5e29f0933759031431e72 (good)
37;; QUESTION SECTION:
38;www.bj.linux-magedu.com.	IN	A
39
40;; ANSWER SECTION:
41www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44
42
43;; Query time: 0 msec
44;; SERVER: 127.0.0.1#53(127.1) (UDP)
45;; WHEN: Mon Oct 20 07:19:59 UTC 2025
46;; MSG SIZE  rcvd: 96
47
48[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @10.0.0.160
49
50; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @10.0.0.160
51;; global options: +cmd
52;; Got answer:
53;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32359
54;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
55
56;; OPT PSEUDOSECTION:
57; EDNS: version: 0, flags:; udp: 1232
58; COOKIE: ed549f1bd8b0a39d0100000068f5e34d9065625c815c2047 (good)
59;; QUESTION SECTION:
60;www.bj.linux-magedu.com.	IN	A
61
62;; ANSWER SECTION:
63www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44
64
65;; Query time: 0 msec
66;; SERVER: 10.0.0.160#53(10.0.0.160) (UDP)
67;; WHEN: Mon Oct 20 07:22:53 UTC 2025
68;; MSG SIZE  rcvd: 96
69
70#找157DNS解析 157将子域转发到160 非权威
71[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @10.0.0.157
72
73; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @10.0.0.157
74;; global options: +cmd
75;; Got answer:
76;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15187
77;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
78
79;; OPT PSEUDOSECTION:
80; EDNS: version: 0, flags:; udp: 1232
81; COOKIE: 02fba371dc424fad0100000068f5e2d92e7bc11c10558457 (good)
82;; QUESTION SECTION:
83;www.bj.linux-magedu.com.	IN	A
84
85;; ANSWER SECTION:
86www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44
87
88;; Query time: 39 msec
89;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
90;; WHEN: Mon Oct 20 07:20:57 UTC 2025
91;; MSG SIZE  rcvd: 96
92

客户端测试

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.157
5
6#去157DNS解析,被转发到160解析 非权威
7[root@rocky-154 ~]#dig www.bj.linux-magedu.com
8
9; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com
10;; global options: +cmd
11;; Got answer:
12;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57512
13;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
14
15;; OPT PSEUDOSECTION:
16; EDNS: version: 0, flags:; udp: 1232
17; COOKIE: 92ae121f6ff541ea0100000068f5e490ee9c4290a2c87597 (good)
18;; QUESTION SECTION:
19;www.bj.linux-magedu.com.	IN	A
20
21;; ANSWER SECTION:
22www.bj.linux-magedu.com. 85961	IN	A	11.22.33.44
23
24;; Query time: 1 msec
25;; SERVER: 10.0.0.157#53(10.0.0.157)
26;; WHEN: Mon Oct 20 15:28:17 CST 2025
27;; MSG SIZE  rcvd: 96
28
29#直接指定160
30[root@rocky-154 ~]#dig www.bj.linux-magedu.com @10.0.0.160
31
32; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com @10.0.0.160
33;; global options: +cmd
34;; Got answer:
35;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20836
36;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
37
38;; OPT PSEUDOSECTION:
39; EDNS: version: 0, flags:; udp: 1232
40; COOKIE: abb996930a627d5d0100000068f5e5290cadca6788e9fa5c (good)
41;; QUESTION SECTION:
42;www.bj.linux-magedu.com.	IN	A
43
44;; ANSWER SECTION:
45www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44
46
47;; Query time: 2 msec
48;; SERVER: 10.0.0.160#53(10.0.0.160)
49;; WHEN: Mon Oct 20 15:30:50 CST 2025
50;; MSG SIZE  rcvd: 96
51
52#指定从DNS
53[root@rocky-154 ~]#dig www.bj.linux-magedu.com @10.0.0.150
54
55; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com @10.0.0.150
56;; global options: +cmd
57;; Got answer:
58;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 12017
59;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
60
61;; OPT PSEUDOSECTION:
62; EDNS: version: 0, flags:; udp: 1232
63; COOKIE: ae9343ecc7e352a80100000068f5e5dc9177cfd3e64b9383 (good)
64;; QUESTION SECTION:
65;www.bj.linux-magedu.com.	IN	A
66
67;; ANSWER SECTION:
68www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44
69
70;; Query time: 0 msec
71;; SERVER: 10.0.0.150#53(10.0.0.150)
72;; WHEN: Mon Oct 20 15:33:48 CST 2025
73;; MSG SIZE  rcvd: 148
74

9、实现DNS自解析的转发

当前我们配置的DNS,如果在自己无法解析后,将直接请求根域的DNS服务器解析。在某些特定环境下,我们可以设置DNS转发服务,将当前无法解析的域名,转发给另外的DNS解析,如果还是无法解析,再提交互联网。

两种转发

转发策略说明
first当客户端请求解析,如果DNS服务器无记录,则转发给上级DNS, 如果上级DNS无返回,则当前DNS请求根域解析。
only当客户端请求解析,如果DNS服务器无记录,则转发给上级DNS, 不管上级DNS是否能解析,当前DNS都直接返回。
主机IP角色备注
10.0.0.157-ubuntuDNS SERVER为客户端主机提供DNS解析服务
10.0.0.150-ubuntuDNS SERVER为客户端主机提供DNS解析服务,并指定转发DNS
10.0.0.154-rocky客户端客户端主机将DNS配置为10.0.0.157

直接DNS配置

1[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options 
2options {
3    ...
4	dnssec-validation no;            #关闭加密验证
5	forwarders { 10.0.0.150; };      #转发服务器
6	forward first;                   #转发策略,间接DNS没有返回,则直接DNS再次向根域请求解析
7
8	listen-on-v6 { any; };
9};
10
11[root@ubuntu-157 ~]# named-checkconf 
12
13[root@ubuntu-157 ~]# systemctl restart named 
14

间接DNS配置

1#新增域名配置
2[root@ubuntu2204-150 ~]# cat /etc/bind/named.conf.default-zones 
3...
4//zone "linux-magedu.com" IN {            #将主从配置注释
5//	type slave;
6//	masters {10.0.0.157;};
7//	file "/etc/bind/slaves/db.linux-magedu.com";
8//};
9
10zone "golang-magedu.com" IN {
11	type master;
12	file "/etc/bind/db.golang-magedu.com";
13};
14
15[root@ubuntu2204-150 ~]# cat /etc/bind/db.golang-magedu.com
16golang-magedu.com.  86400  IN  SOA  golang-dns.  admin.golang-dns.com ( 123 3H 15M 1D 1W)
17
18golang-magedu.com.  86400  IN  NS   dns.golang-magedu.com.
19
20dns.golang-magedu.com.  86400  IN  A   10.0.0.150
21www.golang-magedu.com.  86400  IN  A   1.2.3.4
22test.golang-magedu.com. 86400  IN  A   5.6.7.8
23
24[root@ubuntu2204-150 ~]# systemctl restart named
25

客户端测试

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.157
5
6#157DNS提供解析 权威
7[root@rocky-154 ~]#nslookup www.linux-magedu.com
8Server:		10.0.0.157
9Address:	10.0.0.157#53
10
11Name:	www.linux-magedu.com
12Address: 10.0.0.153
13
14#由157转发至150提供解析 非权威
15[root@rocky-154 ~]#nslookup www.golang-magedu.com
16Server:		10.0.0.157
17Address:	10.0.0.157#53
18
19Non-authoritative answer:
20Name:	www.golang-magedu.com
21Address: 1.2.3.4
22
23[root@rocky-154 ~]#dig www.golang-magedu.com
24
25; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.golang-magedu.com
26;; global options: +cmd
27;; Got answer:
28;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26964
29;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
30
31;; OPT PSEUDOSECTION:
32; EDNS: version: 0, flags:; udp: 1232
33; COOKIE: 80e8d4b349a904be0100000068f5eb1e11f0013f2cc8b0e6 (good)
34;; QUESTION SECTION:
35;www.golang-magedu.com.		IN	A
36
37;; ANSWER SECTION:
38www.golang-magedu.com.	86335	IN	A	1.2.3.4
39
40;; Query time: 0 msec
41;; SERVER: 10.0.0.157#53(10.0.0.157)
42;; WHEN: Mon Oct 20 15:56:14 CST 2025
43;; MSG SIZE  rcvd: 94
44
45#指定150DNS 权威
46[root@rocky-154 ~]#dig www.golang-magedu.com @10.0.0.150
47
48; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.golang-magedu.com @10.0.0.150
49;; global options: +cmd
50;; Got answer:
51;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33227
52;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
53
54;; OPT PSEUDOSECTION:
55; EDNS: version: 0, flags:; udp: 1232
56; COOKIE: 53898bc3a686acfc0100000068f5eb38b65923499f746733 (good)
57;; QUESTION SECTION:
58;www.golang-magedu.com.		IN	A
59
60;; ANSWER SECTION:
61www.golang-magedu.com.	86400	IN	A	1.2.3.4
62
63;; Query time: 0 msec
64;; SERVER: 10.0.0.150#53(10.0.0.150)
65;; WHEN: Mon Oct 20 15:56:41 CST 2025
66;; MSG SIZE  rcvd: 94
67
68#根据转发策略,此解析转发给了间接DNS服务器,间接DNS服务器再向根域请求解析得到
69[root@rocky-154 ~]#nslookup www.baidu.com
70Server:		10.0.0.157
71Address:	10.0.0.157#53
72
73Non-authoritative answer:
74www.baidu.com	canonical name = www.a.shifen.com.
75Name:	www.a.shifen.com
76Address: 110.242.70.57
77Name:	www.a.shifen.com
78Address: 110.242.69.21
79Name:	www.a.shifen.com
80Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc
81Name:	www.a.shifen.com
82Address: 2408:871a:2100:186c:0:ff:b07e:3fbc
83
1[root@ubuntu2204-150 ~]# nslookup www.baidu.com
2Server:		127.0.0.1
3Address:	127.0.0.1#53
4
5Non-authoritative answer:
6www.baidu.com	canonical name = www.a.shifen.com.
7Name:	www.a.shifen.com
8Address: 220.181.111.1
9Name:	www.a.shifen.com
10Address: 220.181.111.232
11Name:	www.a.shifen.com
12Address: 240e:83:205:381:0:ff:b00f:96a2
13Name:	www.a.shifen.com
14Address: 240e:83:205:1cd:0:ff:b0b8:dee9
15
16#删除间接DNS路由表,并清除DNS缓存
17[root@ubuntu2204-150 ~]# route del default 
18
19[root@ubuntu2204-150 ~]# rndc flush
20
21#无法解析,路由表删除
22[root@ubuntu2204-150 ~]# dig www.baidu.com
23
24; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.baidu.com
25;; global options: +cmd
26;; Got answer:
27;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 47717
28;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
29
30;; OPT PSEUDOSECTION:
31; EDNS: version: 0, flags:; udp: 1232
32; COOKIE: bd56abf6482c84eb0100000068f5f15740ebb073e467a6df (good)
33;; QUESTION SECTION:
34;www.baidu.com.			IN	A
35
36;; Query time: 7 msec
37;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
38;; WHEN: Mon Oct 20 16:22:47 CST 2025
39;; MSG SIZE  rcvd: 70
40
41#客户端测试
42#157转发给150,150路由删除,157自己找根解析
43[root@rocky-154 ~]#dig www.baidu.com
44
45; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.baidu.com
46;; global options: +cmd
47;; Got answer:
48;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48424
49;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
50
51;; OPT PSEUDOSECTION:
52; EDNS: version: 0, flags:; udp: 1232
53; COOKIE: 81554849018aeb0a0100000068f5f24923a555a62167071b (good)
54;; QUESTION SECTION:
55;www.baidu.com.			IN	A
56
57;; ANSWER SECTION:
58www.baidu.com.		254	IN	CNAME	www.a.shifen.com.
59www.a.shifen.com.	120	IN	A	110.242.70.57
60www.a.shifen.com.	120	IN	A	110.242.69.21
61
62;; Query time: 319 msec
63;; SERVER: 10.0.0.157#53(10.0.0.157)
64;; WHEN: Mon Oct 20 16:26:50 CST 2025
65;; MSG SIZE  rcvd: 129
66
67#同样如果删除157的路由客户端主机就无法解析成功

DNS解析转发可以设置在特定域里面

1#原先[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options
2... 
3	dnssec-validation no;
4	forwarders { 10.0.0.150; };
5	forward first;
6
7	listen-on-v6 { any; };
8...
9
10#写在zones中的转发配置的优先级高于全局置
11[root@ubuntu ~]# vim /etc/bind/named.conf.default-zones
12zone "ZONE_NAME" IN {
13    type forward;
14    forward first|only;
15    forwarders {IP;};
16}
17

9、实现DNS自解析的视图

在同一台DNS服务器上,根据不同客户端,实现返回不同的解析结果。这样可以做到将客户端解析到距离其物理地址最近的机房内,以达到加快访问速度,以及分流的目的。

主机IP角色备注
10.0.0.157/192.168.10.161-ubuntuDNS SERVER为客户端主机提供DNS解析服务,根据客户端不同网段,返回不同的解析结果
10.0.0.154/192.168.10.154-rocky客户端

注:客户端主机和DNS 服务器都配置两块仅主机网卡,使用不同网段。

DNS SERVER网络配置

1[root@ubuntu-157 ~]# ip a s
21: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
3    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4    inet 127.0.0.1/8 scope host lo
5       valid_lft forever preferred_lft forever
6    inet6 ::1/128 scope host noprefixroute 
7       valid_lft forever preferred_lft forever
82: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
9    link/ether 00:0c:29:88:bd:f8 brd ff:ff:ff:ff:ff:ff
10    altname enp2s1
11    altname enx000c2988bdf8
12    inet 10.0.0.157/24 brd 10.0.0.255 scope global ens33
13       valid_lft forever preferred_lft forever
14    inet6 fe80::20c:29ff:fe88:bdf8/64 scope link proto kernel_ll 
15       valid_lft forever preferred_lft forever
163: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
17    link/ether 00:0c:29:88:bd:02 brd ff:ff:ff:ff:ff:ff
18    altname enp2s5
19    altname enx000c2988bd02
20
21[root@ubuntu-157 ~]# ip addr a dev ens37 192.168.10.157/24
22
23[root@ubuntu-157 ~]# ip a s
24...
253: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
26    link/ether 00:0c:29:88:bd:02 brd ff:ff:ff:ff:ff:ff
27    altname enp2s5
28    altname enx000c2988bd02
29    inet 192.168.10.157/24 scope global ens37
30       valid_lft forever preferred_lft forever
31

DNS SERVER配置

1#将/etc/bind/named.conf.options 转发策略注释
2
3[root@ubuntu-157 ~]# netplan apply 
4
5[root@ubuntu-157 ~]# systemctl restart named
6
7#192.169.10.157新增内容
8[root@ubuntu-157 ~]# cat /etc/bind/named.conf
9...
10include "/etc/bind/named.conf.options";
11include "/etc/bind/named.conf.local";
12#include "/etc/bind/named.conf.default-zones";            #注释
13
14[root@ubuntu-157 bind]# cat /etc/bind/named.conf.options 
15acl test_net{
16	192.168.10.0/24;
17};
18
19acl prod_net{
20	10.0.0.0/24;
21};
22
23acl other_net{
24	any;
25};
26
27view test_view{
28	match-clients {test_net;};
29	include "/etc/bind/named.conf.default-zones";
30	include "/etc/bind/named.conf.test-zones";
31};
32
33view prod_view{
34	match-clients {prod_net;};
35	include "/etc/bind/named.conf.default-zones";
36	include "/etc/bind/named.conf.prod-zones";
37};
38
39view other_view{
40	match-clients {other_net;};
41	include "/etc/bind/named.conf.default-zones";
42};
43
44...
45
46#新增中间文件
47[root@ubuntu-157 ~]# cat /etc/bind/named.conf.test-zones
48zone "sre-magedu.com" {
49	type master;
50	file "/etc/bind/db.test.sre-magedu.com";
51};
52
53[root@ubuntu-157 ~]# cat /etc/bind/named.conf.prod-zones
54zone "sre-magedu.com" {
55	type master;
56	file "/etc/bind/db.prod.sre-magedu.com";
57};
58
59#具体解析规则
60[root@ubuntu-157 ~]# cat /etc/bind/db.test.sre-magedu.com
61test.sre-magedu.com.  86400  IN  SOA  sre-magedu-dns.  admin.sre-magedu.com. ( 123 3H 15M 1D 1W)
62
63test.sre-magedu.com.  86400  IN  NS   dns1.test.sre-magedu.com.  
64test.sre-magedu.com.  86400  IN  NS   dns2.test.sre-magedu.com.
65
66dns1.test.sre-magedu.com.  86400  IN  A   192.169.10.157
67www.test.sre-magedu.com.   86400  IN  A   192.168.10.11
68blog.test.sre-magedu.com.  86400  IN  A   192.168.10.22
69
70[root@ubuntu-157 ~]# cat /etc/bind/db.prod.sre-magedu.com
71test.sre-magedu.com.  86400  IN  SOA  sre-magedu-dns.  admin.sre-magedu.com. ( 123 3H 15M 1D 1W)
72
73test.sre-magedu.com.  86400  IN  NS   dns1.test.sre-magedu.com.  
74test.sre-magedu.com.  86400  IN  NS   dns2.test.sre-magedu.com.
75
76dns1.test.sre-magedu.com.  86400  IN  A   10.0.0.157
77www.test.sre-magedu.com.   86400  IN  A   10.0.0.11
78blog.test.sre-magedu.com.  86400  IN  A   10.0.0.22
79
80[root@ubuntu-157 bind]# named-checkconf /etc/bind/named.conf
81
82#重启服务
83[root@ubuntu-157 bind]# systemctl restart named

客户端网络配置

1[root@rocky-154 ~]#ip a s
21: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
3    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4    inet 127.0.0.1/8 scope host lo
5       valid_lft forever preferred_lft forever
6    inet6 ::1/128 scope host 
7       valid_lft forever preferred_lft forever
82: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
9    link/ether 00:0c:29:2d:41:13 brd ff:ff:ff:ff:ff:ff
10    altname enp3s0
11    inet 10.0.0.154/24 brd 10.0.0.255 scope global dynamic noprefixroute ens160
12       valid_lft 1458sec preferred_lft 1458sec
13    inet6 fe80::20c:29ff:fe2d:4113/64 scope link noprefixroute 
14       valid_lft forever preferred_lft forever
153: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
16    link/ether 00:0c:29:2d:41:1d brd ff:ff:ff:ff:ff:ff
17    altname enp11s0
18
19[root@rocky-154 ~]#ip addr a dev ens192 192.168.10.154/24 
20
21[root@rocky-154 ~]#nmcli con reload 
22
23[root@rocky-154 ~]#nmcli con up ens192
24Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
25
26[root@rocky-154 ~]#ip a s
27...
283: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
29    link/ether 00:0c:29:2d:41:1d brd ff:ff:ff:ff:ff:ff
30    altname enp11s0
31    inet 192.168.10.154/24 brd 192.168.10.255 scope global noprefixroute ens192
32       valid_lft forever preferred_lft forever

客户端测试

1[root@rocky-154 ~]#cat /etc/resolv.conf 
2# Generated by NetworkManager
3search localdomain
4nameserver 10.0.0.157
5
6[root@rocky-154 ~]#dig -b 10.0.0.154 www.sre-magedu.com +short
7
8[root@rocky-154 ~]#dig -b 10.0.0.154 www.sre-magedu.com @10.0.0.157 +short
910.0.0.11
10
11[root@rocky-154 ~]#dig -b 192.168.10.154 www.sre-magedu.com @192.168.10.157 +short
12192.168.10.11
13

四、智能DNS解析

在前面使用bind中的视图,实现了基于不同来源的客户端IP,对同一个域名,返回不同的解析结果。所谓智能DNS,也是根据不同的条件,对相同的域名解析不同的具体IP地址,但其实现,远比bind中用视图复杂得多。
目前很多DNS服务商都提供了智能DNS服务,智能DNS可以通过多种策略来将客户端需要访问的域名解析到不同的数据中心或不同的运营商网路上,比如通过请求者的IP地址得到客户端的地理位置来判断用户的就近性,并结合健康检查策略,将请求解析到最近的服务器上。

1、GLSB全局负载均衡

GLSB(Global Server Load Balance),全局负载均衡。在广域网上,对不同地域的服务器间的流量进行调度,通过判断服务器的负载,带宽的负载等,决定服务器的可用性,同时判断客户端与服务器之间的链路状况,选择与客户端最佳匹配的服务器,并将客户端请求调度到该服务器上。
因此GLSB是对服务器和链路进行综合判断来决定由哪个机房的服务器来提供服务,实现异地服务器群服务质量的保证。

GLSB 的实现有三种方式:基于DNS实现、基于重定向实现、基于路由协议实现,其中最通用的是基于DNS的实现。
GLSB 的使用范围:常用于有异地机房的WEB系统,或在CDN系统中作为核心的流量调度系统。

1#唯品会使用 gslb 来实现流量负载
2[root@rocky-154 ~]#nslookup www.vip.com
3Server:		10.0.0.2
4Address:	10.0.0.2#53
5
6#将 www.vip.com 的域名解析 cname 到 shop.vip.com.vipgslb.com
7#由 shop.vip.com.vipgslb.com 在 DNS层面实现流量负载
8Non-authoritative answer:
9www.vip.com	canonical name = www.vip.com.vipgslb.com.
10Name:	www.vip.com.vipgslb.com
11Address: 14.215.62.24
12Name:	www.vip.com.vipgslb.com
13Address: 2408:8756:4cff:d301::2
14

2、内容分发网络CDN

CDN(Content Delivery Network)是一种通过在全球分布的服务器节点缓存和分发内容,以加速用户访问速度、提高可用性和降低源站负载的网络技术。

CDN 工作原理:

1、客户端请求 www.a.com ,如果是第一次请求,则需要向DNS服务器请求解析。
2、www.a.com 的运维人员设置了CNANME 解析,将 www.a.com 解析到。 www.acdn.com。
3、www.acdn.com 会根据客户端IP地址,返回一个响应速度最快的IP节点给客户端。
4、客户端根据DNS解析得到的IP地址,访问该节点,该节点一般都是CDN服务器节点。
5、如果该CDN节点被访问时没有相关数据,则会将客户端请求通过内部网络转发到原站点获取数据,同时该CDN节点会将此次请求的内容缓存起来,这样下次该节点被访问时,就不用再去请求源站点。

1#百度使用的CDN技术
2[root@rocky-154 ~]#nslookup www.baidu.com
3Server:		10.0.0.2
4Address:	10.0.0.2#53
5
6Non-authoritative answer:
7www.baidu.com	canonical name = www.a.shifen.com.
8Name:	www.a.shifen.com
9Address: 110.242.70.57
10Name:	www.a.shifen.com
11Address: 110.242.69.21
12Name:	www.a.shifen.com
13Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc
14Name:	www.a.shifen.com
15Address: 2408:871a:2100:186c:0:ff:b07e:3fbc
16
17#百度将域名 cname 到 www.a.shifen.com
18由www.a.shifen.com 负责解析,根据访问者地理位置,返
19回最近的CDN节点IP


深度解析 DNS服务:从基础原理到自建 DNS 服务器完整指南》 是转载文章,点击查看原文


相关推荐


Python爬虫入门:从零到数据采集
Moniane2025/10/21

Python爬虫基础指南 Python爬虫是自动化获取网络数据的技术,广泛应用于数据采集、市场分析等领域。以下是核心实现步骤: 1. 核心库选择 import requests # 发送HTTP请求 from bs4 import BeautifulSoup # HTML解析 import pandas as pd # 数据存储 2. 基础爬取流程 # 发送请求 response = requests.get("https://example.com/books") res


GPT-2 中的残差权重初始化
mwq301232025/10/19

GPT-2 中的残差权重初始化 1. 概述 在深度残差网络中,每一层的输出都会被加到其输入上。如果不对这些层的权重进行特殊处理,随着网络深度的增加,残差路径上累加的信号方差可能会不断增大,导致数值不稳定和训练发散。 为了解决这个问题,GPT-2 的作者在论文中提出了一种针对残差连接路径上的层进行特殊缩放的初始化方法。 2. 哪些是“残差权重”? 在 GPT-2 的一个 Decoder Block 中,有两个子层的输出会直接被加到残差流(Residual Stream)上。因此,这两层的权重就是我


SpringBoot启动优化7板斧:砍掉70%启动时间的魔鬼实践
皮皮林5512025/10/18

1.延迟初始化:按需加载的智慧 2.组件扫描精准打击:告别无差别扫描 3.JVM参数调优:启动加速的隐藏开关 4.自动配置瘦身:砍掉Spring Boot的"赘肉" 5.类加载优化:让JVM轻装上阵 6.数据库连接优化:断开启动时的枷锁 7.编译优化:AOT与分层编译的威力 综合优化案例:电商平台实战 启动优化检查清单 各优化手段效果对比图 1. 延迟初始化:按需加载的智慧 实践方案: # application.properties spring.main.lazy-initializat


除了JSON/XML,你还应该了解的数据描述语言ASN.1 —— 附《SpringBoot实现ASN.1在线解析工具》
风象南2025/10/17

前言 在日常开发中,我们经常接触JSON、XML等数据格式,但你是否听说过ASN.1?这种在通信、安全、物联网领域广泛使用的数据描述语言。 可能对一部分开发者来说有些陌生,但在特定场景下却有着不可替代的作用。今天,我们就来深入了解一下ASN.1,并用SpringBoot实现一个在线解析工具。 什么是ASN.1? 基本概念 ASN.1(Abstract Syntax Notation One)是一种标准化的数据描述语言,由ITU-T(国际电信联盟)和ISO(国际标准化组织)共同制定。它提供了一种平


Redis(62)如何优化Redis的连接数?
Victor3562025/10/15

优化 Redis 的连接数可以通过以下策略实现: 使用连接池。 合理配置 Redis。 调整操作系统限制。 使用客户端连接复用。 实现批量操作。 分布式架构及读写分离。 以下将详细探讨这些策略,并提供相应代码示例。 1. 使用连接池 连接池可以重用现有连接,减少连接创建和释放的开销,从而提升性能。 Java 示例(Jedis 连接池) 添加 Jedis 依赖: 确保在 pom.xml 文件中添加 Jedis 依赖: <dependency> <groupId>redis.clie


nginx反向代理与缓存功能
Justin_192025/10/14

目录 一,正向代理和反向代理 1,正向代理概述 2,反向代理概述 二,配置实战 1,实现反向代理负载均衡 三,nginx配置跨域cors 1,跨域的定义 2,同源的定义 3,不同源的限制 4,nginx解决跨域的原理 5,案例 一,正向代理和反向代理 1,正向代理概述 正向代理是一个位于客户端和目标服务器之间的代理服务器。为了从目标服务器得到目标,客户端向代理服务器发送一个请求,并且指定目标服务器,再代理向目标服务器转发请求,将得到的目标返回给客户端。 正向


前端人必看!3个技巧让你彻底搞懂JS条件判断与循环
良山有风来2025/10/13

那些年我们踩过的坑 还记得刚学JavaScript的时候吗?遇到复杂的条件判断就头晕,写循环时总是搞不清该用for还是while。最尴尬的是,代码跑起来总是不按预期的来,bug找得头都大了! 说实话,这些困扰我都经历过。但后来我发现,只要掌握了几个核心技巧,这些看似复杂的问题都能轻松解决。 今天我就把压箱底的干货拿出来,用最通俗易懂的方式,带你彻底搞懂JS的条件判断和循环控制。看完这篇文章,你不仅能写出更优雅的代码,还能避开90%新手都会踩的坑! 条件判断:让你的代码更“聪明” 条件判断就像给代


(1)100天python从入门到拿捏
C嘎嘎嵌入式开发2025/10/11

官方文档 python教程 window系统下查看python版本 win+R键打开cmd输入下面的命令 python --version 或者 python -V 类似c语言以.c为后缀,c++以.cpp为后缀,python文件的扩展名以.py结尾 Jupyter Notebook文件以.ipynb为后缀,该类文件通常包含代码、文本和可视化结果 Python 3简介 Python 3 是一种高级、解释型的编程语言,广泛用于 Web 开发、数据科学、机器学习、自


前端梳理体系从常问问题去完善-框架篇(react生态)
大前端helloworld2025/10/9

前言 国庆去趟了杭州,但是人太多了,走路都觉得空气很闷,天气也很热,玩了两天就回宿舍躺了,感觉人太多,看不到风景,而且消费也很高,性价比不是很值得,就呆在公寓,看了两本书,有一本是名著,《呼啸山庄》虽然是写的是爱情,但爱情背后是人性。爱情啊,这个课题本来就是让人很难读懂得,关于爱,也看了一篇文章。关于爱上人渣得,爱上人渣,或是那些求而不得甚至是受制于禁忌的爱,本质上也是在追求这种刺激,或者说正是因为这样的对象能给自己麻木的感官更大的刺激,从而误以为这就是「爱」的本质,就像是人们虽然知道「吊桥效应


Spec-Kit:AI驱动的软件开发全流程管理工具套件
是魔丸啊2025/10/8

Spec-Kit 是一套专为AI辅助开发场景设计的命令行工具套件,提供了从需求规格到代码实现的标准化工作流程。通过8个核心命令,它确保软件开发的每个环节都保持高质量和一致性。 核心理念 端到端管理:覆盖需求规格化、架构设计、任务分解、代码实现全流程 AI优化设计:专为与Claude等AI助手协作而优化 质量保证:内置多层次验证机制 知识沉淀:项目宪法、设计决策完整记录 这篇文章主要是想记录下spec-kit的核心command具体在干嘛。所以翻译了下所有的command(按照实际使用顺序)

首页编辑器站点地图

Copyright © 2025 聚合阅读

License: CC BY-SA 4.0