尝试设置 DHCP IP 和手动 DNS 服务器

尝试设置 DHCP IP 和手动 DNS 服务器

我的 Ubuntu 18.04 笔记本电脑通过 DHCP 连接到 WiFi,但我想手动在网络中设置一个唯一的 DNS 服务器。

当我使用以下配置设置 /etc/resolv.conf 时,它运行良好,但如您所知,这些设置是暂时的:

#nameserver 127.0.0.53
#options edns0
nameserver 192.168.1.50

结果:

$ nslookup router.home.com
Server:     192.168.1.50
Address:    192.168.1.50#53

Name:   router.home.com
Address: 192.168.1.1

为了使此设置永久生效,我发现我可以使用 netplan,但对我来说不起作用,如下所示:

nslookup router.home.com 服务器:127.0.0.53 地址:127.0.0.53#53 ** 服务器找不到 router.home.com:NXDOMAIN

我当前的配置是:

$ ip addr
3: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:23:4e:26:36:3e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.43/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp1s0
       valid_lft 84380sec preferred_lft 84380sec
    inet6 fe80::340e:32d:6bf5:a2cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Netplan 配置:

$ ls /etc/netplan
01-network-manager-all.yaml

sudo nano /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  wifis:
    wlp1s0:
      dhcp4: yes
      dhcp4-overrides:
        use-dns: no
      access-points:
    "**SSID**":
      password: "**PASSWORD**"
      nameservers:
    addresses: [192.168.1.50]

这是应用更改后的输出:

$ sudo netplan apply
$ sudo netplan --debug apply
** (generate:12411): DEBUG: 14:56:58.874: Processing input file /etc/netplan/01-network-manager-all.yaml..
** (generate:12411): DEBUG: 14:56:58.874: starting new processing pass
** (generate:12411): DEBUG: 14:56:58.874: wlp1s0: adding wifi AP '**SSID**'
** (generate:12411): DEBUG: 14:56:58.874: wlp1s0: setting default backend to 2
** (generate:12411): DEBUG: 14:56:58.874: Configuration is valid
** (generate:12411): DEBUG: 14:56:58.874: Generating output files..
** (generate:12411): DEBUG: 14:56:58.874: networkd: definition wlp1s0 is not for us (backend 2)
DEBUG:no netplan generated networkd configuration exists
DEBUG:netplan generated NM configuration changed, restarting NM
DEBUG:wlp1s0 not found in {}
DEBUG:Merged config:
network:
  bonds: {}
  bridges: {}
  ethernets: {}
  vlans: {}
  wifis:
    wlp1s0:
      access-points:
        **SSID**:
          password: **PASSWORD**
      dhcp4: true
      dhcp4-overrides:
        use-dns: false
      nameservers:
        addresses:
        - 192.168.1.50

DEBUG:Skipping non-physical interface: lo
DEBUG:Skipping non-physical interface: enp2s1
DEBUG:{}
DEBUG:netplan triggering .link rules for lo
DEBUG:netplan triggering .link rules for enp2s1
DEBUG:netplan triggering .link rules for wlp1s0

测试我发现任何变化:

$ systemd-resolve --status
Global
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.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 3 (wlp1s0)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 192.168.1.1

Link 2 (enp2s1)
      Current Scopes: none
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no

即使我禁用了 systemd-resolved 服务,NetworkManager 也会动态修改符号链接 resolv.conf,我使用以下命令修复了该问题:

$ sudo rm /etc/resolv.conf

编辑 resolv.conf:

$ sudo nano /etc/resolv.conf
nameserver ::1
nameserver 127.0.0.1
nameserver 192.168.1.1

我放置了覆盖保护标志:$ sudo chattr +i /etc/resolv.conf

权限:

$ ls -al /etc/resolv.conf
-rw-r--r-- 1 root root 71 jul 18 17:24 /etc/resolv.conf
    %

先感谢您

答案1

不要手动编辑/etc/resolv.conf

/etc/netplan/*.yaml将您的文件设置为...

network:
  version: 2
  renderer: NetworkManager

编辑您的无线连接配置文件...

  • 自动 (DHCP)

  • DNS 自动关闭

  • DNS 字段中以逗号分隔的 DNS 服务器

在此处输入图片描述

更新#1:

您已严重禁用部分网络软件。您需要将其全部恢复...

  • 重新启用 systemd-resolved

  • 删除当前的 /etc/resolv.conf 并重置符号链接

  • 将您的 /etc/netplan/*.yaml 更改为我指示的内容

  • 撤消所做的其他更改

  • 按照我原来的答案

相关内容