You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20.04桌面版命令行设置DNS及配置不生效问题求助

Ubuntu 20.04桌面版命令行设置DNS及配置不生效问题求助

各位好,我正在Ubuntu 20.04桌面版上尝试配置自定义DNS服务器,但遇到了配置不生效的问题,折腾了好一阵还是没找到根源,想请教下大家的思路。

我先梳理下目前的配置情况:

1. /etc/systemd/resolved.conf 配置

这个文件里所有选项都是注释状态,说明没有在这里设置DNS:

#  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
#DNSOverTLS=no
#Cache=no-negative
#DNSStubListener=yes
#ReadEtcHosts=yes

2. /etc/resolv.conf 内容

这个文件是systemd-resolved管理的动态文件,指向本地的DNS stub resolver:

# 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
search *domain*.com

3. Netplan配置文件(/etc/netplan/**-netconf-**.yaml)

我在这里配置了禁用DHCP的DNS,并且指定了自定义的DNS地址(为了安全用***********替换了真实IP):

network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      dhcp4: yes
      dhcp4-overrides:
        use-dns: false
      dhcp6: yes
      dhcp6-overrides:
        use-dns: false
      nameservers:
        addresses:
        - ***********
        - ***********
        - ***********

问题所在

但是当我运行systemd-resolve --status或者resolvectl status时,显示的DNS服务器和我在Netplan里配置的完全不一样(以下是示例,真实域名和DNS做了模糊处理):

Link 2 (ens160)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 8.8.8.8
       DNS Servers: 8.8.8.8
                    8.8.4.4
                    8.8.2.2
        DNS Domain: **domain**.com

我发现/run/systemd/resolve/resolv.conf里的DNS地址和上面status显示的一致,但如果我手动修改这个文件里的DNS,然后执行sudo systemctl restart systemd-resolved,文件会立刻恢复成原来的内容,那些旧的DNS又回来了。

我现在完全搞不懂这些DNS地址到底是从哪里获取的,明明已经在Netplan里禁用了DHCP的DNS,为什么还是会被覆盖?怎么才能让我配置的自定义DNS生效呢?

备注:内容来源于stack exchange,提问作者Vlad Belo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 08:14:50