如何在 Ubuntu 20.04 上使用 Dnsmasq 设置本地 DNS 解析器如何在 Ubuntu 20.04 上使用 Dnsmasq 设置本地 DNS 解析器如何在 Ubuntu 20.04 上使用 Dnsmasq 设置本地 DNS 解析器如何在 Ubuntu 20.04 上使用 Dnsmasq 设置本地 DNS 解析器
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何在 Ubuntu 20.04 上使用 Dnsmasq 设置本地 DNS 解析器

发表 admin at 2025年2月28日
类别
  • 未分类
标签

在此页

  1. 先决条件
  2. 开始
  3. 安装 Dnsmasq
  4. 配置 Dnsmasq
  5. 将 DNS 记录添加到 Dnsmasq 服务器
  6. 验证 Dnsmasq 服务器解析
  7. 配置远程客户端使用 Dnsmasq DNS 服务器
  8. 结论

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 的服务器。
  • 为服务器配置了根密码。

入门

首先,建议将您的系统包更新到最新版本。您可以通过运行以下命令来更新所有包:

apt-get update -y

更新所有软件包后,您需要在系统中禁用 Systemd 解析的服务。 Systemd-resolved 服务用于本地应用程序的网络名称解析。

您可以通过运行以下命令来禁用它:

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

默认情况下,Dnsmasq 在 Ubuntu 20.04 默认存储库中可用。您只需运行以下命令即可安装它:

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 ubunt4 systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Nov 02 11:02:12 ubunt4 dnsmasq[17705]: dnsmasq: syntax check OK.
Nov 02 11:02:12 ubunt4 dnsmasq[17726]: started, version 2.80 cachesize 150
Nov 02 11:02:12 ubunt4 dnsmasq[17726]: DNS service limited to local subnets
Nov 02 11:02:12 ubunt4 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 ubunt4 dnsmasq[17726]: reading /etc/resolv.conf
Nov 02 11:02:12 ubunt4 dnsmasq[17726]: using nameserver 8.8.8.8#53
Nov 02 11:02:12 ubunt4 dnsmasq[17726]: read /etc/hosts - 7 addresses
Nov 02 11:02:12 ubunt4 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

完成后保存并关闭文件。

接下来,您需要将您的服务器 IP 地址添加为您的 resolv.conf 文件中的主要名称服务器。您可以使用以下命令添加它:

nano /etc/resolv.conf

在 \nameserver 8.8.8.8\ 行上方添加以下行:

nameserver your-server-ip

完成后保存并关闭文件。接下来,使用以下命令验证服务器是否存在任何配置错误:

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 服务器条目。

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 linux教程 +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 服务器条目。

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 实用程序检查它。

首先,运行以下命令:

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。我希望您现在可以轻松地在您的本地网络中实施它以进行名称解析。

©2015-2025 Norria support@norria.com