更改 Ubuntu 18.04 安装期间提供的 DNS 服务器

更改 Ubuntu 18.04 安装期间提供的 DNS 服务器

在安装 Ubuntu Server 18.04 期间,我提供了错误的 DNS 服务器,比如说192.168.0.1。我现在想将其更改为其他服务器,比如说8.8.8.8,但找不到正确的位置来执行此操作。

我知道我可以在 中配置全局 DNS 设置/etc/systemd/resolved.conf。虽然这解决了问题,但我仍然在 中看到原始的错误服务器条目systemd-resolve --status

Global
     DNS Servers: 8.8.8.8
      DNSSEC NTA: 10.in-addr.arpa
                  16.172.in-addr.arpa
                  168.192.in-addr.arpa
                  17.172.in-addr.arpa
                  18.172.in-addr.arpa
                  19.172.in-addr.arpa
                  20.172.in-addr.arpa
                  21.172.in-addr.arpa
                  22.172.in-addr.arpa
                  23.172.in-addr.arpa
                  24.172.in-addr.arpa
                  25.172.in-addr.arpa
                  26.172.in-addr.arpa
                  27.172.in-addr.arpa
                  28.172.in-addr.arpa
                  29.172.in-addr.arpa
                  30.172.in-addr.arpa
                  31.172.in-addr.arpa
                  corp
                  d.f.ip6.arpa
                  home
                  internal
                  intranet
                  lan
                  local
                  private
                  test

Link 2 (ens3)
   Current Scopes: DNS
   LLMNR setting: yes
   MulticastDNS setting: no
   DNSSEC setting: no
   DNSSEC supported: no
   DNS Servers: 192.168.0.1
   DNS Domain: xyz.com

如果我尝试使用它sudo systemd-resolve --interface ens3 --set-dns 8.8.8.8来更改它,我会收到一条错误消息:

The specified interface ens3 is managed by systemd-networkd. Operation refused.
Please configure DNS settings for systemd-networkd managed interfaces directly in their .network files.

不幸的是,该/etc/systemd/network/目录是空的。那里没有*.network文件。

但是,我找到了一个配置文件,/run/systemd/network/10-netplan-ens3.network我可以在其中更改条目,但执行完后它会被重置为错误的值

sudo systemctl daemon-reload
sudo systemctl restart systemd-networkd
sudo systemctl restart systemd-resolved

现在,我如何永久更改链接特定设置?

编辑:感谢您的意见!

我更改了 DNS 服务器/etc/netplan/01-netcfg.yaml

输出cat /etc/netplan/01-netcfg.yaml

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      addresses: [ 192.168.0.2/24 ]
      gateway4: 192.168.0.1
      nameservers:
        search: [ xyz.com ]
        addresses:
            - "8.8.8.8"

(可能是因为粘贴的原因空格不完全正确......)

编辑 2:我仔细检查了空格

编辑3:@George Udosen 的回答解决了这个问题,非常感谢。

输出sudo netplan --debug apply

** (generate:10422): DEBUG: 00:54:03.168: Processing input file //etc/netplan/01-netcfg.yaml..
** (generate:10422): DEBUG: 00:54:03.168: starting new processing pass
** (generate:10422): DEBUG: 00:54:03.169: ens3: setting default backend to 1
** (generate:10422): DEBUG: 00:54:03.169: Generating output files..
** (generate:10422): DEBUG: 00:54:03.169: NetworkManager: definition ens3 is not for us (backend 1)
DEBUG:netplan generated networkd configuration exists, restarting networkd
DEBUG:no netplan generated NM configuration exists
DEBUG:device lo operstate is unknown, not replugging
DEBUG:netplan triggering .link rules for lo
DEBUG:device ens3 operstate is up, not replugging
DEBUG:netplan triggering .link rules for ens3

systemd-resolve --status现在列出了正确的 DNS 服务器:

...
Link 2 (ens3)
  Current Scopes: DNS
   LLMNR setting: yes
   MulticastDNS setting: no
   DNSSEC setting: no
   DNSSEC supported: no
   DNS Servers: 8.8.8.8
   DNS Domain: xyz.com

答案1

编辑您的 netplan 配置文件并删除旧的 dns 服务器名称并添加新的。使用 编辑文件sudo nano /etc/netplan/01-netcfg.yaml,您的文件应该类似于以下示例:

network:
    version:2 
    renderer: networkd
    ethernets:
         enp0s3:
             dhcp4: true
             nameservers:
                  search: [mydomain, otherdomain]
                  addresses: [10.10.10.1, 1.1.1.1]

addresses感兴趣的行是设置下的行nameserver。它也可以像这样写:

nameservers:
     search: 
         - mydomain
         - otherdomain
     addresses:
         - "10.10.10.1"
         - "1.1.1.1"

将那里的地址更改为您想要的地址。确保观察缩进。现在,保存文件并应用更改:

sudo netplan --debug apply

答案2

这是一个基于答案的脚本数据伍基

#!/bin/bash        
# Installing resolvconf package
    sudo apt install resolvconf
# Detecting a magic sentence to applies changes... or not
    grep "Make edits to /etc/resolvconf/resolv.conf.d/head." /etc/resolvconf/resolv.conf.d/head &> /dev/null
    if [ ! $? -eq 0 ]
    then
     echo '# Make edits to /etc/resolvconf/resolv.conf.d/head.' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.8.8' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.4.4' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
    fi
# restarting daemon...
    sudo service resolvconf restart
# Flushing former DNS caches (by security)
    sudo systemd-resolve --flush-caches
    sudo systemctl restart systemd-resolved.service
# and check if DNS changes was done... or not ;)
    nslookup askubuntu.com | grep "Server:"
    read $r

相关内容