在 Linux 上查看和刷新 DNS 缓存的方法取决于系统使用的 DNS 解析服务。以下是常见情况的解决方案:
sudo journalctl -u systemd-resolved --no-pager | grep cache
# 或查看详细解析记录
sudo systemd-resolve --statistics
sudo strings /var/cache/nscd/hosts
sudo kill -SIGUSR1 $(pgrep dnsmasq) # 触发日志输出
sudo tail -f /var/log/daemon.log # 查看日志
sudo systemd-resolve --flush-caches
# 验证是否清空
sudo systemd-resolve --statistics | grep 'Current Cache Size'
sudo systemctl restart nscd
# 或直接清除缓存文件
sudo rm -f /var/cache/nscd/hosts
sudo nscd -i hosts
sudo systemctl restart dnsmasq
# 或发送刷新信号
sudo kill -SIGUSR1 $(pgrep dnsmasq)
# 重启所有可能涉及的服务
sudo systemctl restart systemd-resolved nscd dnsmasq
# Chrome/Edge 地址栏访问:
chrome://net-internals/#dns
# Firefox 地址栏访问:
about:networking#dns
dig example.com | grep "Query time" # 观察查询时间变化
建议根据您的具体发行版选择对应方法,大多数现代发行版(如 Ubuntu 18.04+)优先使用 systemd-resolved。