1. 首页 > Linux教程 > 正文

Linux教程FG194-etc-resolv-conf DNS配置

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文档详细介绍/etc/resolv.conf文件的配置和使用,包

风哥提示:

括DNS服务器配置、域名搜索、故障排查等。

Part01-查看DNS配置

1.1 查看resolv.conf文件

# 查看resolv.conf文件
$ cat /etc/resolv.conf
# Generated by NetworkManager
search fgedu.更多视频教程www.fgedu.net.cnnet.cn
nameserver 192.168.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4

# 查看文件权限
$ ls -la /etc/resolv.conf
lrwxrwxrwx. 1 root root 39 Jan 1 00:00:00 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

# 查看实际文件内容
$ cat /run/systemd/resolve/stub-resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. Any changes
# to this file will be overwritten.
#
# Run “resolvectl status” to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or create a
# symlink to a file under /run/systemd/resolve/resolv.conf.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search fgedu.net.cn

Part02-配置DNS服务器

2.1 使用nmcli配置DNS

# 查看当前网络连接
$ nmcli connection show
NAME UUID TYPE DEVICE
ens33 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet ens33

# 查看当前DNS配置
$ nmcli connection show ens33 | grep -i dns
ipv4.dns:from PG视频:www.itpux.com 192.168.1.1,8.8.8.8
ipv4.dns-search: —
ipv4.dns-options: —
ipv6.dns: —
ipv6.dns-search: —
ipv6.dns-options: —

# 设置DNS服务器
$ sudo nmcli connection mod ens33 ipv4.dns “192.168.1.1 8.8.8.8 8.8.4.4”

# 设置DNS搜索域
$ sudo nmcli connection mod ens33 ipv4.dns-search “fgedu.net.cn localdomain”

# 重启网络连接使配置生效
$ sudo nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)

# 验证DNS配置
$ nmcli connection show ens33 | grep -i dns
ipv4.dns: 192.168.1.1,8.8.8.8,8.8.4.4
ipv4.dns-search: fgedu.net.cn,localdomain
ipv4.dns-options: —
ipv6.dns: —
ipv6.dns-search: —
ipv6.dns-options: —

# 查看resolv.conf文件
$ cat /etc/resolv.conf
# Generated by NetworkManager
search fgedu.net.cn localdomain
nameserver 192.168.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4

Part03-测试DNS解析

3.1 使用nslookup测试DNS

# 安装nslookup工具
$ sudo dnf install -y bind-utils

# 测试DNS解析
$ nslookup www.google.com
Server: 192.168.1.1
Address: 192.168.1.1#53

Non-authoritative answer:
Name: www.google.com
Address: 142.250.185.196
Name: www.google.com
Address: 2607:f8b0:4004:800::2004

# 测试反向DNS解析
$ nslookup 192.168.1.1
1.1.168.192.in-addr.arpa name = router.fgedu.net.cn.

# 查看DNS服务器信息
$ nslookup -type=NS fgedu.net.cn
Server: 192.168.1.1
Address: 192.168.1.1#53

Non-authoritative answer:
fgedu.net.cn nameserver = ns1.fgedu.net.cn.
fgedu.net.cn nameserver = ns2.fgedu.net.cn.

3.2 使用dig测试DNS

# 使用dig测试DNS解析
$ dig www.google.com

; <<>> DiG 9.16.23-RH <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12345 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; QUESTION SECTION: ;www.google.com. IN A ;; ANSWER SECTION: www.google.com. 300 IN A 142.250.185.196 ;; Query time: 23 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Thu Apr 03 14:30:00 CST 2026 ;; MSG SIZE rcvd: 59 # 查看DNS响应时间 $ dig +stats www.google.com | grep "Query time" ;; Query time: 23 msec # 测试特定DNS服务器 $ dig @8.8.8.8 www.google.com ; <<>> DiG 9.16.23-RH <<>> @8.8.8.8 www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12346 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; QUESTION SECTION: ;www.google.com. IN A ;; ANSWER SECTION: www.google.com. 300 IN A 142.250.185.196 ;; Query time: 15 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Thu Apr 03 14:31:00 CST 2026 ;; MSG SIZE rcvd: 59

Part04-故障排查

4.1 DNS解析问题排查

# 场景:DNS解析失败
# 1. 检查resolv.conf文件
$ cat /etc/resolv.conf
# Generated by NetworkManager
search fgedu.net.cn
nameserver 192.168.1.1

# 2. 测试DNS服务器连通性
$ ping -c 2 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(更多学习教程公众号风哥教程itpux_com84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.124 ms
— 192.168.1.1 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.123/0.123/0.124/0.000 ms

# 3. 测试DNS端口53
$ nc -zv 192.168.1.1 53
Connection to 192.168.1.1 53 port [tcp/domain] succeeded!

# 4. 测试DNS解析
$ nslookup www.google.com
Server: 192.168.1.1
Address: 192.168.1.1#53

** server can’t find www.google.com: NXDOMAIN

# 5. 更换DNS服务器
$ sudo nmcli connection mod ens33 ipv4.dns “8.8.8.8 8.8.4.4”
$ sudo nmcli connection up ens33

# 6. 再次测学习交流加群风哥微信: itpux-com试DNS解析
$ nslookup www.google.com
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
Name: www.google.com
Address: 142.250.185.196

风哥针对配置建议:
1. 使用nmcli配置DNS,避免手动修改resolv.conf
2. 配置多个DNS服务器提高可靠性
3. 使用公共DNS服务器(8.8.8.8、1.1.1.1等)
4. 配置DNS搜索域简化域名访问
5. 定期测试DNS解析性能

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息