ubuntu 18.04 版本永久修改DNS

需求:在最近的一次项目中有个ubuntu 18.04 版本,我们在修改/etc/resolv.conf后,系统重启 DNS未保存,DNS服务器是:10.1.11.155,用于解析www.test.com域名

操作:

        我们先查看/etc/resolv.conf文件

test@ubuntu:~$ cat /etc/resolv.conf 
# 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 "systemd-resolve --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

        会发现/etc/resolv.conf文件是由systemd-resolved这个服务来控制的

        我们先修改systemd-resolved服务的配置文件

test@ubuntu:~$ cat /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=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
DNS=10.1.11.155

然后重启服务

test@ubuntu:~$ sudo systemctl restart systemd-resolved.service

我们查看下现在的DNS

test@ubuntu:~$ sudo systemd-resolve --status
Global
         DNS Servers: 10.1.11.155
          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 (ens33)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 114.114.114.114
          DNS Domain: ~.

可以看到在global里面已经有了我们需要配置的DNS Servers: 10.1.11.155

现在我们在看看/etc/resolv.conf文件

test@ubuntu:~$ cat /etc/resolv.conf 
# 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 "systemd-resolve --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

发现未变,然后我们在ls -la /etc/resolv.conf

test@ubuntu:~$ ls -la /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Aug 23  2024 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

原来是个链接文件,我们在看看链接的文件

test@ubuntu:~$ cat /run/systemd/resolve/stub-resolv.conf
# 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 "systemd-resolve --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

该文件没变,我们在ls -la /run/systemd/resolve

test@ubuntu:~$ ls -la /run/systemd/resolve
total 8
drwxr-xr-x  3 systemd-resolve systemd-resolve 100 Aug 23 00:46 .
drwxr-xr-x 22 root            root            500 Aug 23 00:46 ..
drwx------  2 systemd-resolve systemd-resolve  60 Aug 22 22:22 netif
-rw-r--r--  1 systemd-resolve systemd-resolve 615 Aug 23 00:46 resolv.conf
-rw-r--r--  1 systemd-resolve systemd-resolve 715 Aug 23 00:46 stub-resolv.conf

发现还有个resolv.conf文件,我们在看看这个文件

test@ubuntu:~$ cat /run/systemd/resolve/resolv.conf 
# 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 10.1.11.155
nameserver 114.114.114.114

这时候才恍然大悟,原来我们修改的/etc/systemd/resolved.conf配置文件是在这里生效的,那我们能不能把这个/etc/resolv.conf文件与/run/systemd/resolve/resolv.conf产生关系呢,答案是可以的,我们只用重新生成个链接文件就好了

test@ubuntu:~$ sudo mv /etc/resolv.conf /etc/resolv.conf.bak
test@ubuntu:~$ sudo ln -s /run/systemd/resolve/resolv.conf /etc

我们在来查看下

test@ubuntu:~$ cat /etc/resolv.conf
# 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 10.1.11.155
nameserver 114.114.114.114

达到了我们想要的效果,这个时候我们nslookup www.test.com成功了,但是又有个问题来了,ping www.test.com失败了

我们修改下nsswitch配置文件

test@ubuntu:~$ cat /etc/nsswitch.conf 
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat systemd
group:          compat systemd
shadow:         compat
gshadow:        files

#hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
hosts:          files dns myhostname
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

我们在这里把这一行注释掉了

#hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
然后直接ping www.test.com成功

那么我们reboot看看是否还能生效

重启后还是生效的,问题解决!

---------------------------------------------------------------------------------------------------------------------------------

Q:在Ubuntu的nsswitch.conf文件中出现了 mdns4_minimal解析方式。mdns4_minimal是什么?

A:

hosts:按指定的行顺序尝试各种名称解析服务。默认情况是:

1. 首先检查/etc/hosts文件。如果该文件为正在讨论的主机名提供IP地址,则使用它。

2. 否则,请尝试mdns4_minimum,它仅在名称以.local结尾时才尝试通过MDNS(multicast DNS )解析名称。如果有,但是没有找到这样的mDNS主机,mdns4_minimum将返回NOTFOUND。NOTFOUND的缺省名称服务切换响应将尝试下一个列出的服务,但是[NOTFOUND=return]条目将覆盖该选项,并停止搜索,保持名称未解析状态。

3.然后尝试指定的DNS服务器。如果名称没有以.local结尾,这将或多或少地立即发生,如果以.local结尾,则根本不会发生。如果你删除[NOTFOUND=return]条目,nsswitch将尝试通过单播DNS找到解析.local主机的方案。这通常是一件坏事,因为它会将许多这样的请求发送到Internet DNS服务器,而这些服务器本可能永远没机会解决这些请求。显然,这种情况经常发生。

这个解答参考了mdns4_minimal是什么-CSDN博客

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值