Debian
安装resolvconf解决debian静态ip下/etc/resolv.conf被覆盖导致DNS解析失败问题
09-08 11:081、问题现象
在 debian 的 /etc/network/interfaces 中设置为静态 IP,并已正确填写 dns-nameservers,执行 apt update 时,所有软件源均报错:
Temporary failure resolving 'mirrors.tuna.tsinghua.edu.cn'
Temporary failure resolving 'security.debian.org'
...
同时,ping 8.8.8.8 能通,但 ping qq.com 等网址不通。
通过命令 cat /etc/resolv.conf 查看 dns 信息,发现没有 nameserver 条目
# Generated by dhcpcd
# /etc/resolv.conf.head can replace this line
# /etc/resolv.conf.tail can replace this line
2、问题原因
原因大概率是系统中安装了 dhcpcd(which dhcpcd 能找到),但并未作为服务运行。然而在每次网络启动时,dhcpcd 的钩子脚本(/usr/lib/dhcpcd/dhcpcd-hooks/20-resolv.conf)会覆盖 resolv.conf,即使配置文件里已写了 nohook resolv.conf(该选项在某些情况下失效)。
3、解决方案
推荐安装 resolvconf,安装后 /etc/network/interfaces 里的 dns-nameservers 设置就能被自动识别并写入 /etc/resolv.conf
⑴ 临时解决 DNS,以便安装软件包
tee /etc/resolv.conf > /dev/null <<EOF
nameserver 8.8.8.8
nameserver 114.114.114.114
EOF
⑵ 安装 resolvconf 包
apt update
apt install resolvconf
⑶ 重启网络服务(将 ens18 替换成实际的网卡名)
sudo ifdown ens18 && sudo ifup ens18
⑷ 验证
cat /etc/resolv.conf
此时,文件内容应该由 resolvconf 生成,并包含在 /etc/network/interfaces 里设置的 DNS 服务器
