ubuntu 18.04 版本永久修改DNS
这时候才恍然大悟,原来我们修改的/etc/systemd/resolved.conf配置文件是在这里生效的,那我们能不能把这个/etc/resolv.conf文件与/run/systemd/resolve/resolv.conf产生关系呢,答案是可以的,我们只用重新生成个链接文件就好了。需求:在最近的一次项目中有个ubuntu 18.04 版本,我们在修改/etc/resolv.conf后,系统重
需求:在最近的一次项目中有个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博客
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------还有一种方法,是参考了这边文章重新夺回对 /etc/resolv.conf 的控制权 · 云原生实验室 (icloudnative.io)
在ubuntu 18.04版本里面有2个服务控制了/etc/resolv.conf
1.我们从上面得知/etc/resolv.conf是一个软连接文件,链接到/run/systemd/resolve/stub-resolv.conf
我们先把systemd-resolved.service关闭掉
test@ubuntu:~$ sudo systemctl stop systemd-resolved.service
[sudo] password for test:
test@ubuntu:~$ systemctl status systemd-resolved.service
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:systemd-resolved.service(8)
https://www.freedesktop.org/wiki/Software/systemd/resolved
https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
test@ubuntu:~$ sudo -i
root@ubuntu:~# systemctl stop systemd-resolved.service
root@ubuntu:~# systemctl disable systemd-resolved
root@ubuntu:~# systemctl status systemd-resolved.service
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:systemd-resolved.service(8)
https://www.freedesktop.org/wiki/Software/systemd/resolved
https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
2.关闭后再重启查看/etc/resolv.conf,发现文件又被NetworkManager服务管控了
我们在修改下NetworkManager服务配置,添加一个dns=none
root@ubuntu:~# cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifupdown,keyfile
dns=none
[ifupdown]
managed=false
[device]
wifi.scan-rand-mac-address=no
在重启服务systemctl restart NetworkManager.service
然后再rm -rf /etc/resolv.conf,在创建/etc/resolv.conf
root@ubuntu:~# vim /etc/resolv.conf
nameserver 11.0.1.18
options single-request-reopen
options single-request
3.重启,然后再查看/etc/resolv.conf文件验证
OK 可以修改这个/etc/resolv.conf文件了
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)