先看 resolv.conf 归谁管
只读readlink -f /etc/resolv.conf
systemctl is-active systemd-resolved跑完应看到: 输出 /run/systemd/resolve 下的路径或 active,说明由 systemd-resolved 管理。到此为止,改 /etc/systemd/resolved.conf 里的 DNS,不要锁文件。
03 / FIELD NOTES
写入 DNS,再用 chattr 锁住 resolv.conf,避免被 DHCP 盖掉。
锁 resolv.conf 是云镜像反复盖 DNS 时的下策,不是默认建议。不可变文件会坑到下一个改机器的人。先写进去;真被盖掉了再锁。
DHCP 或 cloud-init 总是把你刚写的 DNS 盖掉。
若你依赖 DHCP 管 DNS,或用着 systemd-resolved,就别锁。
readlink -f /etc/resolv.conf
systemctl is-active systemd-resolved跑完应看到: 输出 /run/systemd/resolve 下的路径或 active,说明由 systemd-resolved 管理。到此为止,改 /etc/systemd/resolved.conf 里的 DNS,不要锁文件。
printf '%s\n' 'nameserver 1.1.1.1' 'nameserver 1.0.0.1' | tee /etc/resolv.conf跑完应看到: tee 打出两行 nameserver。
chattr +i /etc/resolv.conf跑完应看到: 之后要改,得先 chattr -i /etc/resolv.conf。
把 /etc/resolv.conf 设成不可变。之后要改,先执行 chattr -i /etc/resolv.conf。在重装后的 root 会话里执行。
{
"slug": "chattr-resolv",
"risk": "caution",
"tags": [
"dns",
"chattr",
"resolv.conf"
],
"updated": "2026-10-28",
"pathIndex": null,
"why": "锁 resolv.conf 是云镜像反复盖 DNS 时的下策,不是默认建议。不可变文件会坑到下一个改机器的人。先写进去;真被盖掉了再锁。",
"steps": [
{
"command": "readlink -f /etc/resolv.conf\nsystemctl is-active systemd-resolved",
"risk": "safe"
},
{
"command": "printf '%s\\n' 'nameserver 1.1.1.1' 'nameserver 1.0.0.1' | tee /etc/resolv.conf",
"risk": "caution"
},
{
"command": "chattr +i /etc/resolv.conf",
"risk": "caution"
}
]
}