RouterOS配置DNS是网络管理中的重要环节,无论是搭建本地DNS服务器、实现域名解析加速,还是进行DNS过滤与安全防护,合理的配置都能显著提升网络效率与安全性,以下从基础配置、高级功能、安全优化及故障排查等方面详细说明RouterOS中DNS的配置方法。
基础DNS服务配置
RouterOS可通过dns
服务模块实现本地DNS缓存与转发功能,默认情况下设备已启用DNS服务,但需正确配置上游DNS服务器,进入WinBox或CLI界面,依次执行以下操作:
-
设置上游DNS服务器
在/ip dns
菜单下,通过servers
字段指定ISP提供的公共DNS或可信DNS服务,/ip dns set servers=8.8.8.8,1.1.1.1,223.5.5.5
支持多个服务器地址,RouterOS会按顺序轮询查询,提高可用性,若需特定DNS优先级,可使用
/ip dns server
下的allow-remote-requests
和cache-max-ttl
参数调整。 -
启用本地DNS缓存
默认DNS缓存已开启,可通过cache-size
调整缓存条目数量(默认为2048),/ip dns set cache-size=4096
合理的缓存大小可减少重复查询,提升解析速度。
cache-max-ttl
和cache-used-ttl
参数可控制缓存记录的最大生存时间和使用中的TTL,避免缓存过期导致的解析延迟。 -
配置静态DNS记录
对于内网服务器或固定IP设备,可添加静态DNS记录,避免依赖外部解析:/ip dns static add name=server1 address=192.168.1.10 /ip dns static add name=printer.local address=192.168.1.50
静态记录优先级高于缓存,适合内部服务快速访问。
高级DNS功能实现
DNS over HTTPS (DoH)与DNS over TLS (DoT)
RouterOS v7以上版本支持加密DNS协议,提升隐私安全性,配置DoH需先安装package/doh
包:
/system package install doh
然后在/ip dns doh
中添加服务器,
/ip dns doh add url=https://dns.google/dns-query
/ip dns set use-doh=yes
DoT配置类似,需指定TLS端口(如853)和服务器地址。
DNS智能转发与条件路由
通过/ip dns
的allow-remote-requests
和query-server
字段,可实现基于域名的智能转发,将内网域名解析请求转发至本地DNS,外部请求走公共DNS:
/ip dns set allow-remote-requests=yes
/ip dns server add address=192.168.1.1 allow-remote-requests=yes
/ip dns server add address=8.8.8.8 query=!lan
结合/ip firewall
的layer7-protocol
,可对特定流量进行DNS过滤。
负载均衡与故障转移
在/ip dns
中配置多个上游服务器时,RouterOS默认支持轮询负载均衡,若需主备模式,可通过脚本实现故障检测与切换:
/system script add name=dns-failover source="
/tool fetch url=http://8.8.8.8 -ping-only
:if (\$failure) do={
/ip dns set servers=1.1.1.1
} else={
/ip dns set servers=8.8.8.8,1.1.1.1
}
"
/system scheduler add name=dns-check interval=1m on-event=dns-failover
DNS安全与过滤优化
广告与恶意域名过滤
集成第三方黑名单(如StevenBlack的hosts文件),通过定时任务更新:
/system script add name=update-dns-blocklist source="
/tool fetch url=https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -dst-path=hosts.txt
/import file=hosts.txt
/file remove hosts.txt
"
/system scheduler add name=update-blocklist interval=7d on-event=update-dns-blocklist
导入后,在/ip dns
中启用allow-remote-requests
并配合防火墙规则阻止黑名单域名。
DNS响应速率限制
为防止DNS放大攻击,可配置/ip dns
的max-udp-packet-size
和query-interval
限制:
/ip dns set max-udp-packet-size=512 query-interval=0.1s
在/ip firewall
中添加规则限制DNS端口(53)的访问频率:
/ip firewall filter add chain=input protocol=udp dst-port=53 log-prefix="DNS-Flood" connection-limit=10,32
故障排查与监控
DNS查询日志与缓存查看
启用DNS查询日志:
/ip dns set log-queries=yes
/system logging add prefix=dns topics=dns
查看缓存记录:
/ip dns print cache
通过/tool snmp
监控DNS缓存命中率,或使用/tool graphing
生成性能图表。
常见问题解决
- 解析失败:检查
/ip dns
的servers
配置,使用/tool dns-print
测试域名解析。 - 缓存污染:定期清理缓存(
/ip dns cache flush
)或调整cache-max-ttl
。 - 内网解析异常:确认静态记录无冲突,检查
/ip dns
的allow-remote-requests
是否启用。
RouterOS DNS配置参数速查表
参数 | 作用 | 示例值 |
---|---|---|
servers |
上游DNS服务器地址 | 8.8.8,1.1.1.1 |
cache-size |
DNS缓存条目数 | 4096 |
cache-max-ttl |
缓存最大生存时间 | 1h |
allow-remote-requests |
允许远程DNS查询 | yes |
use-doh |
启用DNS over HTTPS | yes |
max-udp-packet-size |
UDP数据包最大大小 | 512 |
FAQs
Q1: 如何在RouterOS中实现特定域名的强制解析?
A: 可通过/ip dns static
添加静态记录,或使用/ip dns
的server
字段配置条件转发,将example.com
解析指向内网服务器:
/ip dns static add name=example.com address=192.168.1.100
Q2: RouterOS DNS服务突然无法解析域名,如何快速排查?
A: 按以下步骤排查:
- 检查
/ip dns
的servers
字段是否配置正确; - 使用
/tool dns-print
测试域名解析(如/tool dns-print example.com
); - 确认防火墙未阻止DNS端口(53);
- 重启DNS服务(
/ip dns set servers=
重新配置)或清除缓存(/ip dns cache flush
)。