Ubuntu 20.04设置DNS解析(解决resolve.conf被覆盖问题)

众所周知

        Ubuntu在域名解析时,最直接使用的是/etc/resolve.conf文件,它是/run/systemd/resolve/

resolve.conf的软链接,而对于刚装完的ubuntu系统,该文件的内容如下:

# 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

问题分析:

        很显然,我们需要做的就是在/etc/resolve.conf中,配置我们需要的域名解析服务器地址,例如:“nameserver 8.8.8.8”,但是这个文件即使我们修改了,很快又会被覆盖,而且我们注意一个细节——文件的开头就注明了“Do not edit”,到了这里,我们就应该反应过来,直接修改/etc/resolve.conf是不正确的。

解决方案:

        第1步, 修改/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=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

       个人猜测,这个时候,系统在往/run/systemd/resolve/resolv.conf里面写域名解析服务器地址的时候,会从/etc/systemd/resolved.conf中取得DNS相关的配置。

        2,重启域名解析服务

                systemctl restart systemd-resolved
                systemctl enable systemd-resolved

        3,备份当前的/etc/resolve.conf,并重新设置/run/systemd/resolve/resolv.conf  到/etc/resolve.conf的软链接

                mv    /etc/resolv.conf    /etc/resolv.conf.bak
                ln  -s   /run/systemd/resolve/resolv.conf    /etc/

验证成果:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# 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 8.8.8.8
nameserver 114.114.114.114

感谢各个论坛大佬的分享,主要参考:

        https://www.cnblogs.com/mouseleo/p/14976527.html

  • 52
    点赞
  • 121
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
您可以在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解析服务的命令。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值