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
如果是普通文件,说明链接被破坏,需要重建。
编辑配置文件
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
如果前面 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.comcat > 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