Linux 上查看和刷新 DNS 缓存

DNS(Domain Name System)是互联网中用于将域名转换为对应 IP 地址的系统。在 Linux 系统中,DNS 缓存是一种将域名和 IP 地址映射关系缓存在本地的机制,可以加快域名解析速度,并减轻 DNS 服务器的负载。本文将深入探讨如何在 Linux 上查看和刷新 DNS 缓存,以及相关的详细内容和示例代码。

DNS 缓存的重要性

DNS 缓存在 Linux 系统中起着至关重要的作用。它可以减少 DNS 解析的时间,提高网络访问速度,并降低对 DNS 服务器的负载。当系统访问某个域名时,如果已经存在该域名的解析结果在本地缓存中,系统就无需再次向 DNS 服务器发送查询请求,而是直接从缓存中获取结果,从而节省了时间和网络带宽。

查看 DNS 缓存的方法

在 Linux 系统中,有多种方法可以查看 DNS 缓存的内容。

a. 使用 dig 命令

dig 命令是一种功能强大的 DNS 工具,可以用来查询 DNS 信息,包括域名解析结果和 DNS 缓存内容。

复制代码
dig example.com

输出结果:

复制代码
; <<>> DiG 9.16.1-Ubuntu <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62233
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
example.com.            604800  IN      A       93.184.216.34

;; Query time: 43 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Feb 19 12:00:00 UTC 2024
;; MSG SIZE  rcvd: 57

b. 查看 nscd 缓存

nscd(Name Service Cache Daemon)是一个负责管理系统名称服务缓存的守护进程。可以使用 nscd命令来查看 DNS 缓存的内容。

复制代码
nscd -g

输出结果:

复制代码
hosts cache:

        yes  cache is enabled
        yes  cache is persistent
        yes  cache is shared
        211  suggested size
        216320  total data pool size
        1408  used data pool size
        7200  seconds time to live for positive entries
        20  seconds time to live for negative entries
        20  cache hits on positive entries
        0  cache hits on negative entries
        3  cache misses on positive entries
        0  cache misses on negative entries
        100 % cache hit rate
        8  current number of cached values
        9  maximum number of cached values
        0  maximum chain length searched
        0  number of delays on rdlock
        0  number of delays on wrlock
        0  memory allocations failed

c. 查看 systemd-resolved 缓存

systemd-resolved 是一个负责解析 DNS 查询的系统服务,它也会维护一个 DNS 缓存。

可以使用 systemd-resolve 命令来查看缓存内容。

复制代码
systemd-resolve --statistics

输出结果:

复制代码
DNSSEC supported by current servers: no

Transactions               
Current Transactions: 0
  Total Transactions: 2422

Cache                      
  Current Cache Size: 15
          Cache Hits: 2312
        Cache Misses: 110

刷新 DNS 缓存的方法

有时候,可能需要手动刷新 DNS 缓存,以确保系统使用最新的 DNS 解析结果。

a. 使用 systemd-resolved

可以使用 systemd-resolved 命令来刷新 systemd-resolved 服务的 DNS 缓存。

复制代码
sudo systemd-resolve --flush-caches

b. 重启 nscd 服务

如果系统上运行了 nscd 服务,您可以尝试通过重启服务来刷新 DNS 缓存。

复制代码
sudo systemctl restart nscd

c. 清除 DNS 缓存文件

还可以手动删除 DNS 缓存文件来清除 DNS 缓存。

复制代码
sudo rm -rf /var/cache/bind/named_dump.db

高级用法和注意事项

a. 使用 DNS 缓存优化性能

DNS 缓存可以显著提高域名解析的速度,减少对 DNS 服务器的请求压力。通过合理配置 DNS 缓存,可以进一步优化系统性能和网络连接速度。可以调整 DNS 缓存的大小和超时时间来满足需求。

示例代码:
复制代码
# 调整 systemd-resolved 的 DNS 缓存大小和超时时间
sudo systemctl edit systemd-resolved.service

# 查看 systemd-resolved 的配置
cat /etc/systemd/resolved.conf

b. 注意 DNS 缓存一致性

在刷新 DNS 缓存时,需要注意确保所有相关的 DNS 缓存都被刷新,以避免出现 DNS 缓存不一致的情况。这可能会导致应用程序无法访问特定的域名或者连接到错误的 IP 地址。

示例代码:
复制代码
# 刷新 systemd-resolved 服务的 DNS 缓存
sudo systemd-resolve --flush-caches

c. 避免频繁刷新 DNS 缓存

虽然刷新 DNS 缓存可以解决一些 DNS 相关的问题,但是过于频繁地刷新 DNS 缓存可能会影响系统性能和网络连接速度。建议在必要时才刷新 DNS 缓存,并在刷新之前确保了解刷新操作的影响。

示例代码:
复制代码
# 重启 nscd 服务
sudo systemctl restart nscd

# 清除 DNS 缓存文件
sudo rm -rf /var/cache/bind/named_dump.db

总结

在 Linux 系统中,查看和刷新 DNS 缓存是管理网络连接和优化系统性能的重要步骤。通过使用命令行工具如 dignscdsystemd-resolved,可以轻松地查看当前系统的 DNS 缓存信息,并采取必要的措施来刷新缓存以确保最新数据的可用性。了解如何正确地管理 DNS 缓存,可以帮助大家更有效地管理网络资源,提高系统的稳定性和性能。

如果你觉得文章还不错,请大家 点赞、分享、留言 ,因为这将是我持续输出更多优质文章的最强动力!

相关推荐
小五传输6 分钟前
内外网文件交换系统产品推荐 解决隔离网络文件交换5类难题
大数据·运维·安全
AOwhisky7 分钟前
Linux(CentOS)系统管理入门笔记(第一期)——从 Multics 到主流发行版
linux·运维·笔记·centos·云计算
从零开始的代码生活_7 分钟前
Linux epoll 多路转接详解
linux·运维·网络·后端·tcp/ip·计算机网络·php
渣渣盟16 分钟前
Linux软件管理与编辑器命令速查手册
linux·运维·编辑器
姚不倒16 分钟前
F5 健康检查 Monitor 深入篇
运维·负载均衡·f5
小鹿软件办公19 分钟前
通过调整编码参数使用 VLC 媒体播放器无损压缩视频方法
运维·网络·音视频
无足鸟ICT27 分钟前
【RHCA+】扩展正则表达式
linux·正则表达式
nVisual43 分钟前
nVisual:AI驱动的智能排障,平均响应时间缩短40%
android·人工智能·缓存
一池秋_1 小时前
ubuntu(linux)完美复刻windows11字体,解决linux字体费眼晴
linux·运维·ubuntu
随风一样自由1 小时前
【WSL+Linux】国内网络连接微软官方服务器(或GitHub)不稳定,导致下载内核或Linux发行版时超时断开如何解决?
linux·服务器·wsl