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

目录

一、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定义](#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地址所标识的主机。

复制代码
#将域名转换为IP地址进行通信

[root@ubuntu-158 ~]# ping www.baidu.com -c1
PING www.a.shifen.com (220.181.111.232) 56(84) bytes of data.
64 bytes from 220.181.111.232: icmp_seq=1 ttl=128 time=9.06 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 9.061/9.061/9.061/0.000 ms

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地址。

复制代码
#Rocky8-手动配置DNS服务器地址
[root@rocky8-153 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
NAME=eth1
IPADDR=10.0.0.163
PREFIX=24
DEVICE=eth1
GATEWAY=10.0.0.2
DNS1=10.0.0.2                #高可用DNS1故障则DNS2工作
DNS2=114.114.114.114

#查看DNS
[root@rocky8-153 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.2         
nameserver 114.114.114.114

#ubuntu
[root@ubuntu-158 netplan]# cat eth0.yaml 
network:
  version: 2
  ethernets:
    eth0:
      #dhcp4: true
      addresses: [10.0.0.158/24]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [223.5.5.5,8.8.8.8]

#查看DNS
[root@ubuntu-158 netplan]# resolvectl status 
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: stub

Link 2 (eth0)
    Current Scopes: DNS
         Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
       DNS Servers: 223.5.5.5 8.8.8.8
     Default Route: yes

[root@ubuntu-158 netplan]# cat /etc/resolv.conf 
...
nameserver 127.0.0.53
options edns0 trust-ad
search 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数据是可以缓存在任意位置,最终目的是以此减少递归查询过程,可以更快的让用户获得请求结果。

复制代码
#Windows系统显示DNS缓存
C:\Users\HhAosocool>ipconfig/displaydns

Windows IP 配置

    www.baidu.com
    ----------------------------------------
    记录名称. . . . . . . : www.baidu.com
    记录类型. . . . . . . : 5
    生存时间. . . . . . . : 25
    数据长度. . . . . . . : 8
    部分. . . . . . . . . : 答案
    CNAME 记录  . . . . . : www.a.shifen.com


    记录名称. . . . . . . : www.a.shifen.com
    记录类型. . . . . . . : 1
    生存时间. . . . . . . : 25
    数据长度. . . . . . . : 4
    部分. . . . . . . . . : 答案
    A (主机)记录  . . . . : 220.181.111.1
    ...

#Windows系统清除DNS缓存
C:\Users\HhAosocool>ipconfig/flushdns

Windows IP 配置

已成功刷新 DNS 解析缓存。

#Centos系统查看DNS缓存
[root@rocky8-153 ~]#nscd -g

#Centos系统清空DNS缓存
[root@rocky8-153 ~]#nscd -g hosts

#Ubuntu系统查看DNS缓存
[root@ubuntu2204-150 ~]# resolvectl statistics

#Ubuntu系统清空DNS缓存
[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要高。

复制代码
#Windows系统中的hosts文件
%windir%\System32\drivers\etc\hosts

#%windir% 是windows 系统中的环境变量写法,表示 Windows 安装目录,上述路径一般是
C:\Windows\System32\drivers\etc

#Linux系统中的hosts文件
[root@rocky8-153 ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@ubuntu2204-150 ~]# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

[root@ubuntu2204-150 ~]# ping ubuntu
PING ubuntu (127.0.1.1) 56(84) bytes of data.
64 bytes from ubuntu (127.0.1.1): icmp_seq=1 ttl=64 time=0.971 ms
64 bytes from ubuntu (127.0.1.1): icmp_seq=2 ttl=64 time=0.036 ms
^C
--- ubuntu ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.036/0.503/0.971/0.467 ms

#直接在hosts中指定域名的IP地址
[root@ubuntu2204-150 ~]# ping www.baidu.com 
PING www.a.shifen.com (220.181.111.1) 56(84) bytes of data.
64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=1 ttl=128 time=22.6 ms
^C
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 22.552/22.552/22.552/0.000 ms

[root@ubuntu2204-150 ~]# vim /etc/hosts
5.5.5.5 www.baidu.com    #添加


[root@ubuntu2204-150 ~]# ping www.baidu.com 
PING www.baidu.com (5.5.5.5) 56(84) bytes of data.
^C
--- www.baidu.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

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对应关系。

复制代码
#安装
[root@ubuntu2204-150 ~]# apt install bind9

#查询来源
[root@ubuntu2204-150 ~]# dpkg -S /usr/bin/dig
bind9-dnsutils: /usr/bin/dig

#命令格式            
dig [@global-server] [domain] [q-type] [q-class] {q-opt}
           {global-d-opt} host [@local-server] {local-d-opt}
           [ host [@local-server] {local-d-opt} [...]]      
            
dig [-t type] name [@SERVER] [query options]            
            
@global-server     #指定DNS服务器
domain             #要查询的域名
q-type             #要查询的记录类型(a,any,mx,ns,soa,hinfo,axfr,txt,...),默认a
q-class            #要查询的解析类型(in|hs|ch),默认 in
q-opt              #选项
d-opt              #查询选项

#q-opt 项
-h                 #显示帮助
-v                 #显示版本号
-4                 #仅查询IPV4的DNS服务器
-6                 #仅查询IPV6的DNS服务器
-b address[#port]  #使用指定客户端IP去查询DNS
-c class           (specify query class)
-f filename        #从文件中获取要查询的域名
-i                 (use IP6.INT for IPv6 reverse lookups)
-k keyfile         (specify tsig key file)
-m                 (enable memory usage debugging)
-p port            #指定DNS服务查询端口
-q name            (specify query name)
-r                 (do not read ~/.digrc)
-t type            #指定要查询的资源记录类型A|NS|AAA|PTR|...
-u                 #以微秒显示打印时间
-x dot-notation    #反向解析
-y [hmac:]name:key (specify named base64 tsig key)

#d-opt 项
+[no]trace         #是否追踪查询过程
+[no]cmd           #是否在查询结果中显示头信息 
+[no]recurse       #是否进行递归解析查询
+[no]all           #是否显示所有信息,如果否,要指明具体显示内容
+[no]answer        #是否显示answer部份
+[no]question      #是否显示question部份
+[no]authority     #是否显示authority部份
+[no]comment       #是否显示comment部份
+[no]stat          #是否显示status部份
+[no]short         #是否只显示关键信息

#常用组合
dig domain
dig @dns-erver domain | dig domain @dns-server
dig -t q-type domain | dig domain q-type
git -x IP | dig -t ptr reverseIP.in-addr.arpa #reverseIP 表示将要查询的IP倒序输出

[root@ubuntu2204-150 ~]# dig

#dig命令版本和参数
; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>>        #此处查询参数为空
;; global options: +cmd            #默认选项,此项表示显示头部软件版本和参数信息

#查询结果
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34552      #QUERY 表示是执行查询操作,NOERROR 表示解析成功
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 16

# opcode: QUERY     执行查询操作
# status:           NOERROR 没有错误,解析成功
# id: 34552         此次查询的ID,在dns协议中,通过ID编号匹配查询请求和返回结果
# flags: qr rd ra   标志位
#
# qr    (query,查询标志,代表查询操作) 
# rd    (recursion desired, 表示客户端希望进行递归查询)
# ra    (recursive available, 表示DNS服务器支持递归查询)
# aa    (Authoritative Answer, 权威回复,如果查询结果由管理域名的域名服务器而不是缓存服务器提供的,则称为权威回复)
#
#QUERY: 1            查询数,表示1个查询,对应下面 QUESTION SECTION中的记录数
#ANSWER: 13          查询结果,表示有13个查询结果,对应下面 ANSWER SECTION 中的记录数
#AUTHORITY: 0        权威域名服务器记录数量,此处表示有0个权威域名服务器
#ADDITIONAL: 16      额外记录数量,此处表示有16个额外记录,此处缺失该部份内容

#选项
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494

#EDNS: Extended DNS 扩展用户数据报文协议
#version:0 协议版本为 0
#flag:; 标记位为空
#udp:65494 数据包大小

#查询域名,此处表示查根域
;; QUESTION SECTION:
;.				IN	NS

#具体查询结果
#第一列是要要询的域名
#第二列是TTL(time to live),表示该记录的缓存时间,单位是秒
#第三列是要查询的信息类型,IN代表类别为IP协议,即Internet
#第四列是要查询的记录类型,NS表示name server,即域名服务器
#第五列表示查询得到的值
;; ANSWER SECTION:
.			5	IN	NS	k.root-servers.net.
.			5	IN	NS	m.root-servers.net.
.			5	IN	NS	c.root-servers.net.
.			5	IN	NS	g.root-servers.net.
.			5	IN	NS	i.root-servers.net.
.			5	IN	NS	f.root-servers.net.
.			5	IN	NS	h.root-servers.net.
.			5	IN	NS	b.root-servers.net.
.			5	IN	NS	j.root-servers.net.
.			5	IN	NS	e.root-servers.net.
.			5	IN	NS	d.root-servers.net.
.			5	IN	NS	a.root-servers.net.
.			5	IN	NS	l.root-servers.net.

;; ADDITIONAL SECTION:
a.root-servers.net.	5	IN	A	198.41.0.4
b.root-servers.net.	5	IN	A	170.247.170.2
c.root-servers.net.	5	IN	A	192.33.4.12
d.root-servers.net.	5	IN	A	199.7.91.13
e.root-servers.net.	5	IN	A	192.203.230.10
f.root-servers.net.	5	IN	A	192.5.5.241
g.root-servers.net.	5	IN	A	192.112.36.4
h.root-servers.net.	5	IN	A	198.97.190.53
i.root-servers.net.	5	IN	A	192.36.148.17
j.root-servers.net.	5	IN	A	192.58.128.30
k.root-servers.net.	5	IN	A	193.0.14.129
l.root-servers.net.	5	IN	A	199.7.83.42
m.root-servers.net.	5	IN	A	202.12.27.33
a.root-servers.net.	5	IN	AAAA	2001:503:ba3e::2:30
b.root-servers.net.	5	IN	AAAA	2801:1b8:10::b

#本次查询统计信息
;; Query time: 7 msec                            #本次查询消耗时长
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)       #DNS服务器为10.0.0.2 端口是53
;; WHEN: Sat Oct 18 08:28:37 UTC 2025            #查询时间
;; MSG SIZE  rcvd: 503                           #返回内容长度为503字节

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

复制代码
[root@ubuntu2204-150 ~]# dig www.baidu.com

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26413
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 10

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		5	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	5	IN	A	220.181.111.232
www.a.shifen.com.	5	IN	A	220.181.111.1

;; AUTHORITY SECTION:
a.shifen.com.		5	IN	NS	ns4.a.shifen.com.
a.shifen.com.		5	IN	NS	ns1.a.shifen.com.
a.shifen.com.		5	IN	NS	ns5.a.shifen.com.
a.shifen.com.		5	IN	NS	ns2.a.shifen.com.
a.shifen.com.		5	IN	NS	ns3.a.shifen.com.

;; ADDITIONAL SECTION:
ns5.a.shifen.com.	5	IN	A	180.76.76.95
ns1.a.shifen.com.	5	IN	A	110.242.68.42
ns2.a.shifen.com.	5	IN	A	220.181.33.32
ns3.a.shifen.com.	5	IN	A	36.155.132.12
ns3.a.shifen.com.	5	IN	A	153.3.238.162
ns4.a.shifen.com.	5	IN	A	14.215.177.229
ns4.a.shifen.com.	5	IN	A	111.20.4.28
ns5.a.shifen.com.	5	IN	AAAA	240e:940:603:a:0:ff:b08d:239d
ns5.a.shifen.com.	5	IN	AAAA	240e:bf:b801:1006:0:ff:b04f:346b

;; Query time: 79 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sat Oct 18 08:55:23 UTC 2025
;; MSG SIZE  rcvd: 359

#短格式
[root@ubuntu2204-150 ~]# dig www.baidu.com +short
www.a.shifen.com.
220.181.111.232
220.181.111.1

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

复制代码
[root@ubuntu2204-150 ~]# dig @114.114.114.114 www.jose-404.com -b 10.0.0.150

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> @114.114.114.114 www.jose-404.com -b 10.0.0.150
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62841
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.jose-404.com.		IN	A

;; ANSWER SECTION:
www.jose-404.com.	600	IN	A	47.94.245.255

;; Query time: 263 msec
;; SERVER: 114.114.114.114#53(114.114.114.114) (UDP)
;; WHEN: Sat Oct 18 09:08:09 UTC 2025
;; MSG SIZE  rcvd: 50

反向解析

复制代码
[root@ubuntu2204-150 ~]# dig -x 47.94.245.255 +nocmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 33985
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;255.245.94.47.in-addr.arpa.	IN	PTR

;; AUTHORITY SECTION:
94.47.in-addr.arpa.	5	IN	SOA	rdns1.alidns.com. dnsmgr.alibaba-inc.com. 2015011340 1800 600 1814400 300

;; Query time: 35 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sat Oct 18 09:08:36 UTC 2025
;; MSG SIZE  rcvd: 126

[root@ubuntu2204-150 ~]# dig -t ptr 255.245.94.47.in-addr.arpa +nocmd
...

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

复制代码
[root@ubuntu2204-150 1018]# vim domain.txt
[root@ubuntu2204-150 1018]# cat domain.txt
www.baidu.com
[root@ubuntu2204-150 1018]# dig -f domain.txt +short
www.a.shifen.com.
220.181.111.1
220.181.111.232

只查询别名解析

复制代码
[root@ubuntu2204-150 1018]# dig -t cnmae www.baidu.com
...

[root@ubuntu2204-150 1018]# dig www.jd.com in cname
...

2、host命令

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

复制代码
host [option] hostname [server]

#常用选项
-a         #显示所有信息
-c         #指定查询类型 HS|CH|IN
-C         #查询SOA
-d         #同 -v
-i IP6.INT reverse lookups
-l lists all hosts in a domain, using AXFR
-m set memory debugging flag (trace|record|usage)
-N changes the number of dots allowed before root lookup is done
-p         #指定端口
-r         #不递归查询
-R specifies number of retries for UDP packets
-s a SERVFAIL response should stop query
-t         #指定查询类型 CNAME|NS|SOA|TXT|DNSKEY|AXFR|...
-T         #使用TCP进行DNS查询
-U         #使用UDP进行DNS查询
-v         #显示执行过程
-V         #显示命令版本
-w         #如果没有查询结果,则阻塞,一直等待
-W N       #等待N秒后超时
-4         #仅查询IPV4的DNS server
-6         #仅查询IPV4的DNS server

[root@ubuntu2204-150 1018]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 220.181.111.1
www.a.shifen.com has address 220.181.111.232
www.a.shifen.com has IPv6 address 2408:871a:2100:1b23:0:ff:b07a:7ebc
www.a.shifen.com has IPv6 address 2408:871a:2100:186c:0:ff:b07e:3fbc

指定DNS服务器

复制代码
[root@ubuntu2204-150 1018]# host www.baidu.com 114.114.114.114
Using domain server:
Name: 114.114.114.114
Address: 114.114.114.114#53
Aliases: 

www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 220.181.111.1
www.a.shifen.com has address 220.181.111.232
www.a.shifen.com has IPv6 address 240e:83:205:381:0:ff:b00f:96a2
www.a.shifen.com has IPv6 address 240e:83:205:1cd:0:ff:b0b8:dee9

显示所有信息

复制代码
[root@ubuntu2204-150 1018]# host -a www.baidu.com 114.114.114.114
Trying "www.baidu.com"
Using domain server:
Name: 114.114.114.114
Address: 114.114.114.114#53
Aliases: 

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24726
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.			IN	ANY

;; ANSWER SECTION:
www.baidu.com.		658	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	88	IN	AAAA	240e:83:205:1cd:0:ff:b0b8:dee9
www.a.shifen.com.	88	IN	AAAA	240e:83:205:381:0:ff:b00f:96a2
www.a.shifen.com.	88	IN	A	220.181.111.232
www.a.shifen.com.	88	IN	A	220.181.111.1

Received 146 bytes from 114.114.114.114#53 in 159 ms

3、nslookup命令

nslookup:(name server lookup),一个命令行下的网络工具,主要用来查询DNS记录,查看域名解析是否正常,也可用来诊断网络问题。

nslookup 支持交互式和非交互式两种执行方式,在Windows系统中和Linux系统中都可以使用。

nslookup 命令不会查询本地 hosts文件中定义的域名和IP对应关系,也不能查询dns的递归或者迭代。

复制代码
nslookup [-option] [name | -] [server]

#非交互式选项
-type              #指定查询类型 A|AAAA|CNAME|...
-query 

set
server IP/DNS Name #指定DNS服务器,可以用IP地址或域名
root
-a
-d
-t type

交互式、非交互式查询

复制代码
[root@ubuntu2204-150 1018]# nslookup
> www.baidu.com
Server:		127.0.0.53            #本次查询使用的DNS服务器
Address:	127.0.0.53#53         #DNS服务器IP地址以及监听端口

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.1
Name:	www.a.shifen.com
Address: 220.181.111.232
Name:	www.a.shifen.com
Address: 240e:83:205:1cd:0:ff:b0b8:dee9
Name:	www.a.shifen.com
Address: 240e:83:205:381:0:ff:b00f:96a2
> 

[root@ubuntu2204-150 1018]# nslookup www.baidu.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.1
Name:	www.a.shifen.com
Address: 220.181.111.232
Name:	www.a.shifen.com
Address: 2408:871a:2100:186c:0:ff:b07e:3fbc
Name:	www.a.shifen.com
Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc

Windos系统中使用

复制代码
#非交互式
C:\Users\HhAosocool>nslookup www.baidu.com
服务器:  UnKnown
Address:  59.67.168.252

非权威应答:
名称:    www.a.shifen.com
Addresses:  2408:871a:2100:1b23:0:ff:b07a:7ebc
          2408:871a:2100:186c:0:ff:b07e:3fbc
          220.181.111.1
          220.181.111.232
Aliases:  www.baidu.com

#交互式
C:\Users\HhAosocool>nslookup
默认服务器:  UnKnown
Address:  59.67.168.252

> www.baidu.com
服务器:  UnKnown
Address:  59.67.168.252

非权威应答:
名称:    www.a.shifen.com
Addresses:  2408:871a:2100:186c:0:ff:b07e:3fbc
          2408:871a:2100:1b23:0:ff:b07a:7ebc
          220.181.111.232
          220.181.111.1
Aliases:  www.baidu.com

>

指定NDS服务器

复制代码
[root@ubuntu2204-150 1018]# nslookup www.taobao.com 114.114.114.114
Server:		114.114.114.114
Address:	114.114.114.114#53

Non-authoritative answer:
www.taobao.com	canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 106.8.159.210
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 106.8.159.211
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 240e:b1:a830:0:3::26
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 240e:b1:a830:0:3::25

[root@ubuntu2204-150 1018]# nslookup 
> server 223.5.5.5
Default server: 223.5.5.5
Address: 223.5.5.5#53
> www.taobao.com
Server:		223.5.5.5
Address:	223.5.5.5#53

Non-authoritative answer:
www.taobao.com	canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 106.8.159.210
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 106.8.159.211
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 240e:b1:a830:0:3::26
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 240e:b1:a830:0:3::25
> 

查看默认配置和选项

复制代码
[root@ubuntu2204-150 1018]# nslookup
> set all
Default server: 127.0.0.53
Address: 127.0.0.53#53

Set options:
  novc			nodebug		nod2
  search		recurse
  timeout = 0		retry = 3	port = 53	ndots = 1
  querytype = A       	class = IN
  srchlist = localdomain
> 

指定查询类型

复制代码
[root@ubuntu2204-150 1018]# nslookup -type=cname www.baidu.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.

Authoritative answers can be found from:
baidu.com	nameserver = ns3.baidu.com.
baidu.com	nameserver = ns7.baidu.com.
baidu.com	nameserver = dns.baidu.com.
baidu.com	nameserver = ns2.baidu.com.
baidu.com	nameserver = ns4.baidu.com.
ns7.baidu.com	internet address = 180.76.76.92
dns.baidu.com	internet address = 110.242.68.134
ns3.baidu.com	internet address = 153.3.238.93
ns3.baidu.com	internet address = 36.155.132.78
ns2.baidu.com	internet address = 220.181.33.31
ns4.baidu.com	internet address = 111.45.3.226
ns4.baidu.com	internet address = 14.215.178.80
ns7.baidu.com	has AAAA address 240e:bf:b801:1002:0:ff:b024:26de
ns7.baidu.com	has AAAA address 240e:940:603:4:0:ff:b01b:589a
dns.baidu.com	has AAAA address 240e:bf:b801:1002:0:ff:b024:26de
ns2.baidu.com	has AAAA address 240e:940:603:4:0:ff:b01b:589a

4、rndc命令

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

复制代码
[root@ubuntu2204-150 1018]# which rndc
/usr/sbin/rndc

#格式
rndc [-b address] [-c config] [-s server] [-p port] [-k key-file ] [-y key] [-r] [-V] command

#常用子命令
status              #查看BIND服务状态
reload              #重载主配置文件和区域解析库文件
reload zonename     #重载区域解析库文件
retransfer zonename #手动启动区域传送,而不管序列号是否增加
notify zonename     #重新对区域传送发通知
reconfig            #重载主配置文件
querylog            #开启或关闭查询日志文件/var/log/message
trace               #递增debug一个级别
trace LEVEL         #指定使用的级别
notrace             #将调试级别设置为 0
flush               #清空DNS服务器的所有缓存记录

5、whois命令

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

复制代码
#安装
[root@ubuntu ~]# apt install whois -y

#格式
whois [OPTION]... OBJECT...

#常用选项
-H                #不显示相关法律声明             
--help            #显示帮助信息
--version         #显示版本信息

#下列选项需要 whois.ript.net 和 RIPE-like 服务器支持
-x                 #精确匹配
-b                 #输出简略内容
-a                 #搜索所有数据库镜像
-s SOURCE[,SOURCE] #从指定数据库中查询
-q [version|sources|types]     #查询服务器信息

[root@ubuntu2204-150 1018]# whois baidu.com
   Domain Name: BAIDU.COM
   Registry Domain ID: 11181110_DOMAIN_COM-VRSN
   Registrar WHOIS Server: whois.markmonitor.com
   Registrar URL: http://markmonitor.com
   Updated Date: 2025-04-08T00:48:16Z
   Creation Date: 1999-10-11T11:05:17Z
   Registry Expiry Date: 2028-10-11T11:05:17Z
   Registrar: MarkMonitor Information Technology (Shanghai) Co., Ltd.
   Registrar IANA ID: 3838
   Registrar Abuse Contact Email: abusecomplaints@markmonitor.com
   Registrar Abuse Contact Phone: +1.2083895740
   ...

三、自建DNS服务

1、ubuntu中的systemd-resolved服务

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

复制代码
[root@ubuntu-158 netplan]# cat eth0.yaml 
network:
  version: 2
  ethernets:
    eth0:
      #dhcp4: true
      addresses: [10.0.0.158/24]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [223.5.5.5,8.8.8.8]

[root@ubuntu-158 netplan]# resolvectl status 
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: stub

Link 2 (eth0)
    Current Scopes: DNS
         Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 223.5.5.5
       DNS Servers: 223.5.5.5 8.8.8.8
     Default Route: yes

[root@ubuntu-158 netplan]# nslookup www.baidu.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.1
Name:	www.a.shifen.com
Address: 220.181.111.232
Name:	www.a.shifen.com
Address: 240e:83:205:381:0:ff:b00f:96a2
Name:	www.a.shifen.com
Address: 240e:83:205:1cd:0:ff:b0b8:dee9

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

复制代码
[root@ubuntu-158 ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Apr 16  2025 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
nameserver 127.0.0.53        #默认DNS配置
options edns0 trust-ad
search .

#修改DNS解析配置文件
[root@ubuntu-158 ~]# vim /etc/resolv.conf 
[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
nameserver 10.0.0.2
options edns0 trust-ad
search .

#测试
[root@ubuntu-158 ~]# nslookup www.baidu.com
Server:		10.0.0.2
Address:	10.0.0.2#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 110.242.69.21
...

#重启网络服务
[root@ubuntu-158 ~]# netplan apply

#查看
[root@ubuntu-158 ~]# cat /etc/resolv.conf | grep -Ev '^(#|$)'
nameserver 127.0.0.53       #文件内容恢复默认
options edns0 trust-ad
search .

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

复制代码
#-方法1
#修改软链接文件指向,保证永久生效
[root@ubuntu-158 ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Apr 16  2025 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

[root@ubuntu-158 ~]# rm -rf /etc/resolv.conf 

[root@ubuntu-158 ~]# ls /run/systemd/resolve/
io.systemd.Resolve  io.systemd.Resolve.Monitor  resolv.conf  stub-resolv.conf
#resolv.conf:传统DNS配置
#stub-resolv.conf:systemd-resolved存根配置(动态生成),不可直接编辑由服务管理

[root@ubuntu-158 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'

[root@ubuntu-158 ~]# cat /etc/resolv.conf 
...
nameserver 223.5.5.5
nameserver 8.8.8.8
search .

[root@ubuntu-158 ~]# nslookup www.baidu.com
Server:		223.5.5.5
Address:	223.5.5.5#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.232
...

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

复制代码
#修改网卡配置,注释掉DNS配置
[root@ubuntu-158 ~]# vim /etc/netplan/eth0.yaml
[root@ubuntu-158 ~]# cat /etc/netplan/eth0.yaml 
network:
  version: 2
  ethernets:
    eth0:
      #dhcp4: true
      addresses: [10.0.0.158/24]
      gateway4: 10.0.0.2
      nameservers:
        search: [baidu.com]
        #addresses: [223.5.5.5,8.8.8.8]

#重启网络服务
[root@ubuntu-158 ~]# netplan apply 

#查看DNS
...
# No DNS servers known.
search baidu.com

#无法使用DNS解析服务
[root@ubuntu-158 ~]# ping www.baidu.com
ping: www.baidu.com: Temporary failure in name resolution

[root@ubuntu-158 ~]# nslookup www.baidu.com
;; communications error to ::1#53: connection refused
;; communications error to ::1#53: connection refused
;; communications error to ::1#53: connection refused
;; communications error to 127.0.0.1#53: connection refused
;; no servers could be reached

设置全局DNS

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

复制代码
#此方法可以不用删除软连接
[root@ubuntu2204-150 netplan]# cat 50-cloud-init.yaml 
network:
    ethernets:
        ens33:
            dhcp4: true
    version: 2

[root@ubuntu2204-150 netplan]# cat /etc/resolv.conf 
...
nameserver 127.0.0.53
options edns0 trust-ad
search localdomain

[root@ubuntu2204-150 ~]# vim /etc/systemd/resolved.conf 
...
DNS=223.5.5.5 223.6.6.6
DNSStubListener=no            #禁用DNS存根

#重启服务
[root@ubuntu2204-150 ~]# systemctl start systemd-resolved.service

#查看
[root@ubuntu2204-150 ~]# cat /etc/resolv.conf
......
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 10.0.0.2           #网卡配置中DHCP自动分配
search localdomain

#测试
[root@ubuntu2204-150 ~]# nslookup www.baidu.com
Server:		223.5.5.5
Address:	223.5.5.5#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.232
...

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

复制代码
[root@ubuntu2204-150 netplan]# cat 50-cloud-init.yaml 
network:
    ethernets:
        ens33:
          #dhcp4: true
          addresses: [10.0.0.150/24]
          gateway4: 114.114.114.114
          nameservers:
            addresses: [8.8.8.8]
    version: 2

#重启网络服务
[root@ubuntu2204-150 netplan]# netplan  apply

#查看
[root@ubuntu2204-150 netplan]# cat /etc/resolv.conf 
...
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 8.8.8.8
search .

2、bind安装和配置

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

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

复制代码
[root@ubuntu-157 ~]# ss -tlpn
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process                                                  
LISTEN  0       4096     127.0.0.53%lo:53          0.0.0.0:*      users:(("systemd-resolve",pid=520,fd=15))               
LISTEN  0       4096           0.0.0.0:22          0.0.0.0:*      users:(("sshd",pid=1526,fd=3),("systemd",pid=1,fd=97))  
LISTEN  0       4096        127.0.0.54:53          0.0.0.0:*      users:(("systemd-resolve",pid=520,fd=17))               
LISTEN  0       128          127.0.0.1:6010        0.0.0.0:*      users:(("sshd-session",pid=1640,fd=9))                  
LISTEN  0       128              [::1]:6010           [::]:*      users:(("sshd-session",pid=1640,fd=8))                  
LISTEN  0       4096              [::]:22             [::]:*      users:(("sshd",pid=1526,fd=4),("systemd",pid=1,fd=98)) 

#安装
[root@ubuntu-157 ~]# apt install bind9 -y

#启动服务
[root@ubuntu-157 ~]# systemctl start named.service 

#查看
[root@ubuntu-157 ~]# ss -tlpn
State  Recv-Q  Send-Q                      Local Address:Port   Peer Address:Port Process                                                  
LISTEN 0       4096                        127.0.0.53%lo:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=15))               
LISTEN 0       4096                              0.0.0.0:22          0.0.0.0:*     users:(("sshd",pid=1526,fd=3),("systemd",pid=1,fd=253)) 
LISTEN 0       10                              127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=32))                        
LISTEN 0       10                              127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=30))                        
LISTEN 0       4096                           127.0.0.54:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=17))               
LISTEN 0       5                               127.0.0.1:953         0.0.0.0:*     users:(("named",pid=2231,fd=47))                        
LISTEN 0       128                             127.0.0.1:6010        0.0.0.0:*     users:(("sshd-session",pid=1640,fd=9))                  
LISTEN 0       10                             10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=37))                        
LISTEN 0       10                             10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=36))                        
LISTEN 0       128                                 [::1]:6010           [::]:*     users:(("sshd-session",pid=1640,fd=8))                  
LISTEN 0       4096                                 [::]:22             [::]:*     users:(("sshd",pid=1526,fd=4),("systemd",pid=1,fd=254)) 
LISTEN 0       5                                   [::1]:953            [::]:*     users:(("named",pid=2231,fd=48))                        
LISTEN 0       10                                  [::1]:53             [::]:*     users:(("named",pid=2231,fd=40))                        
LISTEN 0       10                                  [::1]:53             [::]:*     users:(("named",pid=2231,fd=41))                        
LISTEN 0       10       [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=44))                        
LISTEN 0       10       [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=45))

