如何在Ubuntu 18.04或20.04上设置自定义DNS服务器

之前修改DNS是非常简单的自从Ubuntu改用systemd作为启动程序后,就不知道往那改变DNS,默认情况你的DNS服务器配置是127.0.0.53即自动查找DNS。在本文中我们将说明怎么找到DNS配置文件,如linux必学的60个命令何修改DNS配置文件,怎么验证DNS是否配置正确。教程适用与Ublinuxuntu 18.04至Ubunlinuxtu 2linux系统0.04

怎么找到DNS配置文件

如果你尝试修改/etc/resolv.conf文件,你将会看到文件顶部如下提示

# Tlinux是什么操作系统his file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for conneclinux系统ting 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 differentlinux命令 way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemlinuxd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

提示不要编辑这个文件,这个文件是由man:systemd-reslinux系统olved(8) 管理,/etc/reslinux必学的60个命令olv.conf 是一个动态生成的文件,当你尝试看man的手册页,你会找到另一个配置, 并且注linux常用命令意到下面的语句

man systemd-resolvedlinux必学的60个命令
```
The DNS servers contacted are determined from the global settings in /etc/systemd/resolved.conf
```

DNS服务器是从全局设置/etc/systemd/resolved.conf读取。现在我们已经找设置DNS服务器的配置文件,我们现在尝试修改它

#  This file is palinuxrt of systemdlinux.
#
#  systemd is flinux必学的60个命令ree software;linux是什么操作系统 you can redistribute it and/or modlinux操作系统基础知识ifylinux it
#  under the terms of the Glinux系统安装NUlinux必学的60个命令 Lesser General Public License as published by
#  the Free Slinux命令oftware Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entrielinux是什么操作系统s in this file show the compile time defaults.
# You can change settilinux命令ngs by editing this file.
# Defaults can be restored by simply dlinux中文乱码视频eleting this file.
#
# See resolvedlinux系统.conf(5) for details
[Resolve]
DNS=8.8.8.8
FallbackDNS=223.5.5.5
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEClinux是什么操作系统=no
#DNSOverTLS=no
#Cache=no-negative
#DNSStubListener=yes
#ReadEtlinux重启命令cHosts=yes

在上面的配置文件中,我们修改了DNS=8.8.linux必学的60个命令8.8,FallbackDNS=223.5.5.5。这样修改并不会马上生linux效,因为这个linux系统配置文件是systemd-resolved服务的配置文件,linux系统涉及到服务的配置基本上都是linux系统安装需要重启服务才会生效,现在我们执linux是什么操作系统行以下命令重启它

sudo systemctl daemon-reload
sudo systemctl restart systemd-resolved.service

使用Dig命令验证我们的改变是否生效

dig wlinux中文乱码视频ww.myfreax.com
; <<>>linux系统安装 DiG 9.16.1-Ubuntu &linux命令lt;<>> www.myfreax.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28043
;; flags: qr rd ra; QUERY: 1, Alinux是什么操作系统NSWER: 1,linux是什么操作系统 AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:;linux是什么操作系统 udp: 512
;; QUESTION SECTION:
;www.myfreax.com.		IN	A
;; ANSWER SECTIOlinux操作系统基础知识N:
wwlinux重启命令w.myfreaxlinux中文乱码视频.com.	599	IN	A	45.32.83.60
;; Query time: 915 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: 一 3月 29 14:31:03 CST 2021
;; MSG SIZE  rcvd: 60

从上面ANSWER SECTION中可以看出,我们依然使用127.0.0.53linux命令 ,这意味着我们的修改并没有生效。我们继续查看man systemd-resolvlinux中文乱码视频ed 文档

