关键在于避开NetworkManager、systemd-resolved等服务覆盖:①推荐用systemd-resolved,改/etc/systemd/resolved.conf并重启服务;②桌面环境用nmcli配置连接级DNS;③Ubuntu Server用Netplan在YAML中静态声明nameservers。

用 systemd-resolved 统一配置(推荐,现代系统默认方案)
这是目前 Ubuntu、Fedora、Debian 等默认启用的机制,配置一次,全局生效,且不冲突。
- 编辑主配置:sudo nano /etc/systemd/resolved.conf
- 取消注释并修改这两行(示例用阿里和 Cloudflare DNS):
DNS=223.5.5.5 1.1.1.1
FallbackDNS=114.114.114.114 8.8.8.8
(可选)指定某域名走特定 DNS:Domains=~example.com - 确保 `/etc/resolv.conf` 指向 stub 解析器:
sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf - 重启服务:sudo systemctl restart systemd-resolved
- 验证:resolvectl status 查看 “DNS Servers” 是否已更新
用 NetworkManager 配置连接级 DNS(桌面或笔记本首选)
如果你用图形界面联网,或者 `nmcli` 是主力工具,硬改 resolv.conf 会被秒覆盖。必须通过 NetworkManager 设置。
- 查当前活跃连接名:nmcli con show --active | awk 'NR>1 {print $1}'
- 为该连接设 DNS(例如连接名为 “Wired connection 1”):
sudo nmcli con mod "Wired connection 1" ipv4.dns "223.5.5.5 119.29.29.29"
sudo nmcli con mod "Wired connection 1" ipv4.ignore-auto-dns yes - 重启连接生效:nmcli con down "Wired connection 1" && nmcli con up "Wired connection 1"
- 验证:nmcli con show "Wired connection 1" | grep dns
通过 Netplan 配置(Ubuntu Server 最稳妥方式)
Ubuntu 18.04+ 默认使用 Netplan 管理网络,DNS 可在 YAML 中静态声明,完全绕过运行时覆盖。
- 找到配置文件,通常为:/etc/netplan/*.yaml(如 `01-network-manager-all.yaml`)
- 在对应网卡段添加 `nameservers` 块(注意缩进):
nameservers:
addresses: [223.5.5.5, 114.114.114.114] - 应用配置:sudo netplan apply
- 确认生效:systemd-resolve --status 或 resolvectl status
resolvectl query github.com(看“Using DNS Server”)
dig +short google.com @223.5.5.5(直连指定服务器)


















