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

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信息已经写入其中了,接下来再使用就不会被重置。

  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以在Ubuntu 20.04设置DNS解析通过安装和配置Dnsmasq。Dnsmasq是一个简单且易于使用的DNS转发器,它可以用作本地DNS服务器和DHCP服务器。 在设置之前,请确保您已满足以下先决条件: 1. 运行Ubuntu 20.04的服务器。 2. 禁用Systemd解析服务,可以通过运行以下命令实现: 接下来,以下是在Ubuntu 20.04设置DNS解析的步骤: 1. 安装Dnsmasq: 在终端中运行以下命令安装Dnsmasq: ``` sudo apt update sudo apt install dnsmasq ``` 2. 配置Dnsmasq: 打开Dnsmasq配置文件: ``` sudo nano /etc/dnsmasq.conf ``` 在文件中,您可以配置Dnsmasq的各种选项。以下是一些常见的设置: - 添加DNS解析的上游服务器,即DNS服务器: ``` server=8.8.8.8 server=8.8.4.4 ``` - 配置本地域名: ``` local=/lab/ domain=lab ``` - 配置DHCP范围(可选): ``` dhcp-range=192.168.0.100,192.168.0.200,12h ``` 您可以根据自己的需求进行配置。完成后保存文件并退出。 3. 重启Dnsmasq服务: 运行以下命令以重新启动Dnsmasq服务,并使配置生效: ``` sudo systemctl restart dnsmasq ``` 4. 配置网络设置: 编辑网络设置文件以将Dnsmasq作为DNS服务器。打开以下文件: ``` sudo nano /etc/network/interfaces ``` 在文件中,将以下行添加到适当的接口配置段中: ``` dns-nameservers 127.0.0.1 ``` 保存文件并退出。 5. 重新启动网络服务: 运行以下命令以重新启动网络服务: ``` sudo systemctl restart networking ``` 现在,您的Ubuntu 20.04服务器上的DNS解析已经设置完毕。Dnsmasq将作为本地DNS服务器响应DNS查询,并根据您的配置将其转发到上游DNS服务器。 请注意,这只是一种设置DNS解析的方法之一。还有其他方法,但Dnsmasq是一种常用且易于配置和使用的选择。 :引用了目标3 x大师3 x工人kubeadm , kubelet , kubectl :v1.20.2 containerd前提条件6个“合适的” Ubuntu实例Ubuntu 20.04.2 通过sudo以完全管理员sysop使用用户sysop 主机名master[0-2] worker[0-2] DNS解析*.lab.... :引用了Dnsmasq的描述和功能说明。 :引用了禁用Systemd解析服务的命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值