测试

复制代码
#网卡中配置
[root@ubuntu-157 ~]# cat /etc/netplan/50-cloud-init.yaml 
network:
  version: 2
  ethernets:
    ens33:
      #dhcp4: true
      addresses: [10.0.0.157/24]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [127.0.0.1]

[root@ubuntu-157 ~]# cat /etc/resolv.conf 
...
nameserver 127.0.0.53
options edns0 trust-ad
search .

[root@ubuntu-157 ~]# rm -rf /etc/resolv.conf 
[root@ubuntu-157 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
[root@ubuntu-157 ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 32 Oct 18 20:02 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf

#重启网络服务
[root@ubuntu-157 ~]# netplan apply 

//去掉全局配置,如果有修改则在该文件中注释掉DNS行
//[root@ubuntu-157 ~]# vim /etc/systemd/resolved.conf 
//systemctl restart systemd-resolved.service

#查看
[root@ubuntu-157 ~]# cat /etc/resolv.conf 
...
nameserver 127.0.0.1
search .

[root@ubuntu-157 ~]# ss -unlp
State  Recv-Q Send-Q                    Local Address:Port   Peer Address:Port Process                                    
UNCONN 0      0                            10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=35))          
UNCONN 0      0                            10.0.0.157:53          0.0.0.0:*     users:(("named",pid=2231,fd=34))          
UNCONN 0      0                             127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=29))          
UNCONN 0      0                             127.0.0.1:53          0.0.0.0:*     users:(("named",pid=2231,fd=28))          
UNCONN 0      0                            127.0.0.54:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=16)) 
UNCONN 0      0                         127.0.0.53%lo:53          0.0.0.0:*     users:(("systemd-resolve",pid=520,fd=14)) 
UNCONN 0      0                             127.0.0.1:323         0.0.0.0:*     users:(("chronyd",pid=991,fd=5))          
UNCONN 0      0                                 [::1]:53             [::]:*     users:(("named",pid=2231,fd=38))          
UNCONN 0      0                                 [::1]:53             [::]:*     users:(("named",pid=2231,fd=39))          
UNCONN 0      0      [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=42))          
UNCONN 0      0      [fe80::20c:29ff:fe88:bdf8]%ens33:53             [::]:*     users:(("named",pid=2231,fd=43))          
UNCONN 0      0                                 [::1]:323            [::]:*     users:(("chronyd",pid=991,fd=6)) 

