目录
2.2.测试DNS解析(检查"问路"能力)或使用更详细的dig
2.5.检查本地缓存(如果使用systemd-resolved)
[3.1.安装bind及bind utils 工具](#3.1.安装bind及bind utils 工具)
[3.3. 实操步骤:搭建主DNS服务器(解析example.com域名)](#3.3. 实操步骤:搭建主DNS服务器(解析example.com域名))
DNS(域名系统)是网络通信的基石,它负责将人类可读的域名(如:www.google.com)转换为机器可读的IP地址(如:142.250.190.78)。

一、核心配置文件详解
1.1.静态主机映射:/etc/hosts
这个是最直接,也就是最简单可配置的本地映射文件。
格式:ip地址 主机名 别名
127.0.0.1 localhost localhost.localdomain
192.168.75.155 my.server.local myserver
它可以用于覆盖公共DNS记录,进行本地测试或屏蔽
如下图,未覆盖公共DNS记录前,我执行ping www.oracle.com


现在我通过修改/etc/hosts文件,来覆盖公共DNS记录,再次执行ping www.oracle.com,很显然现在访问www.baidu.com该域名时,/etc/hosts本地映射已将其映射给192.168.75.155本地网卡的地址,回包的时候也是该IP地址返回的。

再次把它修改回来,发现已经被调整回来了

1.2.解析顺序调度:/etc/nsswitch.conf
它决定系统按什么顺序、从哪里获取信息。
# 关键行:定义主机名解析顺序
hosts: files dns myhostname
解释:先查/etc/hosts文件,再查DNS,最后使用系统主机名。这也更好的解释了为什么会存在覆盖公共DNS的现象。
执行:cat /etc/nsswitch.conf,查看详细的配置文件内容。
bash
[root@rac01 ~]# cat /etc/nsswitch.conf
#
# /etc/nsswitch.conf
#
# An example Name Service Switch config file. This file should be
# sorted with the most-used services at the beginning.
#
# The entry '[NOTFOUND=return]' means that the search for an
# entry should stop if the search in the previous entry turned
# up nothing. Note that if the search failed due to some other reason
# (like no NIS server responding) then the search continues with the
# next entry.
#
# Valid entries include:
#
# nisplus Use NIS+ (NIS version 3)
# nis Use NIS (NIS version 2), also called YP
# dns Use DNS (Domain Name Service)
# files Use the local files
# db Use the local database (.db) files
# compat Use NIS on compat mode
# hesiod Use Hesiod for user lookups
# [NOTFOUND=return] Stop searching if not found so far
#
# To use db, put the "db" in front of "files" for entries you want to be
# looked up first in the databases
#
# Example:
#passwd: db files nisplus nis
#shadow: db files nisplus nis
#group: db files nisplus nis
passwd: files sss
shadow: files sss
group: files sss
#initgroups: files sss
#hosts: db files nisplus nis dns
hosts: files dns myhostname
# Example - obey only what nisplus tells us...
#services: nisplus [NOTFOUND=return] files
#networks: nisplus [NOTFOUND=return] files
#protocols: nisplus [NOTFOUND=return] files
#rpc: nisplus [NOTFOUND=return] files
#ethers: nisplus [NOTFOUND=return] files
#netmasks: nisplus [NOTFOUND=return] files
bootparams: nisplus [NOTFOUND=return] files
ethers: files
netmasks: files
networks: files
protocols: files
rpc: files
services: files sss
netgroup: nisplus sss
publickey: nisplus
automount: files nisplus sss
aliases: files nisplus
[root@rac01 ~]#
1.3.DNS服务器列表:/etc/resolv.conf
这是最核心的DNS客户端配置文件,但现代Linux中它常被自动管理。
关建指令
nameserver 8.8.8.8 # 主DNS服务器(最多三个)
nameserver 114.114.114.114 # 备用DNS服务器
search example.com # 搜索域,查询'db'时会尝试'db.example.com'
options timeout:2 # 查询超时(秒)
options attempts:3 # 尝试次数
注意:在使用了NetworkManager或systemd-resolved的系统上,直接编辑此文件可能无效或被覆盖。
当我们新建一个服务器之后,往往最开始都有着上网的需求(比如需要从网络上下载很多的软件包或则更新对应库的需要),但很多情况都会出现一个比较头大的问题,那就是网卡配置好之后,ping不通外网,并且直接收到:ping: www.baidu.com: 未知的名称或服务类似于这样的东西。
那为什么会出现这种问题?
我们都知道,计算机的网络世界要想互联互通,除了一系列需要处理的协议,还有一个最关键也是最基础的东西,那就是各设备之间的身份地址(ip地址),计算机的互联互通离不开IP地址,也可以说,没有IP地址,我们可能也就实现不了现在这么多设备之间的通信!!!
**那这里是因为什么才导致无法访问外网呢?**是不是当ping这个域名的时候,计算机不能根据这个域名拿到对应域名的IP地址呢?答案是:当然是,如下图,我们直接访问ping ip地址能完全收到回包。

我们先对静态映射文件做一个修改,再次印证我们的猜想

毫无疑问,当发起ping www.baidu.com这个请求的时候,操作系统这边将域名映射给我们本地网卡,当我们本地网卡发现这个ping请求时,就对其进行响应,从而我们就接收到了对应的回包效果。
那这时候怎么解决?

1.4.DNS配置管理器(现代系统)
systemd-resolved(主流):提供本地DNS缓存和更复杂的管理。
查看状态
systemctl status systemd-resolved
查看真正的DNS配置
resolvectl status
NetworkManager(常见于桌面/服务器):管理网络链接及DNS
查看所有链接
nmcli connection show
修改连接的DNS
nmcli connection modify "连接名" ipv4.dns "8.8.8.8 114.114.114.114"
nmcli connection down "连接名";nmcli connection up "连接名" # 重启生效
bash
[root@rac01 ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens33 c6c0bba9-9f95-405f-a8a1-d121d266e6b1 ethernet ens33
[root@rac01 ~]# cat /etc/system
systemd/ system-release system-release-cpe
[root@rac01 ~]# cat /etc/system
systemd/ system-release system-release-cpe
[root@rac01 ~]# cat /etc/sys
sysconfig/ sysctl.conf sysctl.confecho sysctl.d/ systemd/ system-release system-release-cpe
[root@rac01 ~]# cat /etc/sysconfig/network
network network.orabackup network-scripts/
[root@rac01 ~]# cat /etc/sysconfig/network-scripts/if
ifcfg-ens33 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel
ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
[root@rac01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=c6c0bba9-9f95-405f-a8a1-d121d266e6b1
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.75.155
PREFIX=24
GATEWAY=192.168.75.254
DNS1=8.8.8.8
[root@rac01 ~]#
二、诊断与故障排除命令
当遇到:"未知的名称或服务"时,按顺序使用这些命令诊断:
2.1.基础连通性测试(先确认能"出门")
ping -c 4 8.8.8.8

2.2.测试DNS解析(检查"问路"能力)或使用更详细的dig
dig @8.8.8.8 www.oracle.com # 指定向8.8.8.8查询

dig www.oracle.com +trace #显示完整递归追踪(可以先注释配置做观察)

bash
[root@rac01 ~]# dig @114.114.114.114 www.baidu.com +short
www.a.shifen.com.
183.2.172.17
183.2.172.177
[root@rac01 ~]# dig @114.114.114.114 www.oracle.com +trace
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.0.1.P2.el7_9.16 <<>> @114.114.114.114 www.oracle.com +trace
; (1 server found)
;; global options: +cmd
. 2171 IN NS i.root-servers.net.
. 2171 IN NS j.root-servers.net.
. 2171 IN NS k.root-servers.net.
. 2171 IN NS l.root-servers.net.
. 2171 IN NS g.root-servers.net.
. 2171 IN NS f.root-servers.net.
. 2171 IN NS b.root-servers.net.
. 2171 IN NS e.root-servers.net.
. 2171 IN NS d.root-servers.net.
. 2171 IN NS h.root-servers.net.
. 2171 IN NS m.root-servers.net.
. 2171 IN NS a.root-servers.net.
. 2171 IN NS c.root-servers.net.
;; Received 251 bytes from 114.114.114.114#53(114.114.114.114) in 38 ms
com. 172800 IN NS m.gtld-servers.net.
com. 172800 IN NS j.gtld-servers.net.
com. 172800 IN NS g.gtld-servers.net.
com. 172800 IN NS d.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
com. 172800 IN NS e.gtld-servers.net.
com. 172800 IN NS a.gtld-servers.net.
com. 172800 IN NS f.gtld-servers.net.
com. 172800 IN NS i.gtld-servers.net.
com. 172800 IN NS h.gtld-servers.net.
com. 172800 IN NS c.gtld-servers.net.
com. 172800 IN NS l.gtld-servers.net.
com. 172800 IN NS k.gtld-servers.net.
com. 86400 IN DS 19718 13 2 8ACBB0CD28F41250A80A491389424D341522D946B0DA0C0291F2D3D7 71D7805A
com. 86400 IN RRSIG DS 8 1 86400 20260122050000 20260109040000 21831 . KM3CQGj8EaCt8ETJZ+ssupChf/IAnkiiS+l2vpGaawjjkXxwsoFni6U7 JlYNYSNtXjBsXiQ6BuzcnWOGfvFwDabHGyrg1dpKnzAZv2afcjknHPoK 3zjk4JVSOpkdbGL39KaBlIhi5snkO/+vX9oX9nLH8xpLlR9qcGVTWHS0 z3hMl5W0EE7Kp15uq884jlN5OTzBv9YIlJc+vrXCQT16rz0e5qB5Ibps AVnEnqWGD359fYcf7ih7XnkUWWMYyVu/yFL+/fdt4SsKvJqN+v+lNAbW fypefsdgE2lH2sf53cvmRR6yur67fdOl9L/ZOJ39hig8NCvR2ymzGvzD QMHNpw==
;; Received 1177 bytes from 192.112.36.4#53(g.root-servers.net) in 229 ms
oracle.com. 172800 IN NS a18-67.akam.net.
oracle.com. 172800 IN NS a11-66.akam.net.
oracle.com. 172800 IN NS a1-160.akam.net.
oracle.com. 172800 IN NS a13-65.akam.net.
oracle.com. 172800 IN NS ns1.p201.dns.oraclecloud.net.
oracle.com. 172800 IN NS ns2.p201.dns.oraclecloud.net.
oracle.com. 172800 IN NS ns3.p201.dns.oraclecloud.net.
oracle.com. 172800 IN NS ns4.p201.dns.oraclecloud.net.
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 900 IN NSEC3 1 1 0 - CK0Q3UDG8CEKKAE7RUKPGCT1DVSSH8LL NS SOA RRSIG DNSKEY NSEC3PARAM
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 900 IN RRSIG NSEC3 13 2 900 20260114002707 20260106231707 46539 com. dWmHvmQAockqf++5zAsshQyK6CiTgqio/77GUit1Va2F3RURfVPeXM1c zJy6vHKrXLAsFg38LoN6EF4+8SQV2g==
NGDSGN8S86ENSKIHK1OHKC4OUMC3U5HH.com. 900 IN NSEC3 1 1 0 - NGDSIVSEC2SJH25M09V0G6ELCC7CRVOE NS DS RRSIG
NGDSGN8S86ENSKIHK1OHKC4OUMC3U5HH.com. 900 IN RRSIG NSEC3 13 2 900 20260114021129 20260107010129 46539 com. ILINxW1yhKBrj0EwedI690vJ0t5T5irAHcdpnoRkfzXN1349KTiTv3AY jl29/zldiLt0BS3XQiO16nW7GKCG1Q==
couldn't get address for 'a1-160.akam.net': not found
;; Received 585 bytes from 192.52.178.30#53(k.gtld-servers.net) in 173 ms
www.oracle.com. 300 IN CNAME ds-www.oracle.com.edgekey.net.
;; Received 86 bytes from 95.101.36.67#53(a18-67.akam.net) in 179 ms
[root@rac01 ~]#
2.3.检查当前的DNS服务器
cat /etc/resolv.conf

2.4.检查解析顺序
grep ^hosts /etc/nsswitch.conf

从解析结果上看,主机的解析是正确的。
2.5.检查本地缓存(如果使用systemd-resolved)
resolvedctl statistics
因该操作没搭载system-resolved工具包,故无法查询到缓存记录。

三、DNS环境部署
3.1.安装bind及bind utils 工具
CentOS/RHEL
yum install bind bind-utils -y # bind-utils包含dig、nslookup等工具
Ubuntu/Debian
apt install bind9 bind9 utils -y

3.2.核心配置文件梳理
|-----------------------------|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 文件路径 | 作用 | 关建配置项示例 |
| /etc/named.conf | 全局配置(服务器监听、访问控制等) | listen-on port 53 {any;};(允许所有 P 访问 DNS 服务)allow-query{any;};(允许所有客户端查询) |
| /etc/named.rfc1912.zones | 区域配置(定义负责解析的域名) | zone "example.com" IN { typemaster; file "example.com.zone"; };(声明主区域) |
| /var/named/example.com.zone | 正向区域文件(域名->ip映射) | STTL 86400 @ IN SOA ns.example.com.admin.example.com.(20240101013600 1800 604800 86400) @ IN NS ns.example.com.ns IN A 192.168.1.100wwW IN A 192.168.1.101 |
3.3. 实操步骤:搭建主DNS服务器(解析example.com域名)
1)修改全局配置(/etc/named.conf)
bash
vi /etc/named.conf
# 注释掉默认localhost区域,添加自定义区域配置(或直接编辑name.rfc1912.zones)
2)创建区域文件(/var/named/example.com.zone)
bash
cp /var/named/named.localhost /var/named/example.com.zone # 复制模板
vi /var/named/example.com.zone
# 修改SOA记录(邮箱、序列号)、NS记录(DNS服务器域名)、A记录(www、mail等主机)

3)启动并验证服务
bash
systemctl start named # 启动服务
systemctl enable named # 开机自启
named-checkconf # 检查配置文件语法
named-checkzone example.com /var/named/example.com.zone # 检查区域文件语法

4)客户端测试
修改客户端/etc/resolv.conf:nameserver 192.168.1.100(DNS服务器IP)。
执行dig www.example.com,若返回192.168.1.101则部署成功。
