当前位置: 代码网 > 服务器>服务器>Linux > Linux下查看DNS配置信息的常用命令说明

Linux下查看DNS配置信息的常用命令说明

2026年01月31日 Linux 我要评论
在 linux 系统中,了解当前服务器所使用的 dns(domain name system)配置对于网络排错、服务部署和系统管理都至关重要。本文将详细介绍几种常用的查看 dns 信息的方法,帮助你快

在 linux 系统中,了解当前服务器所使用的 dns(domain name system)配置对于网络排错、服务部署和系统管理都至关重要。

本文将详细介绍几种常用的查看 dns 信息的方法,帮助你快速定位和验证 dns 设置。

方法一:查看 /etc/resolv.conf 文件

这是最直接的方式。linux 系统通常通过 /etc/resolv.conf 文件指定 dns 服务器地址。

[root@localhost ~]# cat /etc/resolv.conf
nameserver 192.168.xxx.xxx
search localdomain

说明:

  • nameserver 后面的 ip 地址即为系统当前使用的 dns 服务器。
  • search 行定义了域名搜索后缀(本例中被注释掉了)。

注意:

在使用 networkmanager 或 systemd-resolved 的现代发行版(如 centos 7+/ubuntu 18.04+)中,该文件可能是自动生成的符号链接或由服务动态管理,手动修改可能被覆盖。

方法二:使用 nslookup 命令

nslookup 是一个交互式网络工具,可用于查询 dns 记录。我们可以通过它间接查看当前使用的 dns 服务器。

[root@localhost ~]# nslookup 127.0.0.1 | grep server
server: 192.168.xxx.xxx

说明:

  • 虽然查询的是本地回环地址(127.0.0.1),但返回的 server 字段显示了实际使用的 dns 服务器。
  • 更推荐使用 nslookup example.com 查询任意域名,观察其使用的解析服务器。

提示:nslookup 在部分新系统中可能未默认安装,可使用以下命令安装:

  • rhel/centos:yum install bind-utils
  • debian/ubuntu:apt install dnsutils

方法三:使用 dig 命令

dig(domain information groper)是功能强大的 dns 查询工具,输出信息更详细。

[root@localhost ~]# dig
; > dig 9.3.6-p1-redhat-9.3.6-16.p1.el5 >
;; ->>header<<- …
;; server: 192.168.xxx.xxx#53(192.168.xxx.xxx)
…

要快速提取 dns 服务器地址,可结合管道过滤:

[root@localhost ~]# dig | grep “server:” | awk -f'#' ‘{print 1}' | awk -f': ' ‘{print 2}'
192.168.xxx.xxx
  • 优点:dig 不仅能显示使用的 dns 服务器,还能展示完整的查询过程、响应时间、权威记录等,非常适合调试。

方法四:其他实用方法(适用于特定环境)

  1. 使用 nmcli(networkmanager 命令行工具)

适用于使用 networkmanager 管理网络的系统(如桌面版或较新的服务器版):

查看所有连接的 dns 信息

nmcli dev show | grep dns

查看特定接口(如 eth0)的 dns

nmcli dev show eth0 | grep ip4.dns
  1. 使用 resolvectl(新版 systemd 推荐方式)

在使用 systemd-resolved 服务的系统中(如 ubuntu 20.04+、centos 8+):

resolvectl status

该命令会显示每个网络接口的 dns 配置、是否启用 dnssec、当前使用的解析器等详细信息。

⚠️ 旧版系统可能使用 systemd-resolve --status,但该命令已在较新版本中被弃用。

  1. 已过时的工具(不推荐在新系统使用)
nm-tool | grep dns

nm-tool 在 networkmanager 1.0+ 版本中已被移除,请改用 nmcli。

总结对比

  • 方法 适用场景 是否需安装额外包 备注
  • /etc/resolv.conf 所有 linux 系统 否 最基础,但可能被服务覆盖
  • nslookup 快速验证 dns 解析 可能需要安装 简单直观
  • dig 专业 dns 调试 可能需要安装 功能最全
  • nmcli / resolvectl 使用 networkmanager 或 systemd-resolved 的系统 否(通常预装) 更符合现代 linux 架构

小贴士

  • 如果你发现 /etc/resolv.conf 中的 dns 被自动修改,很可能是 dhcp 客户端或 networkmanager 在管理它。
  • 修改 dns 配置时,建议优先通过网络管理工具(如 nmcli、netplan 或编辑网卡配置文件)进行,而非直接编辑 /etc/resolv.conf。

测试 dns 是否生效,可使用以下命令验证:

dig example.com
host google.com
nslookup baidu.com

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com