#ubuntu测试
[root@ubuntu-157 ~]# nslookup www.baidu.com
Server:		127.0.0.1
Address:	127.0.0.1#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 110.242.69.21
...

[root@ubuntu-157 ~]# dig www.baidu.com @10.0.0.157

; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> www.baidu.com @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4726
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ac883733eda129f40100000068f386e0ca9b14d098b487e3 (good)
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		1200	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	120	IN	A	110.242.70.57
www.a.shifen.com.	120	IN	A	110.242.69.21

;; Query time: 422 msec
;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
;; WHEN: Sat Oct 18 20:24:00 CST 2025
;; MSG SIZE  rcvd: 129

[root@ubuntu-158 ~]# dig www.baidu.com @10.0.0.157

; <<>> DiG 9.20.4-3ubuntu1.2-Ubuntu <<>> www.baidu.com @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18291
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 694b1d7f1aa01bbb0100000068f387263255b8670ea96b6b (good)
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		1130	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	50	IN	A	110.242.70.57
www.a.shifen.com.	50	IN	A	110.242.69.21

;; Query time: 2 msec
;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
;; WHEN: Sat Oct 18 20:25:09 CST 2025
;; MSG SIZE  rcvd: 129

#rocky测试
#配置DNS解析服务
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.2

[root@rocky8-153 ~]#vim /etc/resolv.conf 
nameserver 10.0.0.157

[root@rocky8-153 ~]#nslookup www.baidu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 110.242.69.21
...

例如: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个根域名服务器地址,写在文件中

