systemd-resolved DNS缓存可通过五种方法清理:一、用sudo systemd-resolve --flush-caches刷新;二、重启服务;三、systemctl restart;四、同步重启NetworkManager并刷新缓存;五、手动清空/run/systemd/resolve/cache/目录。

如果您在Linux系统中遇到域名解析异常、网站无法访问或IP地址更新不及时等问题,可能是由于systemd-resolved服务缓存了过期的DNS记录。以下是针对systemd-resolved服务清理DNS缓存的多种操作方法:
一、直接刷新systemd-resolved DNS缓存
该方法通过调用systemd-resolve内置命令清空当前DNS缓存条目,不中断服务运行,适用于需要最小化影响的场景。
1、打开终端,执行以下命令检查systemd-resolved服务是否处于活动状态:sudo systemctl is-active systemd-resolved
2、若返回active,继续执行缓存刷新命令:sudo systemd-resolve --flush-caches
3、验证缓存是否已清空,运行统计命令查看缓存条目数:systemd-resolve --statistics | grep "Cache"
二、重启systemd-resolved服务
重启服务会终止当前进程并重新加载配置,强制重建全部缓存,同时重置内部状态,可解决因服务内部异常导致的解析失败问题。
1、在终端中输入以下命令停止服务:sudo systemctl stop systemd-resolved
2、等待约2秒后启动服务:sudo systemctl start systemd-resolved
3、确认服务已正常运行:sudo systemctl is-active systemd-resolved
三、使用systemctl一键重启服务
该方式将停止与启动合并为单条原子操作,避免手动分步可能遗漏的状态检查,适合脚本化或快速恢复场景。
1、在终端中执行重启命令:sudo systemctl restart systemd-resolved
2、立即验证服务状态是否为active:sudo systemctl is-active systemd-resolved
3、检查DNS解析功能是否恢复正常,例如执行:nslookup example.com
四、配合NetworkManager同步刷新
当systemd-resolved与NetworkManager协同工作时,仅操作前者可能导致DNS配置未同步更新;此方法确保网络管理器向resolver推送最新DNS服务器列表。
1、先重启NetworkManager以更新DNS配置源:sudo systemctl restart NetworkManager
2、再刷新systemd-resolved缓存以应用新配置:sudo systemd-resolve --flush-caches
3、确认resolv.conf软链接指向正确解析器:ls -l /etc/resolv.conf
五、手动清除缓存文件(高级操作)
在极少数systemd-resolved版本存在缓存文件残留的情况下,可直接删除其持久化缓存目录内容,该操作需谨慎且仅在其他方法无效时使用。
1、确认缓存存储路径(通常为):sudo ls /run/systemd/resolve/stub-resolv.conf
2、停止systemd-resolved服务以防止写入冲突:sudo systemctl stop systemd-resolved
3、清空runtime缓存目录:sudo rm -f /run/systemd/resolve/cache/*










