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

本文介绍如何在Ubuntu 20.04 LTS系统中永久修改DNS设置,避免/etc/resolv.conf文件被系统重置。通过编辑/etc/systemd/resolved.conf并重启systemd-resolved服务,确保DNS更改生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"。

root@ubuntu20.04:~# 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 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信息已经写入其中了,接下来再使用就不会被重置。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值