在Ubuntu 20.04上使用Dnsmasq配置本地DNS服务器
欢迎来到我们的教程,了解如何在Ubuntu 20.04上使用Dnsmasq安装和配置本地DNS服务器。 “ Dnsmasq是一种轻型,易于配置的DNS转发器和DHCP服务器。 它旨在为小型网络提供DNS以及可选的DHCP和TFTP。 它可以服务不在全局DNS中的本地计算机的名称。
如果您想节省自己必须以BIND9方式配置DNS服务器的麻烦,则使用Dnsmasq是启动和运行本地DNS服务器的最简单,最快的方法。
因此,在此设置中,我们将Dnsmasq配置为本地缓存DNS服务器,以加快本地DNS解析速度。
在Ubuntu 20.04上使用Dnsmasq配置本地DNS服务器
运行系统更新
更新系统软件包缓存;
apt update
在Ubuntu 20.04上安装Dnsmasq
Dnsmasq在Ubuntu 20.04 Universe仓库中可用。但是,在安装之前 dnsmasq
在Ubuntu 20.04上,禁用Systemd解析的服务(为本地应用程序提供网络名称解析的系统服务)。
systemctl disable --now systemd-resolved
删除默认的resolv.conf文件,并使用您的自定义DNS服务器详细信息创建一个新文件,以使您能够进行安装。
rm -rf /etc/resolv.conf
echo "nameserver 8.8.8.8" > /etc/resolv.conf
完成后,可以通过运行以下命令来安装Dnsmasq。
apt install dnsmasq
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dns-root-data dnsmasq-base libidn11
Suggested packages:
resolvconf
The following NEW packages will be installed:
dns-root-data dnsmasq dnsmasq-base libidn11
0 upgraded, 4 newly installed, 0 to remove and 73 not upgraded.
Need to get 382 kB of archives.
After this operation, 1,155 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
这将安装并启动并使Dnsmasq服务在系统引导时运行。
systemctl status dnsmasq
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-10-06 19:12:31 UTC; 15s ago
Main PID: 17726 (dnsmasq)
Tasks: 1 (limit: 2282)
Memory: 868.0K
CGroup: /system.slice/dnsmasq.service
└─17726 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new --local-service --trust-anchor=.,20326,8,2,e>
Oct 06 19:12:31 ubuntu20 systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Oct 06 19:12:31 ubuntu20 dnsmasq[17705]: dnsmasq: syntax check OK.
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: started, version 2.80 cachesize 150
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: DNS service limited to local subnets
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify dumpfi>
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: reading /etc/resolv.conf
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: using nameserver 8.8.8.8#53
Oct 06 19:12:31 ubuntu20 dnsmasq[17726]: read /etc/hosts - 7 addresses
Oct 06 19:12:31 ubuntu20 systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
在Ubuntu 20.04上使用Dnsmasq配置本地DNS服务器
一旦安装了Dnsmasq,您现在就可以将其配置为Ubuntu 20.04上的本地缓存DNS服务器。 /etc/dnsmasq.conf
是默认的Dnsmasq配置文件。因此,要配置dnsmasq,您需要编辑 /etc/dnsmasq.conf
文件。
首先,创建配置文件的副本;
cp /etc/dnsmasq.conf{,.bak}
接下来,打开配置文件进行编辑;
vim /etc/dnsmasq.conf
该文件的注释很好,所有配置选项均能自我解释。
首先,设置Dnsmasq将侦听DNS请求的端口。默认情况下默认为端口53 UDP。您也可以使用 port
选项。
port=53
禁止转发不带点或域部分的名称;
domain-needed
禁止转发非路由地址空间中的地址;
bogus-priv
定义一个接口(例如, interface=enp0s8
)或IP地址(例如, listen-address=192.168.x.x
),Dnsmasq可以在其上侦听DNS请求。这通常默认为回送地址。在此设置中,我们将Dnsmasq设置为通过回送和非回送接口IP响应内部和外部DNS请求。
listen-address=127.0.0.1,192.168.57.3
相应地替换您的接口IP地址。
启用Dnsmasq将域部分自动附加到简单名称;
expand-hosts
设置dnsmasq的域以附加到简单名称;
domain=kifarunix-demo.com
调整缓存域名的大小。默认是 150
。
cache-size=1000
对于使用Dnsmasq的基本本地缓存DNS服务器,以上配置选项已足够。
没有注释行,这就是我们的Dnsmasq配置文件的样子;
port=53
domain-needed
bogus-priv
listen-address=127.0.0.1,192.168.57.3
expand-hosts
domain=kifarunix-demo.com
cache-size=1000
完成更改后,保存并退出配置文件。
在/ etc / hosts文件中添加Dnsmasq DNS服务器IP
接下来,在服务器上将Dnsmasq IP地址添加为主DNS服务器。 /etc/resolv.conf
。
sed -i '1i nameserver 192.168.57.3' /etc/resolv.conf
的 /etc/resolv.conf
现在看起来像;
cat /etc/resolv.conf
nameserver 192.168.57.3
nameserver 8.8.8.8
将本地DNS记录添加到Dnsmasq服务器
在Dnsmasq服务器上添加本地DNS条目 /etc/hosts
文件。
回声-e“ 192.168.57.19 centos8.kifarunix-demo.com n192.168.57.6 ubuntu18.kifarunix-demo.com” >> / etc / hosts
对其他本地域名也执行相同的操作。
重新启动Dnsmasq
运行Dnsmasq配置检查;
dnsmasq --test
dnsmasq: syntax check OK.
重新启动Dnsmasq;
systemctl restart dnsmasq
确认端口53;
netstat -alnp | grep -i :53
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 18313/dnsmasq
tcp6 0 0 :::53 :::* LISTEN 18313/dnsmasq
udp 0 0 0.0.0.0:53 0.0.0.0:* 18313/dnsmasq
udp6 0 0 :::53 :::* 18313/dnsmasq
请注意,即使dnsmasq仅在某些接口上侦听,它也会绑定到通配符地址。这样做的优点是即使接口来回更改地址也可以工作。
在UFW上打开DNS端口
如果启用了UFW,请打开DNS端口53,UDP。
ufw allow from 192.168.0.0/16 to any port 53 proto udp
相应地更新您的源网络。
验证DNS解析
本地域解析;
dig ubuntu18.kifarunix-demo.com +short
192.168.57.3
外部DNS解析;
dig google.com +short
216.58.223.110
在远程客户端上配置DNS服务器
现在,Dnsmasq已准备好通过本地DNS服务器解决DNS本地查询和外部查询的问题,现在可以在/etc/resolv.conf上为客户端更新DNS条目。
echo "nameserver 192.168.57.3" > /etc/resolv.conf
相应地替换Dnsmasq IP。
执行本地dns查询;
dig ubuntu18.kifarunix-demo.com
; <<>> DiG 9.11.13-RedHat-9.11.13-6.el8_2.1 <<>> ubuntu18.kifarunix-demo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57550
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;ubuntu18.kifarunix-demo.com. IN A
;; ANSWER SECTION:
ubuntu18.kifarunix-demo.com. 0 IN A 192.168.57.3
;; Query time: 1 msec
;; SERVER: 192.168.57.3#53(192.168.57.3)
;; WHEN: Tue Oct 06 00:20:02 EAT 2020
;; MSG SIZE rcvd: 72
现在,让我们使用 钻头 效用。要使用此工具,您需要安装 ldns-utils
在CentOS上打包或 ldnsutils
在Ubuntu上打包。假设已安装软件包,请使用 drill
验证DNS缓存的实用程序;
第一次查询运行;
drill google.com | grep "Query time"
;; Query time: 25 msec
第二次查询;
drill google.com | grep "Query time"
;; Query time: 1 msec
呵呵呵!! Dnsmasq现在已配置并正在运行本地缓存DNS服务器。这标志着本教程有关如何在Ubuntu 20.04上使用Dnsmasq安装和配置本地DNS服务器的结尾。
。