Ubuntu 18.04/18.10 永久修改DNS的方法

    问题
    解决过程
    解决方法
    参考

问题

发现每次在/etc/resolv.conf 修改DNS之后,重启就会失效。

解决过程

在执行

sudo vi /etc/resolv.conf

之后发现有以下输出:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) 
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN 
# 127.0.0.53 is the systemd-resolved stub resolver. 
# run "systemd-resolve --status" to see details about the actual nameservers.

于是查找systemd-resolve相关资料
结果如下:

  • 创建 /etc/resolv.conf 软连接,并将其指向 /usr/lib/systemd/resolv.conf 文件(其中仅设置了单独一个 127.0.0.53 DNS服务器)。 这是推荐的首选方式。
  • 创建 /etc/resolv.conf 软连接, 并将其指向由 systemd-resolved 实时更新的 /run/systemd/resolve/resolv.conf 文件。注意,此文件中只包含所有已知的全局DNS服务器,而不包含针对特定网络接口设置的DNS服务器。 注意,应用程序不应该直接使用 /run/systemd/resolve/resolv.conf 文件, 而是应该继续使用 /etc/resolv.conf 文件。 使用这种方式,所有绕过本地 DNS API 的客户端也将同样绕开 systemd-resolved 服务, 直接使用已知的DNS服务器。
  • 由其他软件包或系统管理员维护 /etc/resolv.conf 的内容。 在这种情况下, systemd-resolved 将会从中读取全局DNS配置。 也就是说,systemd-resolved 只是一个读取 /etc/resolv.conf 配置文件的使用者, 而非此文件的提供者。

注意,上述三种处理方式是自动感知的(不需要特别的配置),完全取决于 /etc/resolv.conf 是否为软连接, 以及软连接指向的目标。

于是:

ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Feb 10 00:36 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

显然与上面的第二种情况类似,也就是说resolv.conf 现在归systemd管,果然找到了/etc/systemd/resolved.conf 文件:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

而里面也有我想找的DNS

解决方法

sudo vim /etc/systemd/resolved.conf 

然后修改DNS,例如:

[Resolve]
DNS=114.114.114.114
DNS=8.8.8.8
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

然后

systemctl restart systemd-resolved.service

查看结果:

$ systemd-resolve --status
Global
         DNS Servers: 114.114.114.114
                      8.8.8.8
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa


参考
systemd-resolved.service 中文手册[金步国]


本文转载自:https://blog.csdn.net/weixin_43640082/article/details/83859885

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