1、dnsmasq软件介绍

dnsmasq 是一款轻量级的网络服务软件,主要用于提供 DNS(域名系统)和 DHCP(动态主机配置协议)服务

作用

  1. 提高网络效率:

    • 通过 DNS 缓存,减少网络延迟。
    • 减少对外部 DNS 请求的数量。
  2. 自动 IP 配置:

    • 自动为新连接的设备分配 IP 地址,无需手动设置,降低配置复杂性。
  3. 增强网络安全:

    • 通过内置的过滤功能,可以限制不必要的 DNS 请求,提高网络安全性。
  4. 简化网络管理:

    • 集成 DNS 和 DHCP 服务,使网络管理更加方便。

2、安装

yum -y install dnsmasq

[master-61 root ~] # yum install -y dnsmasq 
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                            | 3.6 kB  00:00:00     
epel                                                                            | 4.3 kB  00:00:00     
extras                                                                          | 2.9 kB  00:00:00     
updates                                                                         | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package dnsmasq.x86_64 0:2.76-17.el7_9.3 will be installed
--> Processing Dependency: libnettle.so.4()(64bit) for package: dnsmasq-2.76-17.el7_9.3.x86_64
--> Running transaction check
---> Package nettle.x86_64 0:2.7.1-9.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================
 Package               Arch                 Version                        Repository             Size
=======================================================================================================
Installing:
 dnsmasq               x86_64               2.76-17.el7_9.3                updates               281 k
  stalling for dependencies:
 nettle                x86_64               2.7.1-9.el7_9                  updates               328 k

Transaction Summary
=======================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 608 k
Installed size: 1.3 M
Downloading packages:
(1/2): nettle-2.7.1-9.el7_9.x86_64.rpm                                          | 328 kB  00:00:00     
(2/2): dnsmasq-2.76-17.el7_9.3.x86_64.rpm                                       | 281 kB  00:00:00     
-------------------------------------------------------------------------------------------------------
Total                                                                  1.6 MB/s | 608 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : nettle-2.7.1-9.el7_9.x86_64                                                         1/2 
  Installing : dnsmasq-2.76-17.el7_9.3.x86_64                                                      2/2 
  Verifying  : dnsmasq-2.76-17.el7_9.3.x86_64                                                      1/2 
  Verifying  : nettle-2.7.1-9.el7_9.x86_64                                                         2/2 

Installed:
  dnsmasq.x86_64 0:2.76-17.el7_9.3                                                                     

Dependency Installed:
  nettle.x86_64 0:2.7.1-9.el7_9                                                                        

Complete!

3、创建dnsmasq配置文件,设置上游dns服务器地址,设置本地域名解析记录

[master-61 root ~] # vim /etc/dnsmasq.conf 

取消注释,指定上游dns服务器地址
resolv-file=/etc/resolv.dnsmasq.conf


指定运行地址是内网环境
listen-address=172.16.1.61

本地域名解析记录
addn-hosts=/etc/hosts.dnsmasq.conf


查找出配置文件中没有注释的内容
[master-61 root ~] # grep -Ev '^($|#)'  /etc/dnsmasq.conf 
resolv-file=/etc/resolv.dnsmasq.conf
listen-address=172.16.1.61
addn-hosts=/etc/hosts.dnsmasq.conf
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig

4、创建对应的数据文件

[master-61 root ~] # echo 'nameserver 233.5.5.5' > /etc/resolv.dnsmasq.conf

[master-61 root ~] # cat >/etc/hosts.dnsmasq.conf <<EOF
> 172.16.1.61 master-61
> 172.16.1.5 slb-5
> 172.16.1.6 slb-6
> 172.16.1.7 web-7
> 172.16.1.8 web-8
> 172.16.1.9 web-9
> 172.16.1.31 nfs-31
> 172.16.1.41 rsync-41
> 172.16.1.51 db-51
> EOF

5、 启动dnsmasq服务,生效加上的自定义记录

[master-61 root ~] # systemctl start dnsmasq

6、测试dnsmasq是否可以使用

先注释掉本地hosts解析记录
[master-61 root ~] # vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

#内网ip       外网ip     主机名     机器的作用
#172.16.1.61  10.0.0.61  master-61  管理机
#172.16.1.5   10.0.0.5   slb-5      负载均衡服务器  
#172.16.1.6   10.0.0.6   slb-6      负载均衡服务器  
#172.16.1.7   10.0.0.7   web-7      web服务器       
#172.16.1.8   10.0.0.8   web-8      web服务器       
#172.16.1.9   10.0.0.9   web-9      web服务器       
#172.16.1.31  10.0.0.31  nfs-31     存储服务器      
#172.16.1.41  10.0.0.41  rsync-41   备份服务器     
#172.16.1.51  10.0.0.51  db-51      数据库服务器

发现ping不通了
[master-61 root ~] # ping web-7
ping: web-7: Name or service not known


需要指定本地的你dns配置文件的记录,使用dnsmasq的服务端地址
[master-61 root ~] # vim /etc/resolv.conf 
# Generated by NetworkManager
nameserver 172.16.1.61

更多推荐