JavaScript is required
帮助文档

Debian12 修复 Temporary failure in name resolution

Debian12 默认使用 systemd-resolved/etc/resolv.conf 是符号链接,不要直接 vim 改写 resolv.conf,会破坏软链接,重启失效

第一步,先确认现象

# ping IP 看网络通不通
ping 223.5.5.5

# 看 resolv.conf 链接状态
ls -l /etc/resolv.conf

正常输出应当类似:

lrwxrwxrwx 1 root root ... /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf

如果是普通文件,说明链接被破坏,需要重建。

第二步:配置 systemd-resolved

编辑配置文件

nano /etc/systemd/resolved.conf

修改为:

[Resolve]
DNS=223.5.5.5 114.114.114.114
FallbackDNS=8.8.8.8
#DNSOverTLS=no
#MulticastDNS=no
#LLMNR=no
#Cache=no-negative

保存退出 Ctrl+O →回车→Ctrl+X

重启服务:

systemctl restart systemd-resolved
systemctl enable systemd-resolved

第三步:修复 /etc/resolv.conf 软链接(重点!Debian12 高频坑)

如果前面 ls -l 显示不是指向 stub-resolv.conf,执行:

rm -f /etc/resolv.conf
ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

第四步 测试解析

ping www.baidu.com
# 或者用dig验证
dig www.baidu.com

快捷方法:直接复制整条运行(不用本地传文件)

cat > set_dns.sh <<'EOF'
#!/bin/bash
set -e

CONF="/etc/systemd/resolved.conf"
DNS_LINE="DNS=223.5.5.5 114.114.114.114"

if ! grep -qxF "$DNS_LINE" "$CONF"; then
    sed -i '/^\[Resolve\]/a '"$DNS_LINE" "$CONF"
fi

systemctl restart systemd-resolved
systemctl enable systemd-resolved

rm -f /etc/resolv.conf
ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

echo "=== DNS配置完成 ==="
echo "查看resolv.conf: cat /etc/resolv.conf"
echo "查看dns状态: resolvectl status"
EOF

sed -i 's/\r$//' set_dns.sh
chmod +x set_dns.sh
bash set_dns.sh

本篇对您有帮助吗?