按照 Ubuntu 官方的方式来设置 DNS

绝大多数的 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

就能发现我们的新设置已经生效了。

参考链接

  1. netplan 官方网站 https://netplan.io/
  2. Netplan——新一代网络配置工具 https://www.jianshu.com/p/174656635e74
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值