dnsmasq常见用法

dnsmasq能够同时或单独的提供dns和dhcp服务,适合在轻量级的场景使用比如资源资源有限的路由器,防火墙.它能够缓存dns来提高网站的访问速度

安装

sudo apt-get install dnsmasq

配置简介

配置文件在 /etc/dnsmasq.conf

dnsmasq默认开启dns服务,如果不需要请将dns port设置为0 port=0

写好配置文件以后,使用dnsmasq --test可以用来检测配置文件是否编写正常

启动dnsmasq

目前大多数主机已经使用了systemd系统管理这里列出如何启动守护进程

systemctl start dnsmsaq.sevice  #启动
systemctl stop dnsmsaq.sevice  #关闭
systemctl restart dnsmsaq.sevice  #重新启动
systemctl status dnsmsaq.sevice  #查看状态
systemctl enable dnsmsaq.sevice  #开机启动
systemctl disable dnsmsaq.sevice  #关闭开机启动

启动完以后需要吧重启网络操作,会创造出来一个dhcp客户端需要的文件/etc/resolv.conf这个文件会覆盖系统原有的所以一般在配置文件里面另外指定一个会好一些.下面的配置文件会提到这点.

配置dns缓存功能

  • 给本机设置dns缓存,编辑/etc/dnsmasq.conf 注释掉listen-address 添加本地的ip地址即可.
    listen-address=127.0.0.1
  • 给本地主机提供dns缓存服务
    listen-address=192.168.1.1 #示例ip
  • 多个ip
    listen-address=127.0.0.1,192.168.1.1

dns地址文件

默认会使用/etc/resolv.conf为了避免冲突一般我会复制一份重命名成resolv.dnsmsasq.conf
更改文件内容,其实上面的就是可以启用第三方前置dns的意思

/etc/resolv.conf
nameserver 127.0.0.1
# External nameservers
...
/etc/resolv.dnsmasq.conf
# Google's nameservers, for example
nameserver 8.8.8.8
nameserver 8.8.4.4

/etc/resolv.dnsmasq.conf作为前置nameservers来使用,也就是说是dnsmasq的上游dns.具体体现在dnsmasq里面是resolv-file=/etc/resolv.dnsmasq.conf

NetworkManager

NetworkManager有一个启用dnsmasq的插件.如果dnsmasq安装了,但是不能生效请检查这项是否有启用.编辑/etc/NetworkManager/NetworkManager.conf 更改[main] 标签下的dns 添加dnsmasq

/etc/NetworkManager/NetworkManager.conf
[main]
...
dns=dnsmasq

重启 NetworkManager或重重启主机. NetworkManager将自动启动dnsmasq,并添加127.0.0.1到/etc/resolv.conf中.这时的dns服务服务在 /run/NetworkManager/resolv.conf. 你可以使用$ drill example.com 来验证是否生效以及解析时间是否变化.

自定义配置

自定义的配置文件可以写在/etc/NetworkManager/dnsmasq.d/里面.例如改变dnsmasq在RAm中的缓存数量

/etc/NetworkManager/dnsmasq.d/cache.conf
cache-size=1000        #数量
  • 测试方法
$ drill archlinux.org | grep "Query time"
;; Query time: 18 msec

$ drill archlinux.org | grep "Query time"
;; Query time: 2 msec

dhcp 服务

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to
# localhost and udp port 67 to world:
interface=<LAN-NIC>       

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with
# dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world
# requests to them, but the paranoid might like to close them and let the 
# kernel handle them:
bind-interfaces          #用的哪个网卡接口,尤其是双网卡的指定一下

# Optionally set a domain name
domain=example.com

# Set default gateway
dhcp-option=3,192.168.1.1                  #设置默认网关,可以不写

# Set DNS servers to announce
dhcp-option=6,8.8.8.8,8.8.4.4              #可以不写

# Dynamic range of IPs to make available to LAN PC and the lease time. 
# Ideally set the lease time to 5m only at first to test everything works okay before you set long-lasting records.
dhcp-range=192.168.111.50,192.168.111.100,12h       #地址范围以及租约时间           

# If you’d like to have dnsmasq assign static IPs to some clients, bind the LAN computers
# NIC MAC addresses:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50           #静态ip,地址与ip绑定
dhcp-host=aa:bb:cc:ff:dd:ee,192.168.111.51

TFTP 服务

这货还可以用作tftp服务器

为了增加安全性,建议使用dnsmasq的tftp安全模式。在安全模式下,只有dnsmasq用户拥有的文件将通过tftp服务。你将需要chown tftp root和其中的所有文件给dnsmasq用户使用这个功能。

/etc/dnsmasq.conf
enable-tftp
tftp-root=/srv/tftp         #服务器默认的根目录
tftp-secure                 #安全tftp

pxe服务器

pxe需要dhcp和tftp服务器,这两个功能都可以由dnsmasq提供。逆天不逆天???

/etc/dnsmasq.conf
interface=enp0s0
bind-dynamic
dhcp-range=192.168.0.1,proxy

重定向

重定向所有google的请求到对应的dns上

server=/www.google.com/<ISP DNS IP>

重定向地址

address=/example.com/1.2.3.4        #单条劫持
address=/#/1.2.3.4      #劫持所有网址

此外dnsmasq还支持两条dnsmasq同时存在于不同的网口上
静态使用interface 和 bind-interface选项

动态使用

except-interface=lo
bind-dynamic
上一篇
下一篇