Ubuntu20.04设置DNS后自动重置问题

Linux 系统通常通过修改/etc/resolv.conf 来设置DNS,原始内容如下:

nameserver 127.0.0.53
options edns0 trust-ad

添加新的dns,例如:

nameserver 8.8.8.8
nameserver 114.114.114.114

但是在使用过程中,发现过一会儿/etc/resolv.conf就被重置了,又变回了原始的内容;而且reboot重启之后,/etc/resolv.conf也会被重置。

仔细查看才发现/etc/resolv.conf本身在第一行的注释里面已经写了“Do not edit"。

[email protected]:~# cat /etc/resolv.conf.bak 
# 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 "resolvectl 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 trust-ad

这个文件是由man:systemd-resolved(8) 管理,/etc/resolv.conf 是一个动态生成的文件,当你尝试看man的手册页,你会找到另一个配置, 并且注意到下面的语句

man systemd-resolved
```
The DNS servers contacted are determined from the global settings in /etc/systemd/resolved.conf
```

Ubuntu20.04设置DNS后自动重置问题_第1张图片

 

解决:

经过查阅相关资料,找到如下方法可以修改Ubuntu20.04 LTS版本的DNS

首先修改 /etc/systemd/resolved.conf 文件,在其中添加dns信息,例如:

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

保存退出。

然后以root身份在终端中依次执行如下命令:

sudo systemctl restart systemd-resolved
sudo systemctl enable systemd-resolved
 
sudo mv /etc/resolv.conf  /etc/resolv.conf.bak
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

再查看/etc/resolv.conf文件就可以看到新的dns信息已经写入其中了,接下来再使用就不会被重置。

你可能感兴趣的:(Linux,Ubuntu,linux)