如何在Ubuntu 20.04上使用Dnsmasq设置本地DNS解析器
如何在Ubuntu 20.04上使用Dnsmasq设置本地DNS解析器
Dnsmasq代表“ DNS伪装”的缩写,是一种用于小型网络的简单,轻便且易于使用的DNS转发器。 可以将其配置为DNS缓存和DHCP服务器,并支持IPv4和IPv6协议。 当它收到任何DNS查询时,它将从其缓存中答复它们,或转发到其他DNS服务器。
Dnsmasq由三个子系统组成:
- DNS子系统:用于缓存不同的记录类型,包括A,AAAA,CNAME和PTR。
- DHCP子系统:支持DHCPv4,DHCPv6,BOOTP和PXE
- 路由器广告子系统:它为IPv6主机提供基本的自动配置。 它可以独立使用,也可以与DHCPv6结合使用。
在本教程中,我们将向您展示如何在Ubuntu 20.04服务器上使用Dnsmasq设置本地DNS服务器。
先决条件
- 运行Ubuntu 20.04的服务器。
- 为服务器配置了root密码。
入门
首先,建议将系统软件包更新为最新版本。 您可以通过运行以下命令来更新所有软件包:
apt-get update -y
更新所有软件包后,您将需要在系统中禁用Systemd解析的服务。 Systemd解析的服务用于本地应用程序的网络名称解析。
您可以通过运行以下命令禁用它:
systemctl disable --now systemd-resolved
禁用该服务后,您将需要删除默认的resolv.conf文件,并使用您的自定义DNS服务器详细信息创建一个新文件。
您可以使用以下命令删除默认的resolv.conf文件:
rm -rf /etc/resolv.conf
接下来,使用以下命令将Google DNS服务器添加到resolv.conf文件:
echo "nameserver 8.8.8.8" > /etc/resolv.conf
完成后,您可以继续下一步。
安装Dnsmasq
默认情况下,Ubuntu 20.04默认存储库中提供Dnsmasq。 您可以通过运行以下命令来安装它:
apt-get install dnsmasq dnsutils ldnsutils -y
安装完成后,Dnsmasq服务将自动启动。 您可以使用以下命令检查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-11-02 11:02:01 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> Nov 02 11:02:12 ubuntu2004 systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server... Nov 02 11:02:12 ubuntu2004 dnsmasq[17705]: dnsmasq: syntax check OK. Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: started, version 2.80 cachesize 150 Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: DNS service limited to local subnets Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify dumpfi> Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: reading /etc/resolv.conf Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: using nameserver 8.8.8.8#53 Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: read /etc/hosts - 7 addresses Nov 02 11:02:12 ubuntu2004 systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
完成后,您可以继续下一步。
配置Dnsmasq
接下来,您将需要将Dnsmasq配置为本地DNS服务器。 您可以通过编辑Dnsmasq主配置文件来做到这一点:
nano /etc/dnsmasq.conf
更改以下行:
port=53 domain-needed bogus-priv listen-address=127.0.0.1,your-server-ip expand-hosts domain=dns-example.com cache-size=1000
完成后,保存并关闭文件。
接下来,您需要在resolv.conf文件中将服务器的IP地址添加为主域名服务器。 您可以使用以下命令添加它:
nano /etc/resolv.conf
在“名称服务器8.8.8.8”行上方添加以下行:
nameserver your-server-ip
完成后,保存并关闭文件。 接下来,使用以下命令验证服务器是否存在任何配置错误:Advertisement.banner-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf} .banner-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf}
dnsmasq --test
如果一切正常,您应该获得以下输出:
dnsmasq: syntax check OK.
最后,重新启动Dnsmasq服务以应用更改:
systemctl restart dnsmasq
此时,Dnsmasq已启动并在端口53上侦听。您可以使用以下命令进行验证:
ss -alnp | grep -i :53
您应该获得以下输出:
udp UNCONN 0 0 0.0.0.0:53 0.0.0.0:* users:(("dnsmasq",pid=41051,fd=4)) udp UNCONN 0 0 [::]:53 [::]:* users:(("dnsmasq",pid=41051,fd=6)) tcp LISTEN 0 32 0.0.0.0:53 0.0.0.0:* users:(("dnsmasq",pid=41051,fd=5)) tcp LISTEN 0 32 [::]:53 [::]:* users:(("dnsmasq",pid=41051,fd=7))
将DNS记录添加到Dnsmasq服务器
接下来,您将需要编辑/ etc / hosts文件并添加本地DNS服务器条目。Advertisement.large-leaderboard-2{text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf}
nano /etc/hosts
添加以下行:
your-server-ip host1.dns-example.com
完成后,保存并关闭文件。
验证Dnsmasq服务器分辨率
此时,将安装并配置Dnsmasq。 否,该验证DNS解析了。
您可以使用dig命令检查DNS解析,如下所示:
dig host1.dns-example.com +short
如果一切正常,您应该在以下输出中看到服务器ip:
your-server-ip
您还可以使用以下命令来验证外部DNS解析:
dig howtoforge.com +short
您应该获得以下输出:
172.67.68.93 104.26.3.165 104.26.2.165
配置远程客户端以使用Dnsmasq DNS服务器
接下来,您将需要配置一个远程客户端,以将您的Dnsmasq DNS服务器用作默认DNS服务器。
首先,使用以下命令安装DNS工具:
apt-get install dnsutils ldnsutils -y
安装后,您将需要编辑/etc/resolv.conf文件和Dnsmasq DNS服务器条目。Advertisement.leader-1{text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf} .leader-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf}
nano /etc/resolv.conf
在文件开头添加以下行:
nameserver your-server-ip
完成后,保存并关闭文件。
接下来,使用以下命令验证本地DNS解析:
dig host1.dns-example.com
您应该看到以下输出:
; DiG 9.9.5-3ubuntu0.4-Ubuntu host1.dns-example.com ;; global options: +cmd ;; Got answer: ;; HEADER opcode: QUERY, status: NOERROR, id: 26401 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;host1.dns-example.com. IN A ;; ANSWER SECTION: host1.dns-example.com. 0 IN A 45.58.32.165 ;; Query time: 301 msec ;; SERVER: 45.58.32.165#53(45.58.32.165) ;; WHEN: Mon Nov 02 16:49:37 IST 2020 ;; MSG SIZE rcvd: 66
接下来,您将需要验证DNS服务器是否可以缓存。 您可以使用钻取实用程序进行检查。
首先,运行以下命令:
drill google.com | grep "Query time"
您应该看到以下输出:
;; Query time: 290 msec
接下来,再次运行命令以检查缓存是否正常工作:
drill google.com | grep "Query time"
您应该看到查询时间现在减少到4毫秒:
;; Query time: 4 msec
结论
恭喜你 您已成功安装Dnsmasq并将其配置为本地DNS服务器和Ubuntu 20.04。 我希望您现在可以轻松地在本地网络中实现它以进行名称解析。