/ETClinux操作系统基础知识/RESOLV.CONF
Flinux必学的60个命令our modes of handling /etc/resolv.conf (see resolv.conf(5)) are supported:
•   syslinux命令temd-resolved maintains the /run/systemd/resolvelinux系统/stub-resolv.conf file flinux系统安装or compatibility with traditional Linux plinux操作系统基础知识rogramslinux重启命令. This file may be symlinkedlinux系统安装 from /etc/resolinux命令lv.conf. This file lists the
127.0.0.53 DNS stub (see above) as the only DNS server. It also contains a list of search domains that are in use by systemd-resolved. Thelinux操作系统基础知识 list of search domains is always kept up-to-date. Note
that /run/systemd/relinux重启命令solve/stub-resolv.conf should not be used direlinuxctly by applications, but onlinux重启命令ly through a symlink from /etclinux重启命令/resolv.conf. This file may be symlinked from /etc/resolv.conf in order
to connect all local clients that bypass local DNS APIs to systemd-resolved with correct search domains settings. Thilinux系统安装s mode of operation is recommended.
•   A static file /usr/lib/systemd/resolv.conf is provided that lists the 127.0.0.53 DNS stub (see above) as only DNS server. This file may be symlinked from /etc/resolv.conf in order tlinuxo connect all
locallinux操作系统基础知识 clients that bypass local DNS APIs to systemd-resolved. This file does not contain any search domains.
•   systelinux是什么操作系统md-resolved maintains the /run/systemd/resolve/resolv.conf file for compatibility with traditional Linux programs. This file may be symlinked from /linux重启命令etc/resolv.conf and is always kept
ulinux操作系统基础知识p-to-date, containing information about all known DNS serverlinux中文乱码视频s. Note the file format'linux系统s limitatilinuxons: it does nlinux命令ot know alinux命令 concept of per-interface DNS servers and hence only contains system-wide DNS
serlinux系统安装ver definitions. Note that /run/systlinux系统emd/resolve/resolv.conf should not be used dilinux常用命令rlinux重启命令ectllinux命令y by applications, but only through a sylinux系统mlink from /etc/resolv.clinux命令onf. If this mode of operation is used
local clients thalinux必学的60个命令t bypass any llinuxocal DNS API wlinux系统安装ill also bypass systemd-resolved and will talk directly to the known DNS servers.
•   Alternatively, /etc/resolv.conf may be managedlinux命令 by other packages, in which case systemd-resolved will read it for DNS configuration data. In this mode of operation sylinux中文乱码视频stemd-resolved is consumer
rather tlinuxhan prolinuxvider of this configuration filinux命令le.
Note that the selected mode of operation forlinux重启命令 this file is detected fully automatically, dependilinux系统安装ng on whether /etc/resolv.conf is a symlink to /run/systemd/resolvelinux中文乱码视频/rlinux系统esolv.conf or lists 127.0.0.53 as
DNS server

Note that the seleclinux系统安装ted mode of operlinux系统ation for this file is detected fully automatically, depending on whether /etc/resolv.conf is a sylinux命令mlink to /run/systemd/resolve/resolv.conf or lists 127.0.0.53 as
DNS server.

上面文档提示我们,对systemd-resolvelinux是什么操作系统d配置更改生成linux系统安装的结果放在/run/systemd/resolve/resolv.conf 但是我们/etc/resolv.conflinux必学的60个命令是链接到/run/systemd/resolve/stub-resolv.conf

linux是什么操作系统下来我们只需要改变/etlinux中文乱码视频c/resolv.conf 的文件链接即可。通过ln命令

sudo mv /etc/resolv.conf /etc/resolv.conflinux是什么操作系统.baklinux必学的60个命令
sudo ln -s /run/systemd/resolve/resolv.linuxconf /etc/resolv.conf

接下linux来使用Dlinux必学的60个命令ig命令进行验证

dig wwlinux中文乱码视频w.myfreax.com
; <<>>linux必学的60个命令 DlinuxiG 9.16.1-Ubuntu <<>> www.myfreax.linuxcom
;; global options: +linux操作系统基础知识cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOlinux必学的60个命令ERRlinux系统安装OR, idlinux重启命令: 36578
;; flags: qr rlinux系统d ra; QUERY: 1, ANSWER: 1, Alinux常用命令UTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flaglinux命令s:; udp: 512
;; QUESTION SECTION:
;www.myfreax.com.		IN	A
;; ANSWER SECTION:
www.myfreax.com.	599	IN	A	45.32.83.60
;; Qulinux常用命令ery time: 895 mslinux中文乱码视频ec
;; SElinux系统RVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 一 3月 29 14:50:46 CST 2021
;; MSlinux操作系统基础知识G SIZE  rcvd: 60

怎么修改DNS配置文件

如果你已经看完上面的linux整个分析过程,那么基本也是完成了DNS的修改。这里我们在简linux操作系统基础知识单的总结一下

修改/etc/systemd/resolved.conf

[Resolve]
DNS=8.8.8.8
FallbackDNS=223.5.5.5
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=no-nlinux中文乱码视频egative
#DNSStubListener=yes
#ReadEtlinux重启命令cHosts=yes

我们添加DNS=8.8.8.8,FallbacklinuxDNS=22linux是什么操作系统3.5.linux5.5作为我们的DNS服务器

重启slinux重启命令ystemd-resolved服务

sudo systlinux重启命令emctl daemon-rellinux重启命令oad
sudo systemctl restart systemd-resolved.service

将/etc/resolv.conf链接到/run/systemd/resolve/resolv.conf

sudo mv /etlinuxc/resolv.conf /etc/resolv.conf.bak
sudo ln -s /run/syslinux命令temd/resolve/resolv.linux命令conf /etc/resolv.conf

使用Dig命令验证DNS的更改

dig www.myfreax.com

结论

我们分析了整个DNS配置查找过程,现在我们已经知道只需要更改/etc/slinux常用命令ystemd/resolved.conf/etc/resollinux操作系统基础知识v.conf 文件指向/run/systemd/resolve/resolv.conf即可改变DNSlinux系统安装