How to use dnsmasq to deploy DNS / DHCP server on CentOS / RHEL 8/7

Dynamic Host Configuration Protocol (DHCP) server to dynamically assign IP addresses and other network configuration parameters for each device on the network. DNS forwarder will be on non-LAN local domain name DNS queries are forwarded to an upstream DNS server (the external network). Recursive DNS cache server can respond to requests from the client so that you can resolve DNS queries more quickly, thereby improving the site previously visited DNS lookup speed.

It has a DNS subsystem that provides local DNS server for the network, and forwards all types of queries to upstream DNS recursive server and caching common record types. DHCP subsystem supports DHCPv4, DHCPv6, BOOTP, PXE and TFTP server. Router advertisement subsystem supports basic IPv6 host auto-configuration.

Dnsmasq installed in CentOS and RHEL Linux in

1. dnsmasq package repository available in the default, and may use a package manager YUM easily installed, as shown in FIG.

# yum install dnsmasq

2. dnsmasq packages after the installation is complete, you need to start the dnsmasq service immediately, and allow it to start automatically when the system boots. In addition, check its status using the following systemctl order to ensure that it is up and running.

# systemctl start dnsmasq
# systemctl enable dnsmasq
# systemctl status dnsmasq

Configuring dnsmasq server in CentOS and RHEL Linux

3. can /etc/dnsmasq.conf file (containing comments and explain the options) to configure dnsmasq server, you can also add user-defined configuration files to /etc/dnsmasq.d directory.

By default, DNS is enabled, so before making any changes, be sure to create a backup /etc/dnsmasq.conf file.

# cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

4.现在,使用您喜欢的基于文本的编辑器打开/etc/dnsmasq.conf文件,并进行以下建议的配置设置。

# vi /etc/dnsmasq.conf

listen-address选项用于设置dnsmasq监听的IP地址。 要使用您的CentOS/RHEL服务器在LAN上侦听DHCP和DNS请求,请如图所示将listen-address选项设置为其LAN IP地址(请记住包括127.0.0.1)。 请注意,服务器IP必须是静态的。

listen-address=::1,127.0.0.1,192.168.56.10

与上述相关,您可以使用interface选项限制dnsmasq侦听的接口(为多个接口添加更多行)。

interface=eth0

5.如果要将域(可以如下所示进行设置)自动添加到hosts文件中的简单名称,请取消注释expand-hosts选项。

expand-hosts

6.要为dnsmasq设置域,这意味着只要设置的域匹配,DHCP客户端将具有完全限定的域名,并为所有客户端设置“域” DHCP选项。

domain=tecmint.lan

7.接下来,还如图所示,使用服务器选项(格式为server=dns_server_ip)为非本地域定义上游DNS服务器。

# Google's nameservers
server=8.8.8.8
server=8.8.4.4

8.然后,您可以使用显示的地址选项将本地域强制为IP地址。

address=/tecmint.lan/127.0.0.1
address=/tecmint.lan/192.168.56.10

9.保存文件,并检查配置文件语法中的错误,如图所示。

# dnsmasq --test

使用/etc/resolv.conf文件配置dnsmasq

10.在此步骤中,您需要通过将localhost地址添加为/etc/resolv.conf文件中的唯一名称服务器,来使所有查询都发送到dnsmasq。

# vi /etc/resolv.conf

11. /etc/resolv.conf文件由本地守护程序(尤其是NetworkManager)维护,因此任何用户进行的更改都将被覆盖。 为了防止这种情况,请使用chattr命令通过设置不可变文件属性(禁用对文件的写访问)来对其进行写保护。

# chattr +i /etc/resolv.conf
# lsattr /etc/resolv.conf

定义DNS主机和名称

12. Dnsmasq从/ etc / hosts文件中读取所有DNS主机和名称,因此,如图所示添加DNS主机的IP地址和名称对。

127.0.0.1      dnsmasq
192.168.56.10  dnsmasq
192.168.56.1   gateway
192.168.56.100 maas-controller
192.168.56.20  nagios
192.168.56.25  webserver1

重要说明:本地DNS名称也可以通过从DHCP子系统导入名称来定义,也可以通过配置各种有用的记录类型来定义。

13.要应用以上更改,请如图所示重新启动dnsmasq服务。

# systemctl restart dnsmasq

14.如果正在运行Firewalld服务,则需要在防火墙配置中打开DNS和DHCP服务,以允许来自LAN上主机的请求传递到dnsmasq服务器。

# firewall-cmd --add-service=dns --permanent
# firewall-cmd --add-service=dhcp --permanent
# firewall-cmd --reload

测试本地DNS

15.要测试本地DNS服务器或转发是否工作正常,您需要使用dig或nslookup之类的工具来执行DNS查询。 这些工具由bind-utils软件包提供,该软件包可能未预先安装在CentOS / RHEL 8上,但您可以如图所示进行安装。

# yum install bind-utils

16.安装后,您可以在本地域上运行一个简单查询,如图所示。

# dig tecmint.lan
或者
# nslookup tecmint.lan

17.您也可以尝试查询其中一台服务器的FQDN。

# dig webserver1.tecmint.lan
或者
# nslookup webserver1.tecmint.lan

18.要测试反向IP查找,请运行类似的命令。

# dig -x 192.168.56.25
或者
# nslookup 192.168.56.25

使用dnsmasq启用DHCP服务器

19.您可以通过取消注释dhcp-range选项来启用DHCP服务器,并提供可用于租用的地址范围以及可选的租用时间,例如(对于多个网络重复)。

dhcp-range=192.168.0.50,192.168.0.150,12h

20.以下选项定义了DHCP服务器将其租约数据库保留在何处,这将帮助您轻松检查其已分配的IP地址。

dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases

21.要将DHCP服务器设置为权威模式,请取消注释该选项。

dhcp-authoritative

22.保存文件,然后重新启动dnsmasq服务以应用最近的更改。

# systemctl restart dnsmasq

如果您想与我们分享有关本指南的任何问题或想法,请使用以下的评论栏与我们联系。

Guess you like

Origin www.linuxidc.com/Linux/2019-11/161273.htm