绝大多数的 Linux 发行版都是在 /etc/resolv.conf 文件中设置 DNS 服务器,然而到了 Ubuntu 这里,这个方法不 work 了。即使强行手动修改这个文件,系统仍然会自动恢复到如下的默认设置。
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0
网上有文章介绍说需要修改 /etc/systemd/resolved.conf ,然而你会发现 DNS 的配置还是不能正常生效。甚至有人建议把 systemd-resolved 服务停掉,这就更不是官方推荐的做法了。实际上,Ubuntu 使用了 netplan 工具来配置网络。修改 DNS,需要在 netplan 的配置文件 /etc/netplan/*.yaml 修改。通常,配置文件会是 01-xxx.yaml 的形式,内容一般如下
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
eno1:
addresses: [xxx.xxx.xxx.xxx/24]
gateway4: xxx.xxx.xxx.1
nameservers:
addresses:
- "8.8.8.8"
只需要把 DNS 服务器的地址加入 nameservers 下面的 addresses 列表里(如上述的 8.8.8.8)就可以了。
请注意,修改完配置文件后,需要运行
netplan apply
以使新配置生效。
这时可以运行如下命令查看系统所用的 DNS
systemd-resolve --status
就能发现我们的新设置已经生效了。
参考链接
- netplan 官方网站 https://netplan.io/
- Netplan——新一代网络配置工具 https://www.jianshu.com/p/174656635e74