复制代码
[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones 
// prime the server with knowledge of the root servers
zone "." {
	type hint;
	file "/usr/share/dns/root.hints";
};
...

[root@ubuntu-157 ~]# cat /usr/share/dns/root.hints | grep -v '^;'
.                        3600000      NS    A.ROOT-SERVERS.NET.
A.ROOT-SERVERS.NET.      3600000      A     198.41.0.4
A.ROOT-SERVERS.NET.      3600000      AAAA  2001:503:ba3e::2:30
.                        3600000      NS    B.ROOT-SERVERS.NET.
B.ROOT-SERVERS.NET.      3600000      A     170.247.170.2
B.ROOT-SERVERS.NET.      3600000      AAAA  2801:1b8:10::b
.                        3600000      NS    C.ROOT-SERVERS.NET.
C.ROOT-SERVERS.NET.      3600000      A     192.33.4.12
C.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2::c
.                        3600000      NS    D.ROOT-SERVERS.NET.
D.ROOT-SERVERS.NET.      3600000      A     199.7.91.13
D.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2d::d
.                        3600000      NS    E.ROOT-SERVERS.NET.
E.ROOT-SERVERS.NET.      3600000      A     192.203.230.10
E.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:a8::e
.                        3600000      NS    F.ROOT-SERVERS.NET.
F.ROOT-SERVERS.NET.      3600000      A     192.5.5.241
F.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:2f::f
.                        3600000      NS    G.ROOT-SERVERS.NET.
G.ROOT-SERVERS.NET.      3600000      A     192.112.36.4
G.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:12::d0d
.                        3600000      NS    H.ROOT-SERVERS.NET.
H.ROOT-SERVERS.NET.      3600000      A     198.97.190.53
H.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:1::53
.                        3600000      NS    I.ROOT-SERVERS.NET.
I.ROOT-SERVERS.NET.      3600000      A     192.36.148.17
I.ROOT-SERVERS.NET.      3600000      AAAA  2001:7fe::53
.                        3600000      NS    J.ROOT-SERVERS.NET.
J.ROOT-SERVERS.NET.      3600000      A     192.58.128.30
J.ROOT-SERVERS.NET.      3600000      AAAA  2001:503:c27::2:30
.                        3600000      NS    K.ROOT-SERVERS.NET.
K.ROOT-SERVERS.NET.      3600000      A     193.0.14.129
K.ROOT-SERVERS.NET.      3600000      AAAA  2001:7fd::1
.                        3600000      NS    L.ROOT-SERVERS.NET.
L.ROOT-SERVERS.NET.      3600000      A     199.7.83.42
L.ROOT-SERVERS.NET.      3600000      AAAA  2001:500:9f::42
.                        3600000      NS    M.ROOT-SERVERS.NET.
M.ROOT-SERVERS.NET.      3600000      A     202.12.27.33
M.ROOT-SERVERS.NET.      3600000      AAAA  2001:dc3::35

其他机器DNS指向本机

复制代码
[root@rocky-154 ~]#hostname -I
10.0.0.154 

[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

[root@rocky-154 ~]#dig www.baidu.com

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21290
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 8d73d3552b84bed60100000068f39a70039beadd5ee9208f (good)
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		1152	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	120	IN	A	220.181.111.1
www.a.shifen.com.	120	IN	A	220.181.111.232

;; Query time: 811 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Sat Oct 18 21:47:28 CST 2025
;; MSG SIZE  rcvd: 129

3、相关配置说明

①bind配置文件及解析流程

复制代码
[root@ubuntu-157 ~]# dpkg -L bind9
......
/etc/bind
/etc/bind/bind.keys
/etc/bind/db.0                     #db.* 名具体解析规则文件
/etc/bind/db.127
/etc/bind/db.255
/etc/bind/db.empty
/etc/bind/db.local
/etc/bind/named.conf               #主配置文件
/etc/bind/named.conf.default-zones #中间配置文件,该文件中定义了域名和具体解析规则文件的对应关系
/etc/bind/named.conf.local         #中间配置文件,引用/etc/bind/zones.rfc1918,被注释
/etc/bind/named.conf.options       #bind配置项
/etc/bind/zones.rfc1918            #中间配置文件,该文件中定义了域名和具体解析规则文件的对应关系
主配置文件
复制代码
[root@ubuntu-157 ~]# cat /etc/bind/named.conf
// This is the primary configuration file for the BIND DNS server named.
//
// Please read /usr/share/doc/bind9/README.Debian for information on the
// structure of BIND configuration files in Debian, *BEFORE* you customize
// this configuration file.
//
// If you are just adding zones, please do that in /etc/bind/named.conf.local

include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";                #不关注
include "/etc/bind/named.conf.default-zones";

#帮助
man named.conf

[root@ubuntu-157 ~]# cat /etc/bind/named.conf.local
//
// Do any local configuration here
//

// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";
选项配置文件
复制代码
[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options
options {
	directory "/var/cache/bind";

	// If there is a firewall between you and nameservers you want
	// to talk to, you may need to fix the firewall to allow multiple
	// ports to talk.  See http://www.kb.cert.org/vuls/id/800113

	// If your ISP provided one or more IP addresses for stable 
	// nameservers, you probably want to use them as forwarders.  
	// Uncomment the following block, and insert the addresses replacing 
	// the all-0's placeholder.

	// forwarders {
	// 	0.0.0.0;
	// };

	//========================================================================
	// If BIND logs error messages about the root key being expired,
	// you will need to update your keys.  See https://www.isc.org/bind-keys
	//========================================================================
	dnssec-validation auto;

	listen-on-v6 { any; };
};

[root@rocky8-153 ~]#cat /etc/named.conf 
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

options {
	listen-on port 53 { 127.0.0.1; };
	listen-on-v6 port 53 { ::1; };
	directory 	"/var/named";
	dump-file 	"/var/named/data/cache_dump.db";
	statistics-file "/var/named/data/named_stats.txt";
	memstatistics-file "/var/named/data/named_mem_stats.txt";
	secroots-file	"/var/named/data/named.secroots";
	recursing-file	"/var/named/data/named.recursing";
	allow-query     { localhost; };

	/* 
	 - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
	 - If you are building a RECURSIVE (caching) DNS server, you need to enable 
	   recursion. 
	 - If your recursive DNS server has a public IP address, you MUST enable access 
	   control to limit queries to your legitimate users. Failing to do so will
	   cause your server to become part of large scale DNS amplification 
	   attacks. Implementing BCP38 within your network would greatly
	   reduce such attack surface 
	*/
	recursion yes;

	dnssec-enable yes;
	dnssec-validation yes;

	managed-keys-directory "/var/named/dynamic";

	pid-file "/run/named/named.pid";
	session-keyfile "/run/named/session.key";

	/* https://fedoraproject.org/wiki/Changes/CryptoPolicy */
	include "/etc/crypto-policies/back-ends/bind.config";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
	type hint;
	file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

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

|---------|------------|---------------------------------|
| 配置 | 配置字段 | 备注 |
| 全局配置 | options{}; | 全局配置选项 |
| 日志子系统配置 | logging{}; | 运行日志 |
| 网络自定义集合 | acl | 将某个网段或某个具体IP地址定义在一个集合里面 |
| 视图 | view | 配合acl将不同的请求来源用不同的解析规则返回,实现智能DNS |

复制代码
#常用全局配置选项
options {

 #此配置表示DNS服务只监听了本机127.0.0.1的53端口,如果对外提供DNS服务,可以将此行注释或值改成any
 listen-on port 53 { 127.0.0.1; }; 
 
 #监听IPV6的53端口,配置方法同上
 listen-on-v6 port 53 { ::1; };
 
 #监听本机所有IPV6地址,不想监听IPV6地址,可以将 any 改成 none
 listen-on-v6 { any; };
 
 #此配置表示仅本机可以使用DNS服务的解析查询功能,如果对外提供DNS服务,可以将此行注释或值改成any
 allow-query     { localhost; }; 

 #是否启用加密验证,在使用转发的时候,将此项改为 no
 dnssec-validation auto;

 #转发服务器
 forwarders { 10.0.0.207; }; 
    
 #转发策略
 forward first; 

};
中间配置文件
复制代码
zone "ZONE_NAME" IN {                     #IN 可以省略不写
    type {master|slave|hint|forward};     #类型 master,slave 用于DNS主从,forward表示转发
    file "file_path";                     #具体解析规则文件路径
};

#该文件中定义了要解析的域名与具体解析规则之间的对应关系

[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones
// prime the server with knowledge of the root servers
zone "." {
	type hint;
	file "/usr/share/dns/root.hints";
};

// be authoritative for the localhost forward and reverse zones, and for
// broadcast zones as per RFC 1912

zone "localhost" {
	type master;
	file "/etc/bind/db.local";
};

zone "127.in-addr.arpa" {
	type master;
	file "/etc/bind/db.127";
};

zone "0.in-addr.arpa" {
	type master;
	file "/etc/bind/db.0";
};

zone "255.in-addr.arpa" {
	type master;
	file "/etc/bind/db.255";
};
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同子网掩码运算后得到的网段 |

复制代码
#ACL格式
acl ACL_NAME{
    IP;
    IP;
    NET/NETMAST;
    NET/NETMAST;
    ......
};

#范例
acl test_net{
    192.168.0.0/24;
    172.16.0.0/24;
};
acl prod_net{
    10.0.0.0/24;
};
acl other_net{
    any;
};
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的,如果请求被命中,就进入解析,不再向后匹配。

复制代码
#格式 
view VIEW_NAME{
    match-clients { acl_name; };
    zone "domain" IN {
    type mater;
    file "domain.zone";
    };
    include "/etc/named.rfc1912.zones.domain"
};

#范例
view prod_view{
    match-clients { prod_net; };
    include "/etc/named.rfc1912.zones";
    include "/etc/named.rfc1912.zones.prod";
};
view other_view{
    match-clients {other_net;};
    include "/etc/named.rfc1912.zones";
};
具体解析规则

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

复制代码
#例子
[root@ubuntu-157 ~]# cat /etc/bind/db.0
;
; BIND reverse data file for "this host on this network" zone
;
$TTL	604800
@	IN	SOA	localhost. root.localhost. (
			      1		; Serial
			 604800		; Refresh
			  86400		; Retry
			2419200		; Expire
			 604800 )	; Negative Cache TTL
;
@	IN	NS	localhost.
Resource Recode定义
复制代码
#RR格式
NAME   TTL   CLASS   TYPE   VALUE

#字段说明
NAME          #资源记录名称,根据TYPE不一样,写法会有不同
TTL           #缓存有效期,默认单位是秒,也可以加单位{ M(分), H(时), D(天), W(周) }
CLASS         #资源记录类别
TYPE          #记录类型,具体值见下表
VALUE         #此记录对应的具体值,根据TYPE不一样,写法会有不同

CLASS字段说明

|----|-------------------------------------|
| 值 | 说明 |
| IN | 标准的IPV4/6 Dns记录,常用 |
| CS | CSNET,已被废弃 |
| CH | CHAOS,一种早期协议,查询DNS服务器元数据(如版本、主机信息等) |
| HS | Hesiod,目录服务协议 |

TYPE字段说明

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

②A、AAAA记录

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

AAAA 记录是IPV6地址的主机记录,是将主机域名解析到指定物理机的IPV6地址上。

复制代码
www.linux-magedu.com.   86400 IN A  10.0.0.210
blog.linux-magedu.com.  86400 IN A  10.0.0.206 
blog.linux-magedu.com.  86400 IN A  10.0.0.210     #如果有多条A记录,且有A记录的IP与DNS机器IP相同,则优先返回
*.linux-magedu.com.     86400 IN A  10.0.0.210     #泛解析,匹配所有以 linuxmagedu.com结束的域名或主机名
linux-magedu.com.       86400 IN A  10.0.0.167 
@                       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记录

复制代码
linux-magedu.com.      86400   IN MX  10 mx1.linux-magedu.com.
linux-magedu.com.      86400   IN MX  20 mx2.linux-magedu.com.

mx1.linux-magedu.com.  86400   IN A   10.0.0.206
mx2.linux-magedu.com.  86400   IN A   10.0.0.208

⑤NS记录

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

复制代码
inux-magedu.com.        86400 IN NS dns1.linux-magedu.com.
linux-magedu.com.       86400 IN NS dns2.linux-magedu.com.

dns1.linux-magedu.com.  86400 IN A  10.0.0.206
dns2.linux-magedu.com.  86400 IN A  10.0.0.208

⑥PTR记录

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

复制代码
#A记录
blog.linux-magedu.com.    86400 IN A   10.0.0.167

#与其对应的PTR记录
#IP地址反着写,后面跟 .in-addr.arpa.
167.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服务器名称 |
| 服务器管理员邮箱 | 邮箱中的@要写成 . |
| 当前数据库的版本号 | 主从服务器要同步数据,此字段就是数据更新的标识 |
| 从服务器拉取数据的时间间隔 | 从服务器拉取数据的时间间隔 |
| 从服务器同步失败后重试时间间隔 | 上次同步失败后,间隔多久重试 |
| 从服务器同步失败超过多长时间从服务器失败 | 同步失败时长超过此值,则认为从服务器数据无效 |
| 不存在的记录缓存时长 | 当查询一个不存在的解析记录时,该记录在指定时间内直接返回不存在 |

复制代码
linux-magedu.com.   86400 IN SOA linux-dns. admin.linux-dns.com. (123 3H 15M 1D 1W)

#服务器名 linux-dns
#管理员邮箱   amdin@linux-dns.com
#版本号 123
#从服务器更新间隔 3H
#失败重试间隔 15M
#从服务器数据失效时长 1D
#无效记录缓存时长     1W

⑧TXT记录

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

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

复制代码
_dnsauth.linux-magedu.com.      86400 IN TXT 
2024dtetmvzwclwf6wsl0y6jcpvwga2wkibgyb1a103yd7re2

4、实现DNS自解析主机

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

在DNS SERVER上实现域名解析

新增zones

objectivec 复制代码
[root@ubuntu-157 bind]# vim /etc/bind/named.conf.default-zones 

[root@ubuntu-157 bind]# cat /etc/bind/named.conf.default-zones 
....
zone "linux-magedu.com" IN {
	type master;
	file "/etc/bind/db.linux-magedu.com";        #具体解析规则文件
};

设置具体解析规则

objectivec 复制代码
[root@ubuntu-157 ~]# cd /etc/bind/
[root@ubuntu-157 bind]# ls
db.0    db.empty    named.conf.default-zones  rndc.key
db.127  db.local    named.conf.local          zones.rfc1918
db.255  named.conf  named.conf.options

[root@ubuntu-157 bind]# vim /etc/bind/db.linux-magedu.com
linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 123 3H 15M 1D 1W )

linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.

dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
dns2.linux-magedu.com.  86400  IN  A   10.0.0.157

www.linux-magedu.com.  86400  IN  A   10.0.0.153
w.linux-magedu.com.    86400  IN  A   1.2.3.4
bbs.linux-magedu.com.  86400  IN  A   11.11.11.11

#修改权限,修改属主属组(可选)
[root@ubuntu ~]# chmod 644 /etc/bind/db.linux-magedu.com 
[root@ubuntu ~]# chown root.root /etc/bind/db.linux-magedu.com

#语法检查
[root@ubuntu-157 bind]# named-checkzone linux-magedu.com ./db.linux-magedu.com    #域名 对应解析规则文件
zone linux-magedu.com/IN: loaded serial 123
OK

#重载生效
[root@ubuntu-157 bind]# rndc reload
server reload successful

服务端测试

objectivec 复制代码
[root@ubuntu-157 bind]# dig www.linux-magedu.com

; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> www.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41337
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 90a348da1df4980b0100000068f4a05b47bd9983787466eb (good)
;; QUESTION SECTION:
;www.linux-magedu.com.		IN	A

;; ANSWER SECTION:
www.linux-magedu.com.	86400	IN	A	10.0.0.153

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Sun Oct 19 16:24:59 CST 2025
;; MSG SIZE  rcvd: 93

[root@ubuntu-157 bind]# dig w.linux-magedu.com

; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> w.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45549
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 380717e24e4d2d960100000068f4a0802440deb31b424730 (good)
;; QUESTION SECTION:
;w.linux-magedu.com.		IN	A

;; ANSWER SECTION:
w.linux-magedu.com.	86400	IN	A	1.2.3.4

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Sun Oct 19 16:25:36 CST 2025
;; MSG SIZE  rcvd: 91

[root@ubuntu-157 bind]# dig bbs.linux-magedu.com

; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> bbs.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1305
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: b46e28297f5c3b5e0100000068f4a0874befd4097972c809 (good)
;; QUESTION SECTION:
;bbs.linux-magedu.com.		IN	A

;; ANSWER SECTION:
bbs.linux-magedu.com.	86400	IN	A	11.11.11.11

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Sun Oct 19 16:25:43 CST 2025
;; MSG SIZE  rcvd: 93

[root@ubuntu-157 bind]# ping www.linux-magedu.com 
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from 10.0.0.153: icmp_seq=1 ttl=64 time=4.19 ms
64 bytes from 10.0.0.153: icmp_seq=2 ttl=64 time=0.955 ms
64 bytes from 10.0.0.153: icmp_seq=3 ttl=64 time=0.746 ms
64 bytes from 10.0.0.153: icmp_seq=4 ttl=64 time=0.495 ms
^C
--- www.linux-magedu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3024ms
rtt min/avg/max/mdev = 0.495/1.597/4.194/1.507 ms
[root@ubuntu-157 bind]# ping w.linux-magedu.com 
PING w.linux-magedu.com (1.2.3.4) 56(84) bytes of data.
^C
--- w.linux-magedu.com ping statistics ---
23 packets transmitted, 0 received, 100% packet loss, time 22559ms

在客户端上配置并测试

objectivec 复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.2

#指定DNS服务器的IP地址
[root@rocky-154 ~]#vim /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

[root@rocky-154 ~]#dig www.linux-magedu.com

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52881
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 55815c97d605789a0100000068f4a0e7d39a4f07008f1eb3 (good)
;; QUESTION SECTION:
;www.linux-magedu.com.		IN	A

;; ANSWER SECTION:
www.linux-magedu.com.	86400	IN	A	10.0.0.153

;; Query time: 4 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Sun Oct 19 16:27:18 CST 2025
;; MSG SIZE  rcvd: 93

[root@rocky-154 ~]#nslookup www.linux-magedu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Name:	www.linux-magedu.com
Address: 10.0.0.153

[root@rocky-154 ~]#host bbs.linux-magedu.com
bbs.linux-magedu.com has address 11.11.11.11

[root@rocky-154 ~]#ping w.linux-magedu.com
PING w.linux-magedu.com (1.2.3.4) 56(84) bytes of data.
^C
--- w.linux-magedu.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1056ms

在web服务主机上实现网站

objectivec 复制代码
[root@rocky8-153 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.2

[root@rocky8-153 ~]#yum install nginx

[root@rocky8-153 ~]#systemctl restart nginx.service
[root@rocky8-153 ~]#systemctl disable --now  firewalld

[root@rocky8-153 ~]#cd /usr/share/nginx/html

[root@rocky8-153 html]#ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@rocky8-153 html]#mv index.html index.html.bak

[root@rocky8-153 html]# vim index.html
<h1>this is www.linux-magedu.com</h1>

#本机测试
[root@rocky8-153 html]#curl 127.1
<h1>this is www.linux-magedu.com</h1>

#客户端测试
[root@rocky-154 ~]#curl www.linux-magedu.com
<h1>this is www.linux-magedu.com</h1>

在windows物理机上测试

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

objectivec 复制代码
C:\Users\HhAosocool>ping www.linux-magedu.com

正在 Ping www.linux-magedu.com [10.0.0.153] 具有 32 字节的数据:
来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64
来自 10.0.0.153 的回复: 字节=32 时间<1ms TTL=64

10.0.0.153 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

5、实现DNS自解析的主从

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

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

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

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

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

配置DNS主从

DNS SLAVE配置

vbnet 复制代码
#安装
[root@ubuntu2204-150 ~]# apt install bind9 -y

#关闭systemd-resolved服务
[root@ubuntu2204-150 ~]# systemctl stop systemd-resolved.service 

#解决ubuntu中的systemd-resolved服务DNS相关问题
[root@ubuntu2204-150 ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Sep 11  2024 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
[root@ubuntu2204-150 ~]# rm -rf /etc/resolv.conf 
[root@ubuntu2204-150 ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
[root@ubuntu2204-150 ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 32 Oct 19 09:35 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf

[root@ubuntu2204-150 ~]# vim /etc/resolv.conf
nameserver 127.0.0.1

#新增主从同步配置
[root@ubuntu2204-150 ~]# vim /etc/bind/named.conf.default-zones 
...
zone "linux-magedu.com" IN {
	type slave;
	masters {10.0.0.157;};
	file "/etc/bind/slaves/db.linux-magedu.com";
};

#创建目录
[root@ubuntu2204-150 ~]# mkdir /etc/bind/slaves
[root@ubuntu2204-150 ~]# chmod g=rwx /etc/bind/slaves

#/etc/apparmor.d/usr.sbin.named文件第19行后添加
[root@ubuntu2204-150 ~]# vim /etc/apparmor.d/usr.sbin.named 
/etc/bind/slaves/** rwk,
/etc/bind/slaves/ rwk,

#重启服务
[root@ubuntu2204-150 ~]# systemctl restart apparmor.service 

[root@ubuntu2204-150 ~]# systemctl restart named

#解析文件同步
[root@ubuntu2204-150 ~]# ls /etc/bind/slaves
db.linux-magedu.com

#文件格式与MASTER上不一样,不再是文本文件
[root@ubuntu2204-150 ~]# file /etc/bind/slaves/db.linux-magedu.com 
/etc/bind/slaves/db.linux-magedu.com: data

DNS MASTER配置

objectivec 复制代码
#添加从DNS配置,此配置是保证能推送数据到从节点
[root@ubuntu-157 ~]# cat /etc/bind/db.linux-magedu.com 
linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 123 3H 15M 1D 1W )

linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.

dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
dns2.linux-magedu.com.  86400  IN  A   10.0.0.150        #修改为从DNS服务器IP地址

www.linux-magedu.com.  86400  IN  A   10.0.0.153
w.linux-magedu.com.    86400  IN  A   1.2.3.4
bbs.linux-magedu.com.  86400  IN  A   11.11.11.11

客户端测试

objectivec 复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

#默认DNS返回
[root@rocky-154 ~]#nslookup www.linux-magedu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Name:	www.linux-magedu.com
Address: 10.0.0.153

#指定从DNS地址
[root@rocky-154 ~]#dig bbs.linux-magedu.com @10.0.0.150

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> bbs.linux-magedu.com @10.0.0.150
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29632
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 9135d08c270822ff0100000068f4d7986dd5213e63826b0e (good)
;; QUESTION SECTION:
;bbs.linux-magedu.com.		IN	A

;; ANSWER SECTION:
bbs.linux-magedu.com.	86400	IN	A	11.11.11.11

;; Query time: 1 msec
;; SERVER: 10.0.0.150#53(10.0.0.150)
;; WHEN: Sun Oct 19 20:20:39 CST 2025
;; MSG SIZE  rcvd: 93

[root@rocky-154 ~]#host w.linux-magedu.com
w.linux-magedu.com has address 1.2.3.4

#关闭DNS MASTER上的服务
[root@ubuntu-157 bind]# systemctl stop named.service

[root@ubuntu-157 bind]# systemctl is-active named
inactive

#测试
[root@rocky-154 ~]#ping www.linux-magedu.com
ping: www.linux-magedu.com: Name or service not known

#默认DNS无法解析
[root@rocky-154 ~]#nslookup bbs.linux-magedu.com
;; connection timed out; no servers could be reached

#客户端添加从DNS
[root@rocky-154 ~]#vim /etc/resolv.conf 
[root@rocky-154 ~]#cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157
nameserver 10.0.0.150

#再次测试
[root@rocky-154 ~]#ping www.linux-magedu.com
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=1 ttl=64 time=0.905 ms
64 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=2 ttl=64 time=0.499 ms
64 bytes from 10.0.0.153 (10.0.0.153): icmp_seq=3 ttl=64 time=0.637 ms
^C
--- www.linux-magedu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2026ms
rtt min/avg/max/mdev = 0.499/0.680/0.905/0.169 ms

#DNS SLAVE能提供解析
[root@rocky-154 ~]#nslookup bbs.linux-magedu.com
Server:		10.0.0.150
Address:	10.0.0.150#53

Name:	bbs.linux-magedu.com
Address: 11.11.11.11

主从DNS数据同步测试

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

objectivec 复制代码
#在DNS MASTER增加一条db解析记录
[root@ubuntu-157 bind]# cat db.linux-magedu.com 
linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 124 3H 15M 1D 1W )

linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.

dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
dns2.linux-magedu.com.  86400  IN  A   10.0.0.150

www.linux-magedu.com.  86400  IN  A   10.0.0.153
w.linux-magedu.com.    86400  IN  A   1.2.3.4
bbs.linux-magedu.com.  86400  IN  A   11.11.11.11
blog.linux-magedu.com. 86400  IN  A   6.6.6.6                #添加

#查看文件修改时间
[root@ubuntu-157 bind]# ll db.linux-magedu.com 
-rw-r--r-- 1 root bind 502 Oct 19 20:37 db.linux-magedu.com

[root@ubuntu-157 bind]# dig blog.linux-magedu.com

; <<>> DiG 9.20.11-0ubuntu0.1-Ubuntu <<>> blog.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2621
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: fc865b82c15d437f0100000068f4dc0549e5eac6e4341ca1 (good)
;; QUESTION SECTION:
;blog.linux-magedu.com.		IN	A

;; ANSWER SECTION:
blog.linux-magedu.com.	86400	IN	A	6.6.6.6

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Sun Oct 19 20:39:33 CST 2025
;; MSG SIZE  rcvd: 94

#在DNS SLAVE测试 - 还未同步数据,解析失败
[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com 
-rw-r--r-- 1 bind bind 464 Oct 19 20:00 /etc/bind/slaves/db.linux-magedu.com

[root@ubuntu2204-150 ~]# dig blog.linux-magedu.com

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> blog.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 62014
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 7c3490c9cbb3fc8a0100000068f4dc68998a6748289824f5 (good)
;; QUESTION SECTION:
;blog.linux-magedu.com.		IN	A

;; AUTHORITY SECTION:
linux-magedu.com.	86400	IN	SOA	linux-dns.linux-magedu.com. admin.linux-magedu.com. 123 10800 900 86400 604800

;; Query time: 3 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Sun Oct 19 20:41:12 CST 2025
;; MSG SIZE  rcvd: 130

#DNS MASTER更改版本号
[root@ubuntu-157 bind]# vim db.linux-magedu.com
linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 125 3H 15M 1D 1W )
...

[root@ubuntu-157 bind]# rndc reload
server reload successful

[root@ubuntu-157 bind]# ll db.linux-magedu.com 
-rw-r--r-- 1 root bind 502 Oct 19 20:43 db.linux-magedu.com

#DNS SLAVE 同步成功
[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com 
-rw-r--r-- 1 bind bind 464 Oct 19 20:44 /etc/bind/slaves/db.linux-magedu.com

TCP端口和UDP端口

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

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

objectivec 复制代码
[root@ubuntu-157 ~]# vim /etc/bind/named.conf.default-zones 
[root@ubuntu-157 ~]# cat /etc/bind/named.conf.default-zones 
...
zone "0.0.10.in-addr.arpa" IN {
	type master;
	file "/etc/bind/db.0.0.10.in-addr.arpa";
};

#设置解析规则
[root@ubuntu-157 ~]# vim /etc/bind/db.0.0.10.in-addr.arpa
[root@ubuntu-157 ~]# cat /etc/bind/db.0.0.10.in-addr.arpa
0.0.10.in-addr.arpa.  86400  IN  SOA  ptr-test.  admin.linux-magedu.com.  ( 1 3H 15M 1D 1W )
0.0.10.in-addr.arpa.  86400  IN  NS   ptr.linux-magedu.com.

153.0.0.10.in-addr.arpa.  86400  IN  PTR  www.linux-magedu.com.
123.0.0.10.in-addr.arpa.  86400  IN  PTR  about.linux-magedu.com.

#语法检查
[root@ubuntu-157 ~]# named-checkzone 0.0.10.in-addr.arpa /etc/bind/db.0.0.10.in-addr.arpa
zone 0.0.10.in-addr.arpa/IN: loaded serial 1
OK

[root@ubuntu-157 ~]# rndc reload
server reload successful

#客户端测试
[root@rocky-154 ~]#dig -x 10.0.0.153 @10.0.0.157

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> -x 10.0.0.153 @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2247
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 20fb75d2520da50b0100000068f4eb3f00689718c6841cf9 (good)
;; QUESTION SECTION:
;153.0.0.10.in-addr.arpa.	IN	PTR

;; ANSWER SECTION:
153.0.0.10.in-addr.arpa. 86400	IN	PTR	www.linux-magedu.com.

;; Query time: 1 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Sun Oct 19 21:44:30 CST 2025
;; MSG SIZE  rcvd: 114

[root@rocky-154 ~]#dig -x 10.0.0.123 @10.0.0.157

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> -x 10.0.0.123 @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33759
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: d94e61f59dd337330100000068f4eb3e92c6bfc888e9dc8b (good)
;; QUESTION SECTION:
;123.0.0.10.in-addr.arpa.	IN	PTR

;; ANSWER SECTION:
123.0.0.10.in-addr.arpa. 86400	IN	PTR	about.linux-magedu.com.

;; Query time: 1 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Sun Oct 19 21:44:29 CST 2025
;; MSG SIZE  rcvd: 116
objectivec 复制代码
#上述规则 其他写法

$TTL 1D
@    IN    SOA    ptr-test. admin.linux-magedu.com. (1 3H 15M 1D 1W)
           NS     ptr.linux-magedu.com.
153        PTR    www.linux-magedu.com.
123        PTR    about.linux-magedu.com.

7、实现DNS缓存

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

Rocky系统中设置DNS缓存

objectivec 复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

[root@rocky-154 ~]#ping www.linux-magedu.com
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=0.732 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.486 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.613 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.974 ms
^C
--- www.linux-magedu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3045ms
rtt min/avg/max/mdev = 0.486/0.701/0.974/0.180 ms

#停止157上的DNS服务
[root@ubuntu-157 bind]# systemctl stop named


#客户端测试,无法解析域名
[root@rocky-154 ~]#ping www.linux-magedu.com
ping: www.linux-magedu.com: Name or service not known

#启动,让客户端安装
[root@ubuntu-157 bind]# systemctl start named

#安装缓存软件,执行前保证客户机DNS能用,先启动DNS服务
[root@rocky-154 ~]#yum install -y nscd

[root@rocky-154 ~]#systemctl enable --now nscd
Created symlink /etc/systemd/system/multi-user.target.wants/nscd.service → /usr/lib/systemd/system/nscd.service.
Created symlink /etc/systemd/system/sockets.target.wants/nscd.socket → /usr/lib/systemd/system/nscd.socket.

[root@rocky-154 ~]#ping www.linux-magedu.com
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=1.26 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.486 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.535 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.414 ms
^C
--- www.linux-magedu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 4076ms
rtt min/avg/max/mdev = 0.414/0.624/1.264/0.323 ms

#再次停止157DNS服务
[root@ubuntu-157 bind]# systemctl stop named

#客户端继续可以解析域名
[root@rocky-154 ~]#ping www.linux-magedu.com
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=1.11 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.565 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.426 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=4 ttl=64 time=0.449 ms
^C
--- www.linux-magedu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3085ms
rtt min/avg/max/mdev = 0.426/0.636/1.107/0.278 ms

#没有缓存到,无法解析
[root@rocky-154 ~]#ping bbs.linux-magedu.com
ping: bbs.linux-magedu.com: Name or service not known

#启动DNS服务
[root@ubuntu-157 bind]# systemctl start named

[root@rocky-154 ~]#ping bbs.linux-magedu.com
PING bbs.linux-magedu.com (11.11.11.11) 56(84) bytes of data.
^C
--- bbs.linux-magedu.com ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 4122ms

#再次关闭157DNS服务
[root@ubuntu-157 bind]# systemctl stop named

[root@rocky-154 ~]#ping bbs.linux-magedu.com
PING bbs.linux-magedu.com (11.11.11.11) 56(84) bytes of data.
^C
--- bbs.linux-magedu.com ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2077ms

Rocky系统查看DNS缓存

objectivec 复制代码
[root@rocky-154 ~]#nscd -g
nscd configuration:

              0  server debug level
        13m  2s  server runtime
              5  current number of threads
             32  maximum number of threads
              0  number of times clients had to wait
             no  paranoia mode enabled
           3600  restart internal
              5  reload count
...

Ubuntu系统中的DNS缓存

复制代码
#ubuntu自带DNS缓存,systemd-resolved服务提供
#用一台新主机160,没有配置过bind程序的主机

[root@ubuntu2204-160 ~]# cat /etc/resolv.conf 
...
nameserver 127.0.0.53
options edns0 trust-ad
search localdomain


#无法PING通,因为该域名解析是配置在157主机上
[root@ubuntu2204-160 ~]# ping www.linux-magedu.com -c1
ping: www.linux-magedu.com: Name or service not known

[root@ubuntu2204-160 ~]# dig www.linux-magedu.com

; <<>> DiG 9.18.30-0ubuntu0.22.04.2-Ubuntu <<>> www.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 30658
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.linux-magedu.com.		IN	A

;; AUTHORITY SECTION:
com.			5	IN	SOA	a.gtld-servers.net. nstld.verisign-grs.com. 1760938999 1800 900 604800 900

;; Query time: 476 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Mon Oct 20 05:43:34 UTC 2025
;; MSG SIZE  rcvd: 122

#指定DNS
[root@ubuntu2204-160 ~]# dig www.linux-magedu.com @10.0.0.157

; <<>> DiG 9.18.30-0ubuntu0.22.04.2-Ubuntu <<>> www.linux-magedu.com @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57569
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: d0dab62d0195cd770100000068f5cc391efd2be593473270 (good)
;; QUESTION SECTION:
;www.linux-magedu.com.		IN	A

;; ANSWER SECTION:
www.linux-magedu.com.	86400	IN	A	10.0.0.153

;; Query time: 3 msec
;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
;; WHEN: Mon Oct 20 05:44:25 UTC 2025
;; MSG SIZE  rcvd: 93

#修改网卡,指定DNS
[root@ubuntu2204-160 ~]# cat /etc/netplan/50-cloud-init.yaml 
...
network:
    ethernets:
        ens33:
           #dhcp4: true
           addresses: [10.0.0.160/24]
           routes: [{to: default,via: 10.0.0.2}]
           nameservers:
             addresses: [10.0.0.157]           #指定DNS10.0.0.157
    version: 2

[root@ubuntu2204-160 ~]# netplan apply 

#测试-生效
[root@ubuntu2204-160 ~]# host www.linux-magedu.com
www.linux-magedu.com has address 10.0.0.153

[root@ubuntu2204-160 ~]# ping www.linux-magedu.com -c1
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=2.87 ms

--- www.linux-magedu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 2.867/2.867/2.867/0.000 ms

#关闭157 远程DNS服务
[root@ubuntu-157 ~]# systemctl stop named

#再次测试客户端,上次解析后有了缓存
[root@ubuntu2204-160 ~]# ping www.linux-magedu.com 
PING www.linux-magedu.com (10.0.0.153) 56(84) bytes of data.
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=1 ttl=64 time=0.631 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=2 ttl=64 time=0.364 ms
64 bytes from www.linux-magedu.com (10.0.0.153): icmp_seq=3 ttl=64 time=0.509 ms
^C
--- www.linux-magedu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.364/0.501/0.631/0.109 ms

Ubuntu系统查看DNS缓存

复制代码
[root@ubuntu2204-160 ~]# resolvectl statistics 
DNSSEC supported by current servers: no

Transactions            
Current Transactions: 0
  Total Transactions: 48
                        
Cache                   
  Current Cache Size: 2
          Cache Hits: 5
        Cache Misses: 6
                        
DNSSEC Verdicts         
              Secure: 0
            Insecure: 0
               Bogus: 0
       Indeterminate: 0

Windows中的DNS缓存

复制代码
C:\Users\HhAosocool>ipconfig/displaydns
...
www.linux-magedu.com
    ----------------------------------------
    记录名称. . . . . . . : www.linux-magedu.com
    记录类型. . . . . . . : 1
    生存时间. . . . . . . : 86373
    数据长度. . . . . . . : 4
    部分. . . . . . . . . : 答案
    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-ubuntu | MASTER DNS SERVER | 为客户端主机提供DNS解析服务 |
| 10.0.0.150-ubuntu | SLAVE DNS SERVER | 为客户端主机提供DNS解析服务,同时从master同步解析数据 |
| 10.0.0.160-ubuntu | Subdomain DNS SERVER | 为子域提供解析服务 |
| 10.0.0.154-rocky | 客户端 | 客户端主机将DNS配置为10.0.0.157 |
| 10.0.0.153-rocky | WEB- | 提供web网站,解析域名 |

DNS MASTER 配置

复制代码
#添加bj子域配置,bj子域由bjdns服务解析,修改序列号
[root@ubuntu-157 ~]# cat /etc/bind/db.linux-magedu.com 
linux-magedu.com.  86400  IN  SOA  linux-dns  admin.linux-magedu.com.  ( 127 3H 15M 1D 1W )

linux-magedu.com.  86400  IN  NS   dns1.linux-magedu.com.
linux-magedu.com.  86400  IN  NS   dns2.linux-magedu.com.
bj.linux-magedu.com.  86400  IN  NS  bj.linux-magedu.com.

dns1.linux-magedu.com.  86400  IN  A   10.0.0.157
dns2.linux-magedu.com.  86400  IN  A   10.0.0.150
bj.linux-magedu.com.    86400  IN  A   10.0.0.160

www.linux-magedu.com.  86400  IN  A   10.0.0.153
w.linux-magedu.com.    86400  IN  A   1.2.3.4
bbs.linux-magedu.com.  86400  IN  A   11.11.11.11

#重载生效
[root@ubuntu-157 ~]# rndc reload
server reload successful

[root@ubuntu-157 ~]# ll /etc/bind/db.linux-magedu.com 
-rw-r--r-- 1 root bind 563 Oct 20 14:43 /etc/bind/db.linux-magedu.com

#查看从服务器,主从同步成功
[root@ubuntu2204-150 ~]# ll /etc/bind/slaves/db.linux-magedu.com
-rw-r--r-- 1 bind bind  465 Oct 20 14.43 /etc/bind/db.linux-magedu.com

子域DNS配置

复制代码
[root@ubuntu2204-160 ~]# apt install bind9 -y

#子域配置
[root@ubuntu2204-160 ~]# cat /etc/bind/named.conf.default-zones 
...
zone "bj.linux-magedu.com" IN {
	type master;
	file "/etc/bind/db.bj.linux-magedu.com";
};

[root@ubuntu2204-160 ~]# cat /etc/bind/db.bj.linux-magedu.com
bj.linux-magedu.com.  86400  IN  SOA  bj-dns.  admin.bj-dns.com ( 123 3H 15M 1D 1W )

bj.linux-magedu.com.  86400  IN  NS   bj-sub.bj.linux-magedu.com.

bj-sub.bj.linux-magedu.com.  86400  IN  A   10.0.0.160
www.bj.linux-magedu.com.     86400  IN  A   11.22.33.44
test.bj.linux-magedu.com.    86400  IN  A   55.66.77.88

#生效
[root@ubuntu2204-160 ~]# rndc reload
server reload successful

#测试
#本机配置 标志位:权威aa
[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @127.1

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @127.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6684
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 7d5520c1a2a50e4f0100000068f5e29f0933759031431e72 (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.1) (UDP)
;; WHEN: Mon Oct 20 07:19:59 UTC 2025
;; MSG SIZE  rcvd: 96

[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @10.0.0.160

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @10.0.0.160
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32359
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ed549f1bd8b0a39d0100000068f5e34d9065625c815c2047 (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44

;; Query time: 0 msec
;; SERVER: 10.0.0.160#53(10.0.0.160) (UDP)
;; WHEN: Mon Oct 20 07:22:53 UTC 2025
;; MSG SIZE  rcvd: 96

#找157DNS解析 157将子域转发到160 非权威
[root@ubuntu2204-160 ~]# dig www.bj.linux-magedu.com @10.0.0.157

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.bj.linux-magedu.com @10.0.0.157
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15187
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 02fba371dc424fad0100000068f5e2d92e7bc11c10558457 (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44

;; Query time: 39 msec
;; SERVER: 10.0.0.157#53(10.0.0.157) (UDP)
;; WHEN: Mon Oct 20 07:20:57 UTC 2025
;; MSG SIZE  rcvd: 96

客户端测试

复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

#去157DNS解析,被转发到160解析 非权威
[root@rocky-154 ~]#dig www.bj.linux-magedu.com

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57512
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 92ae121f6ff541ea0100000068f5e490ee9c4290a2c87597 (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 85961	IN	A	11.22.33.44

;; Query time: 1 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Mon Oct 20 15:28:17 CST 2025
;; MSG SIZE  rcvd: 96

#直接指定160
[root@rocky-154 ~]#dig www.bj.linux-magedu.com @10.0.0.160

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com @10.0.0.160
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20836
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: abb996930a627d5d0100000068f5e5290cadca6788e9fa5c (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44

;; Query time: 2 msec
;; SERVER: 10.0.0.160#53(10.0.0.160)
;; WHEN: Mon Oct 20 15:30:50 CST 2025
;; MSG SIZE  rcvd: 96

#指定从DNS
[root@rocky-154 ~]#dig www.bj.linux-magedu.com @10.0.0.150

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.bj.linux-magedu.com @10.0.0.150
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 12017
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ae9343ecc7e352a80100000068f5e5dc9177cfd3e64b9383 (good)
;; QUESTION SECTION:
;www.bj.linux-magedu.com.	IN	A

;; ANSWER SECTION:
www.bj.linux-magedu.com. 86400	IN	A	11.22.33.44

;; Query time: 0 msec
;; SERVER: 10.0.0.150#53(10.0.0.150)
;; WHEN: Mon Oct 20 15:33:48 CST 2025
;; MSG SIZE  rcvd: 148

9、实现DNS自解析的转发

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

两种转发

|-------|----------------------------------------------------------|
| 转发策略 | 说明 |
| first | 当客户端请求解析,如果DNS服务器无记录,则转发给上级DNS, 如果上级DNS无返回,则当前DNS请求根域解析。 |
| only | 当客户端请求解析,如果DNS服务器无记录,则转发给上级DNS, 不管上级DNS是否能解析,当前DNS都直接返回。 |

|-------------------|------------|--------------------------|
| 主机IP | 角色 | 备注 |
| 10.0.0.157-ubuntu | DNS SERVER | 为客户端主机提供DNS解析服务 |
| 10.0.0.150-ubuntu | DNS SERVER | 为客户端主机提供DNS解析服务,并指定转发DNS |
| 10.0.0.154-rocky | 客户端 | 客户端主机将DNS配置为10.0.0.157 |

直接DNS配置

复制代码
[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options 
options {
    ...
	dnssec-validation no;            #关闭加密验证
	forwarders { 10.0.0.150; };      #转发服务器
	forward first;                   #转发策略,间接DNS没有返回,则直接DNS再次向根域请求解析

	listen-on-v6 { any; };
};

[root@ubuntu-157 ~]# named-checkconf 

[root@ubuntu-157 ~]# systemctl restart named 

间接DNS配置

复制代码
#新增域名配置
[root@ubuntu2204-150 ~]# cat /etc/bind/named.conf.default-zones 
...
//zone "linux-magedu.com" IN {            #将主从配置注释
//	type slave;
//	masters {10.0.0.157;};
//	file "/etc/bind/slaves/db.linux-magedu.com";
//};

zone "golang-magedu.com" IN {
	type master;
	file "/etc/bind/db.golang-magedu.com";
};

[root@ubuntu2204-150 ~]# cat /etc/bind/db.golang-magedu.com
golang-magedu.com.  86400  IN  SOA  golang-dns.  admin.golang-dns.com ( 123 3H 15M 1D 1W)

golang-magedu.com.  86400  IN  NS   dns.golang-magedu.com.

dns.golang-magedu.com.  86400  IN  A   10.0.0.150
www.golang-magedu.com.  86400  IN  A   1.2.3.4
test.golang-magedu.com. 86400  IN  A   5.6.7.8

[root@ubuntu2204-150 ~]# systemctl restart named

客户端测试

复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

#157DNS提供解析 权威
[root@rocky-154 ~]#nslookup www.linux-magedu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Name:	www.linux-magedu.com
Address: 10.0.0.153

#由157转发至150提供解析 非权威
[root@rocky-154 ~]#nslookup www.golang-magedu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Non-authoritative answer:
Name:	www.golang-magedu.com
Address: 1.2.3.4

[root@rocky-154 ~]#dig www.golang-magedu.com

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.golang-magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26964
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 80e8d4b349a904be0100000068f5eb1e11f0013f2cc8b0e6 (good)
;; QUESTION SECTION:
;www.golang-magedu.com.		IN	A

;; ANSWER SECTION:
www.golang-magedu.com.	86335	IN	A	1.2.3.4

;; Query time: 0 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Mon Oct 20 15:56:14 CST 2025
;; MSG SIZE  rcvd: 94

#指定150DNS 权威
[root@rocky-154 ~]#dig www.golang-magedu.com @10.0.0.150

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.golang-magedu.com @10.0.0.150
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33227
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 53898bc3a686acfc0100000068f5eb38b65923499f746733 (good)
;; QUESTION SECTION:
;www.golang-magedu.com.		IN	A

;; ANSWER SECTION:
www.golang-magedu.com.	86400	IN	A	1.2.3.4

;; Query time: 0 msec
;; SERVER: 10.0.0.150#53(10.0.0.150)
;; WHEN: Mon Oct 20 15:56:41 CST 2025
;; MSG SIZE  rcvd: 94

#根据转发策略,此解析转发给了间接DNS服务器,间接DNS服务器再向根域请求解析得到
[root@rocky-154 ~]#nslookup www.baidu.com
Server:		10.0.0.157
Address:	10.0.0.157#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 110.242.70.57
Name:	www.a.shifen.com
Address: 110.242.69.21
Name:	www.a.shifen.com
Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc
Name:	www.a.shifen.com
Address: 2408:871a:2100:186c:0:ff:b07e:3fbc

[root@ubuntu2204-150 ~]# nslookup www.baidu.com
Server:		127.0.0.1
Address:	127.0.0.1#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 220.181.111.1
Name:	www.a.shifen.com
Address: 220.181.111.232
Name:	www.a.shifen.com
Address: 240e:83:205:381:0:ff:b00f:96a2
Name:	www.a.shifen.com
Address: 240e:83:205:1cd:0:ff:b0b8:dee9

#删除间接DNS路由表,并清除DNS缓存
[root@ubuntu2204-150 ~]# route del default 

[root@ubuntu2204-150 ~]# rndc flush

#无法解析,路由表删除
[root@ubuntu2204-150 ~]# dig www.baidu.com

; <<>> DiG 9.18.39-0ubuntu0.22.04.1-Ubuntu <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 47717
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: bd56abf6482c84eb0100000068f5f15740ebb073e467a6df (good)
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; Query time: 7 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; WHEN: Mon Oct 20 16:22:47 CST 2025
;; MSG SIZE  rcvd: 70

#客户端测试
#157转发给150,150路由删除,157自己找根解析
[root@rocky-154 ~]#dig www.baidu.com

; <<>> DiG 9.11.36-RedHat-9.11.36-16.el8_10.4 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48424
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 81554849018aeb0a0100000068f5f24923a555a62167071b (good)
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		254	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	120	IN	A	110.242.70.57
www.a.shifen.com.	120	IN	A	110.242.69.21

;; Query time: 319 msec
;; SERVER: 10.0.0.157#53(10.0.0.157)
;; WHEN: Mon Oct 20 16:26:50 CST 2025
;; MSG SIZE  rcvd: 129

#同样如果删除157的路由客户端主机就无法解析成功

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

复制代码
#原先[root@ubuntu-157 ~]# cat /etc/bind/named.conf.options
... 
	dnssec-validation no;
	forwarders { 10.0.0.150; };
	forward first;

	listen-on-v6 { any; };
...

#写在zones中的转发配置的优先级高于全局置
[root@ubuntu ~]# vim /etc/bind/named.conf.default-zones
zone "ZONE_NAME" IN {
    type forward;
    forward first|only;
    forwarders {IP;};
}

9、实现DNS自解析的视图

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

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

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

DNS SERVER网络配置

复制代码
[root@ubuntu-157 ~]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:88:bd:f8 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    altname enx000c2988bdf8
    inet 10.0.0.157/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe88:bdf8/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:0c:29:88:bd:02 brd ff:ff:ff:ff:ff:ff
    altname enp2s5
    altname enx000c2988bd02

[root@ubuntu-157 ~]# ip addr a dev ens37 192.168.10.157/24

[root@ubuntu-157 ~]# ip a s
...
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:0c:29:88:bd:02 brd ff:ff:ff:ff:ff:ff
    altname enp2s5
    altname enx000c2988bd02
    inet 192.168.10.157/24 scope global ens37
       valid_lft forever preferred_lft forever

DNS SERVER配置

复制代码
#将/etc/bind/named.conf.options 转发策略注释

[root@ubuntu-157 ~]# netplan apply 

[root@ubuntu-157 ~]# systemctl restart named

#192.169.10.157新增内容
[root@ubuntu-157 ~]# cat /etc/bind/named.conf
...
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
#include "/etc/bind/named.conf.default-zones";            #注释

[root@ubuntu-157 bind]# cat /etc/bind/named.conf.options 
acl test_net{
	192.168.10.0/24;
};

acl prod_net{
	10.0.0.0/24;
};

acl other_net{
	any;
};

view test_view{
	match-clients {test_net;};
	include "/etc/bind/named.conf.default-zones";
	include "/etc/bind/named.conf.test-zones";
};

view prod_view{
	match-clients {prod_net;};
	include "/etc/bind/named.conf.default-zones";
	include "/etc/bind/named.conf.prod-zones";
};

view other_view{
	match-clients {other_net;};
	include "/etc/bind/named.conf.default-zones";
};

...

#新增中间文件
[root@ubuntu-157 ~]# cat /etc/bind/named.conf.test-zones
zone "sre-magedu.com" {
	type master;
	file "/etc/bind/db.test.sre-magedu.com";
};

[root@ubuntu-157 ~]# cat /etc/bind/named.conf.prod-zones
zone "sre-magedu.com" {
	type master;
	file "/etc/bind/db.prod.sre-magedu.com";
};

#具体解析规则
[root@ubuntu-157 ~]# cat /etc/bind/db.test.sre-magedu.com
test.sre-magedu.com.  86400  IN  SOA  sre-magedu-dns.  admin.sre-magedu.com. ( 123 3H 15M 1D 1W)

test.sre-magedu.com.  86400  IN  NS   dns1.test.sre-magedu.com.  
test.sre-magedu.com.  86400  IN  NS   dns2.test.sre-magedu.com.

dns1.test.sre-magedu.com.  86400  IN  A   192.169.10.157
www.test.sre-magedu.com.   86400  IN  A   192.168.10.11
blog.test.sre-magedu.com.  86400  IN  A   192.168.10.22

[root@ubuntu-157 ~]# cat /etc/bind/db.prod.sre-magedu.com
test.sre-magedu.com.  86400  IN  SOA  sre-magedu-dns.  admin.sre-magedu.com. ( 123 3H 15M 1D 1W)

test.sre-magedu.com.  86400  IN  NS   dns1.test.sre-magedu.com.  
test.sre-magedu.com.  86400  IN  NS   dns2.test.sre-magedu.com.

dns1.test.sre-magedu.com.  86400  IN  A   10.0.0.157
www.test.sre-magedu.com.   86400  IN  A   10.0.0.11
blog.test.sre-magedu.com.  86400  IN  A   10.0.0.22

[root@ubuntu-157 bind]# named-checkconf /etc/bind/named.conf

#重启服务
[root@ubuntu-157 bind]# systemctl restart named

客户端网络配置

复制代码
[root@rocky-154 ~]#ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:41:13 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 10.0.0.154/24 brd 10.0.0.255 scope global dynamic noprefixroute ens160
       valid_lft 1458sec preferred_lft 1458sec
    inet6 fe80::20c:29ff:fe2d:4113/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:41:1d brd ff:ff:ff:ff:ff:ff
    altname enp11s0

[root@rocky-154 ~]#ip addr a dev ens192 192.168.10.154/24 

[root@rocky-154 ~]#nmcli con reload 

[root@rocky-154 ~]#nmcli con up ens192
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

[root@rocky-154 ~]#ip a s
...
3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:41:1d brd ff:ff:ff:ff:ff:ff
    altname enp11s0
    inet 192.168.10.154/24 brd 192.168.10.255 scope global noprefixroute ens192
       valid_lft forever preferred_lft forever

客户端测试

复制代码
[root@rocky-154 ~]#cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 10.0.0.157

[root@rocky-154 ~]#dig -b 10.0.0.154 www.sre-magedu.com +short

[root@rocky-154 ~]#dig -b 10.0.0.154 www.sre-magedu.com @10.0.0.157 +short
10.0.0.11

[root@rocky-154 ~]#dig -b 192.168.10.154 www.sre-magedu.com @192.168.10.157 +short
192.168.10.11

四、智能DNS解析

在前面使用bind中的视图,实现了基于不同来源的客户端IP,对同一个域名,返回不同的解析结果。所谓智能DNS,也是根据不同的条件,对相同的域名解析不同的具体IP地址,但其实现,远比bind中用视图复杂得多。

目前很多DNS服务商都提供了智能DNS服务,智能DNS可以通过多种策略来将客户端需要访问的域名解析到不同的数据中心或不同的运营商网路上,比如通过请求者的IP地址得到客户端的地理位置来判断用户的就近性,并结合健康检查策略,将请求解析到最近的服务器上。

1、GLSB全局负载均衡

GLSB(Global Server Load Balance),全局负载均衡。在广域网上,对不同地域的服务器间的流量进行调度,通过判断服务器的负载,带宽的负载等,决定服务器的可用性,同时判断客户端与服务器之间的链路状况,选择与客户端最佳匹配的服务器,并将客户端请求调度到该服务器上。

因此GLSB是对服务器和链路进行综合判断来决定由哪个机房的服务器来提供服务,实现异地服务器群服务质量的保证。

GLSB 的实现有三种方式:基于DNS实现、基于重定向实现、基于路由协议实现,其中最通用的是基于DNS的实现。

GLSB 的使用范围:常用于有异地机房的WEB系统,或在CDN系统中作为核心的流量调度系统。

复制代码
#唯品会使用 gslb 来实现流量负载
[root@rocky-154 ~]#nslookup www.vip.com
Server:		10.0.0.2
Address:	10.0.0.2#53

#将 www.vip.com 的域名解析 cname 到 shop.vip.com.vipgslb.com
#由 shop.vip.com.vipgslb.com 在 DNS层面实现流量负载
Non-authoritative answer:
www.vip.com	canonical name = www.vip.com.vipgslb.com.
Name:	www.vip.com.vipgslb.com
Address: 14.215.62.24
Name:	www.vip.com.vipgslb.com
Address: 2408:8756:4cff:d301::2

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节点会将此次请求的内容缓存起来,这样下次该节点被访问时,就不用再去请求源站点。

复制代码
#百度使用的CDN技术
[root@rocky-154 ~]#nslookup www.baidu.com
Server:		10.0.0.2
Address:	10.0.0.2#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 110.242.70.57
Name:	www.a.shifen.com
Address: 110.242.69.21
Name:	www.a.shifen.com
Address: 2408:871a:2100:1b23:0:ff:b07a:7ebc
Name:	www.a.shifen.com
Address: 2408:871a:2100:186c:0:ff:b07e:3fbc

#百度将域名 cname 到 www.a.shifen.com
由www.a.shifen.com 负责解析,根据访问者地理位置,返
回最近的CDN节点IP
相关推荐
Ray Song3 个月前
Linux DNS解析3 -- DNS解析代理配置使用
linux·dns解析·dns代理
Ray Song3 个月前
Linux DNS解析1--终端通过网关或者路由器进行域名解析的原理
linux·运维·服务器·dns解析
中科三方1 年前
网络安全宣传周 | DNS安全威胁与应对措施分享
dns解析
cui_win1 年前
Linux性能优化-网络篇-DNS问题排查
网络·dns·dns解析·dig命令·nslookup
IT WorryFree2 年前
智能dns服务器、自建DNS服务器、自架DNS服务器、DNS分流
运维·服务器·dns·dns服务器·智能dns·dns分流·smartdns
lzwx52132 年前
腾讯云添加SSL证书
https·腾讯云·ssl·dns解析
程序员小十一2 年前
DNS部署与安全详解(上)
域名·dns服务器部署·监听端口·dns解析