AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1536806
Accepted
Ken Mandelberg
Ken Mandelberg
Asked: 2024-12-31 02:23:10 +0800 CST2024-12-31 02:23:10 +0800 CST 2024-12-31 02:23:10 +0800 CST

在 Ubuntu 24.04 上永久添加第二个名称服务器

  • 772

我需要添加第二个名称服务器。我可以在 resolve.conf 中临时添加,但我想要一个重启后仍然有效的解决方案。

当自动关闭时,设置 GUI 会填写 DNS,但这似乎没有任何作用。

dns
  • 3 3 个回答
  • 81 Views

3 个回答

  • Voted
  1. Micronux
    2024-12-31T02:43:46+08:002024-12-31T02:43:46+08:00

    首先打开终端,然后输入以下命令:

    sudo su
    sudo systemctl status resolvconf.service
    sudo apt update
    

    如果您尚未安装 resolvconf,请先执行以下操作:

    sudo apt install resolvconf
    sudo systemctl enable resolvconf.service
    sudo systemctl start resolvconf.service
    sudo systemctl status resolvconf.service
    

    如果您已经安装了 resolvconf,请从这里完成安装:

    sudo nano /etc/resolvconf/resolv.conf.d/head
    sudo resolvconf --enable-updates
    sudo resolvconf -u
    sudo nano /etc/resolvconf
    
    • 0
  2. Alexandru Gologan-Dalimon
    2024-12-31T04:41:45+08:002024-12-31T04:41:45+08:00

    目前还不太清楚您正在进行什么样的组合乐趣。

    我假设您当前拥有的设置是默认的 systemd-resolved,并且可能是您正在连接的 DHCP 服务器正在刷新设置,如果您自动获取它们或者只是此刻根本没有获取任何东西。

    无论如何,解决该问题的一个可能方法是尝试对 /etc/systemd/resolved.conf 进行更改,方式与对 /etc/resolved.conf 所做的更改相同

    似乎 systemd-resolved 会让 /etc/systemd/resolved.conf 优先于您能够添加它的所有其他地方。

    此外,根据设置,您还可以引用 /etc/systemd/resolved.conf.d/ 中的内容,可以添加/修改这些内容,其结果与在 /etc/systemd/resolved.conf 中更改的结果相同。(所以最好也检查一下那里,以确保整洁)

    有关上述内容的更多参考/详细信息:

    https://wiki.archlinux.org/title/Systemd-resolved

    24.04 - 如何查找 /etc/resolv.conf 中更改 DNS 设置的“内容”

    https://forum.cloudron.io/topic/12384/no-dns-after-upgrade-ubuntu-22-04-to-24-04/5

    • 0
  3. Best Answer
    mpboden
    2024-12-31T12:55:51+08:002024-12-31T12:55:51+08:00

    定义 DNS 服务器的方法有很多种,每种方法都有各自的优缺点。我还将概述如何拥有搜索域。

    1. 通过 GUI 使用网络设置
    2. 通过 GUI 使用nm-connection-editor
    3. 通过定义全局 DNS 服务器和搜索域/etc/systemd/resolved.conf
    4. 使用命令nmcli

    但首先,不要编辑/etc/resolv.conf。此文件由系统管理,无需直接编辑。此文件中定义的名称服务器是127.0.0.53,它是本地缓存存根解析器。这是任何搜索中查询的第一个服务器,只有当查询不在缓存中时才会查询上行服务器。

    默认情况下,/etc/resolv.conf是 的符号链接/run/systemd/resolve/stub-resolv.conf。如果情况不再如此,并且它是您编辑的静态文件,请运行以下命令返回其默认状态:

    sudo rm /etc/resolv.conf
    sudo ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf`
    

    其次,无需安装resolveconf。可能仍会有一些用例,但对于您的需求,完全没有必要仅仅为了定义另一个 DNS 服务器而安装应用程序。


    注意:听起来您当前的网络设置是通过 DHCP 获取 IP 地址和任何 DNS 服务器。以下添加其他 DNS 服务器的示例假设使用 DHCP,而不是静态定义的 IP 地址。


    1. 通过网络设置中的 GUI 定义 DNS 服务器

    如果您想手动分配 DNS 服务器,而不是通过 DHCP 获取它们,只需取消选中“自动”框并定义它们。点击应用后,它们将生效,但请记住,这会覆盖通过 DHCP 推送给您的任何 DNS 服务器。此外,您无法使用此方法定义任何搜索域。

    举个例子:

    勾选“自动”后:

    在此处输入图片描述

    查看输出resolvectl以查看当前通过 DHCP 推送给您的 DNS 服务器:

    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: 208.65.212.2
           DNS Servers: 208.65.212.2 208.65.212.34
    

    在设置面板中手动配置DNS服务器后,点击应用:

    在此处输入图片描述

    检查输出resolvectl以查看更改是否生效。请注意,通过 DHCP 推送给您的名称服务器不再存在。

    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
           DNS Servers: 8.8.8.8 1.1.1.1
    

    2. 使用 GUI 定义 DNS 服务器nm-connection-editor

    打开终端并运行以下命令来打开nm-connection-editor:

    sudo -E nm-connection-editor
    

    将会打开一个窗口。单击您的连接,然后单击齿轮图标。

    在此处输入图片描述

    转到IPV4 设置选项卡。输入任何其他 DNS 服务器(以逗号分隔)和搜索域(也以逗号分隔)。然后点击保存。

    在此处输入图片描述

    关闭窗口,然后返回终端。使用 重新启动网络管理器sudo systemctl restart NetworkManager并查看 的输出resolvectl。您会注意到,您的链接中已添加了一个额外的 DNS 服务器以及您的自定义搜索域(列为 DNS 域)。

    $ sudo systemctl restart NetworkManager
    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: 8.8.8.8
           DNS Servers: 8.8.8.8 208.65.212.2 208.65.212.34
            DNS Domain: mycustomdomain
    

    3. 通过编辑定义 DNS 服务器和搜索域/etc/systemd/resolved.conf

    在 中定义 DNS 服务器和搜索域时/etc/systemd/resolved.conf,您定义的是全局DNS 服务器和搜索域,这意味着它们可用于所有链接。如果可以接受,请打开终端并使用 编辑文件sudo nano /etc/systemd/resolved.conf。

    要定义附加 DNS 服务器,请添加以下节:

    DNS=8.8.8.8
    

    要添加搜索域,请添加以下节:

    Domains=mycustomdomain
    

    保存并关闭后,运行sudo systemctl restart systemd-resolved。然后检查的输出。请注意,除了推送给您的 DNS 服务器之外resolvectl,现在还有一个全局DNS 服务器和搜索域(列为 DNS 域)。

    $ sudo systemctl restart systemd-resolved
    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
           DNS Servers: 8.8.8.8
            DNS Domain: mycustomdomain
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
           DNS Servers: 208.65.212.2 208.65.212.34
    
    

    4.使用命令定义DNS服务器nmcli

    首先需要找到连接名称。使用以下命令nmcli con:

    $ nmcli con
    NAME            UUID                                  TYPE      DEVICE 
    netplan-enp0s3  1eef7e45-3b9d-3043-bee3-fc5925c90273  ethernet  enp0s3 
    lo              ae2370cb-4852-4da8-8894-2acd8467ad5a  loopback  lo    
    

    从上面的命令可以看出,我的系统上的连接名称是netplan-enp0s3。请用以下命令替换您的名称:

    要定义附加 DNS 服务器:

    nmcli con mod "netplan-enp0s3" ipv4.dns "8.8.8.8"
    

    要定义搜索域:

    nmcli con mod "netplan-enp0s3" ipv4.dns-search "mycustomdomain"
    

    然后重新启动网络管理器并查看输出resolvectl:

    $ sudo systemctl restart NetworkManager
    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: 8.8.8.8
           DNS Servers: 8.8.8.8 208.65.212.2 208.65.212.34
            DNS Domain: mycustomdomain
    

    请注意,在本例中,它将 DNS 服务器和搜索域添加到特定链接,而不是作为全局设置。此外,它保留了通过 DHCP 推送的 DNS 服务器。

    如果您想要分配多个 DNS 服务器和一个搜索域并忽略通过 DHCP 推送给您的任何 DNS 服务器,请运行以下命令:

    $ nmcli con mod "netplan-enp0s3" ipv4.dns "8.8.8.8 1.1.1.1"
    $ nmcli con mod "netplan-enp0s3" ipv4.dns-search "mycustomdomain"
    $ nmcli con mod "netplan-enp0s3" ipv4.ignore-auto-dns yes
    $ sudo systemctl restart NetworkManager
    $ resolvectl
    Global
             Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
      resolv.conf mode: stub
    
    Link 2 (enp0s3)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: 8.8.8.8
           DNS Servers: 8.8.8.8 1.1.1.1
            DNS Domain: mycustomdomain
    
    

    最后,请注意,通过上述任何方法添加搜索域时,/etc/resolv.conf也会更新以反映附加的搜索域:

    $ cat /etc/resolv.conf 
    # This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
    # Do not edit.
    #
    # This file might be symlinked as /etc/resolv.conf. If you're looking at
    # /etc/resolv.conf and seeing this text, you have followed the symlink.
    #
    # 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 should typically 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 mycustomdomain
    
    • 0

相关问题

  • 将服务器从 DHCP 切换到静态 IP

  • “.local”有什么作用?

  • 如何在学校网络上配置域名[关闭]

  • 更改DNS IP的正确方法是什么?

  • 如何清除 DNS 缓存?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve