如何在Ubuntu 18.04 LTS上安装和配置DNSMASQ

时间:2020-02-23 14:44:47  来源:igfitidea点击:

本教程将在Ubuntu 18.04 LTS上安装和配置DNSMAQ DNS服务器()。
对于DNSMASQ的那些,DNSMASQ是一种简单,轻量级,易于使用和管理DNS服务器,支持LUA脚本,IPv6,DNSSEC,用于PXE,BOOTP和TFTP的网络引导。
因此,它具有很小的足迹,因此适用于资源受限的路由器和防火墙。

DNSMASQ旨在为小于中型网络环境提供DNS,以及可选的DHCP/TFTP服务。
当它收到DNS查询时,它将从其本地缓存中应答,或者将它们转发到其他递归DNS服务器,该服务器可以是绑定或者任何其他DNS服务器。

DNSMASQ子系统

DNSMASQ有三个主要子系统,即:DNS子系统:提供A,AAAA,CNAME和PTR的缓存,DNSKEY和DS RECORDS.DHCP子系统:为DHCPv4,DHCPv6,BOTP和PXE提供支持。
我们可以使用静态和动态DHCP租赁,内置于只读TFTP服务器,以支持NetBoot.RouterAD子系统:为IPv6主机提供基本的自动配置

第1步:在Ubuntu 18.04上安装DNSMASQ

Ubuntu 18.04附带系统D-Demaryve,因为它需要禁用,因为它与端口53绑定,它将与DNSMASQ端口冲突。

运行以下命令以禁用已解析的服务:

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

另外,删除符号链接 resolv.conf文件

$ls -lh /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Aug  8 15:52 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
$sudo rm /etc/resolv.conf

然后创建新的rollev.conf文件。

echo "nameserver 8.8.8.8" > /etc/resolv.conf

DNSMASQ可在APT存储库上提供,可以通过运行来完成简单的安装:

sudo apt-get install dnsmasq

DNSMASQ的主要配置文件是 /etc/dnsmasq.conf
通过修改此文件配置DNSMASQ。

sudo vim /etc/dnsmasq.conf

这是最小的配置

# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.
port=53
# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# By  default,  dnsmasq  will  send queries to any of the upstream
# servers it knows about and tries to favour servers to are  known
# to  be  up.  Uncommenting this forces dnsmasq to try each query
# with  each  server  strictly  in  the  order  they   appear   in
# /etc/resolv.conf
strict-order
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts
# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
#domain=thekelleys.org.uk
domain=mypridomain.com
# Set Liste address
listen-address=127.0.0.1 # Set to Server IP for network responses

如果要启用DNSSEC验证和缓存,取消注释

#dnssec

完成任何其他更改,我们可以在完成后看到相关并重新启动DNSMASQ:

sudo systemctl restart dnsmasq

第2步:将DNS记录添加到DNSMASQ

在文件中添加DNS记录。 /etc/hosts
dnsmasq将使用这些记录回复客户端的查询。

$sudo vim /etc/hosts
10.1.3.4 server1.mypridomain.com
10.1.4.4 erp.mypridomain.com 
192.168.10.2 checkout.mypridomain.com 
192.168.4.3 hello.world

添加记录后需要重新启动DNSMASQ服务。

sudo systemctl restart dnsmasq

第3步:测试DNSMAQ DNS功能

要验证DNSMASQ响应所添加的记录,请将服务器的DNS服务器指向DNSMASQ服务器。
编辑 /etc/network/interfaces用于持久配置或者文件 /etc/netplan/在Ubuntu 18.04服务器上。

由于这是一个测试,我将修改运行时文件 /etc/resolv.conf

$sudo vim /etc/resolv.conf
nameserver 127.0.0.1
nameserver 8.8.8.8

使用DIG测试:

$dig A erp.mypridomain.com
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> A erp.mypridomain.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43392
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;erp.mypridomain.com.		IN	A
;; ANSWER SECTION:
erp.mypridomain.com.	0	IN	A	10.1.4.4
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Aug 21 10:35:41 UTC 2016
;; MSG SIZE  rcvd: 64

这是另一个例子:

$dig checkout.mypridomain.com A +noall +answer
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> checkout.mypridomain.com A +noall +answer
;; global options: +cmd
checkout.mypridomain.com. 0 IN A 192.168.10.2

我们可以确认我们正在配置的响应。

将DNSMASQ配置为DHCP服务器(可选)

我们可以使用DNSMASQ将IP地址分配给客户端,静态或者动态。

编辑文件a /etc/dnsmasq.conf并提供DHCP选项。
我们需要提供:默认网关IP addressdns服务器IP地址(可能dnsmasq或者不同的DNS服务器)网络子网MaskDhcp地址RangentP服务器

见下文示例

dhcp-range=192.168.3.25,192.168.3.50,24h
dhcp-option=option:router,192.168.3.1
dhcp-option=option:ntp-server,192.168.3.5
dhcp-option=option:dns-server,192.168.3.5
dhcp-option=option:netmask,255.255.255.0

重新启动DNSMASQ并配置客户端以获取此服务器的IP地址。

sudo systemctl restart dnsmasq