解决 Ubuntu 中 systemd-resolved 陷入 DNS 环路打满 CPU
分析 Ubuntu systemd-resolved 报 DNS loop CPU 100% 诱因,详解 127.0.0.53 本地回环脱节与上游 DNS 规范配置。
问题现象
在 Ubuntu 20.04/22.04/24.04 服务器上,系统突然出现卡顿,通过 top 查看发现进程 systemd-resolved 持续占据 100% CPU:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
452 systemd-resolve 20 0 24512 18210 8120 R 99.9 0.2 45:10.12 systemd-resolve
系统日志 /var/log/syslog 疯狂刷屏:
systemd-resolved[452]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction.
机器域名解析完全瘫痪。
触发根因
这是 Linux 中由于网络配置冲突导致的本地 DNS 递归死循环(DNS Routing Loop):
- 上游 DNS 错误配置指向了本地回环:在
/etc/systemd/resolved.conf或 Netplan 中配置的外部 DNS 服务器,被误写成了127.0.0.1、127.0.0.53或本机网卡自身 IP。 - 当应用向本地解析器发起查询时,
systemd-resolved将该查询作为“上游递归请求”再次发给自己,无限递归转发产生报文风暴,导致 CPU 瞬间打满并耗尽线程。 - 存在两个相互冲突的网络管理守护进程(如 NetworkManager 与 systemd-resolved 同时重写
/etc/resolv.conf)。
诊断排查步骤
查看 systemd-resolved 绑定的真实上游 DNS 服务器:
resolvectl status
检查输出中的 Current DNS Server 与 DNS Servers 列表:若列表中赫然列有 127.0.0.1、127.0.0.53 或 localhost,100% 确认陷入环路!
解决方案
方案一:在 resolved.conf 中显式指定公共真实上游 DNS(破开环路)
编辑 /etc/systemd/resolved.conf:
[Resolve]
# 必须指定公网或权威真实 DNS,严禁包含 127.0.0.x
DNS=223.5.5.5 119.29.29.29 8.8.8.8
FallbackDNS=1.1.1.1
Domains=~.
DNSStubListener=yes
重启服务生效:
sudo systemctl restart systemd-resolved
方案二:确保 /etc/resolv.conf 正确软链接到 stub 文件
确保符号链接标准合法:
sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
方案三:若不需要本地缓存,直接绕过并停用 systemd-resolved
如果更倾向使用传统的静态 /etc/resolv.conf:
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved
sudo rm -f /etc/resolv.conf
echo "nameserver 223.5.5.5" | sudo tee /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
验证方法
再次执行 top 查看进程状态:
systemd-resolved CPU 占用回落至 0.0%,执行 curl -I https://www.google.com 域名瞬间秒级解析,系统性能恢复正常。


