6人参与 • 2026-01-31 • Linux
在 linux 系统中,了解当前服务器所使用的 dns(domain name system)配置对于网络排错、服务部署和系统管理都至关重要。
本文将详细介绍几种常用的查看 dns 信息的方法,帮助你快速定位和验证 dns 设置。
这是最直接的方式。linux 系统通常通过 /etc/resolv.conf 文件指定 dns 服务器地址。
[root@localhost ~]# cat /etc/resolv.conf nameserver 192.168.xxx.xxx search localdomain
说明:
注意:
在使用 networkmanager 或 systemd-resolved 的现代发行版(如 centos 7+/ubuntu 18.04+)中,该文件可能是自动生成的符号链接或由服务动态管理,手动修改可能被覆盖。
nslookup 是一个交互式网络工具,可用于查询 dns 记录。我们可以通过它间接查看当前使用的 dns 服务器。
[root@localhost ~]# nslookup 127.0.0.1 | grep server server: 192.168.xxx.xxx
说明:
提示:nslookup 在部分新系统中可能未默认安装,可使用以下命令安装:
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适用于使用 networkmanager 管理网络的系统(如桌面版或较新的服务器版):
查看所有连接的 dns 信息
nmcli dev show | grep dns
查看特定接口(如 eth0)的 dns
nmcli dev show eth0 | grep ip4.dns
在使用 systemd-resolved 服务的系统中(如 ubuntu 20.04+、centos 8+):
resolvectl status
该命令会显示每个网络接口的 dns 配置、是否启用 dnssec、当前使用的解析器等详细信息。
⚠️ 旧版系统可能使用 systemd-resolve --status,但该命令已在较新版本中被弃用。
nm-tool | grep dns
nm-tool 在 networkmanager 1.0+ 版本中已被移除,请改用 nmcli。
总结对比
小贴士
测试 dns 是否生效,可使用以下命令验证:
dig example.com
host google.com
nslookup baidu.com
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论