Config File
/etc/dnsmasq.conf
/etc/dnsmasq.d/ # need to be included in main conf
部分配置项
cache-size=0 # 禁用dns缓存
#port=53 # default
listen-address=127.0.0.1
listen-address=192.168.1.1
# 忽略上游服务器提供的 private IP 解析记录(将返回 NXDOMAIN)。OpenWrt 的 dnsmasq 默认开启该选项
stop-dns-rebind
# dns rebind 的白名单域名
rebind-domain-ok=/example.com/
listen-address 可以配置多个;如果不配置,则监听所有 interface 里指定的端口的所有IP。如果配置了,则只监听这些IP。
address=/ad.com/0.0.0.0
server=/cn/114.114.114.114
# catch all domains
address=/#/192.168.2.1
Use with VPN
/etc/resolv.conf
nameserver 127.0.0.1
/etc/dnsmasq.conf
no-resolv
server=8.8.8.8
conf-dir=/etc/dnsmasq.d/,*.conf
#conf-file=/root/files/appdata/dnsmasq/dnsmasq.conf
只允许本机自身的DNS解析(仅用于代替 /etc/resolv.conf)
listen-address=127.0.0.1
no-resolv
server=8.8.8.8
conf-dir=/etc/dnsmasq.d/,*.conf
或使用一个独立的 resolv-file
resolv-file=/etc/resolv-native.conf
conf-dir=/etc/dnsmasq.d/,*.conf
iptables
部署在路由器上时,可以劫持所有(局域网机器)发送的 DNS 解析请求到本机。这样所有设备无论配置的是什么 DNS,都可以保证由本路由器解析。某些版本的路由器固件 Web UI 里自带开关可以启用下面这条 iptables 语句。
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-port 53
Route
通过 ip route 或 ip rule 使 8.8.8.8 通过 VPN 路由。
# 直接添加 route 到 main 路由表
ip route add 8.8.8.8 dev tun0
# 或配合 ip rule 策略路由 (推荐这种,更灵活)
ip route add default dev tun0
ip rule add to 8.8.8.8 table 10 prio 10
dnsmasq-china-list (optional)
推荐配合 dnsmasq-china-list 使中国国内域名解析支持国内的 CDN。
cd /root/
git clone https://github.com/felixonmars/dnsmasq-china-list
# vi /etc/dnsmasq.conf
# add the below line
conf-dir=/root/dnsmasq-china-list,*.conf
辅助脚本
/usr/bin/dnsmasq-china-list-update
(需要安装 wget, unzip 这2个包)
#!/bin/sh
# add the following line to /etc/dnsmasq.conf:
# conf-dir=/etc/dnsmasq.d,*.conf
mkdir /tmp/dnsmasq-chinalist
cd /tmp/dnsmasq-chinalist
wget https://github.com/felixonmars/dnsmasq-china-list/archive/master.zip -O master.zip
unzip -o master.zip
if [ $? -ne 0 ]
then
exit
fi
mkdir /etc/dnsmasq.d
cp dnsmasq-china-list-master/*.conf /etc/dnsmasq.d
rm -rf /tmp/dnsmasq-chinalist
echo success
Block AD
推荐使用一些基于 DNS 的在线 Ad 屏蔽源:
- hosts-blocklists (推荐)
- anti-AD (中文特化)
这些源通常提供 hosts, dnsmasq conf 或其他 DNS 工具格式配置文件的订阅。
dnsmasq里指定使用额外 hosts 文件(可以指定目录,读取其中所有文件作为hosts):
addn-hosts=/root/hosts
指定额外配置文件:
conf-file=/path/to/file.conf
# 或指定目录下所有配置文件(请注意旧版本的 Dnsmasq 不支持 ",*.conf" 限定只读取目录下指定后缀文件作为配置文件的语法)
conf-dir=/etc/dnsmasq.d,*.conf
辅助脚本
(需要安装 wget)
/usr/bin/update-hosts-blocklists
#!/bin/sh
#add to /etc/dnsmasq.conf:
# conf-dir=/etc/dnsmasq.d,*.conf
# addn-hosts=/etc/dnsmasq.d/hostnames.txt
mkdir /tmp/hosts-blocklists
cd /tmp/hosts-blocklists
wget https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt
wget https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt
cp hostnames.txt /etc/dnsmasq.d
cp domains.txt /etc/dnsmasq.d/domains-block.conf
cd /tmp
rm -rf /tmp/hosts-blocklists
注意这个广告屏蔽源的 hostnames.txt 和 domains.txt 2个文件加起来大小8MB多(OpenWrt 的 overlay 的 squalsh 压缩文件系统实际占用空间约 2MB)。请保证路由器上flash有足够空余空间。
请注意,加载配置文件里 server/address/ipset 规则数量很多时可能会影响 Dnsmasq 性能。可以考虑使用 smartdns 替代 Dnsmasq。