使用OpenWRT搭建内网DNS服务器

简介

好多人都存在DNS泄露问题,口述方式讲解如何防止DNS泄露不是很直观,所以写了这篇教程分享作者是如何搭建内网DNS服务器
下面是本教程所搭建内网DNS服务器的查询流程

[终端设备] 
   │ 发送 DNS 请求
   ▼
[AdguardHome]
   │ 匹配查询规则
   ├─ 特定设备
   │     │
   │     ▼
   │  [SmartDns:6053] (返回优选 IP)
   │     │ 解析国内域名
   │     ▼
   │  [国内 DNS 服务器]
   │
   ├─ 特定域名
   │     ├─ 重定向域名
   │     │      │
   │     │      ▼
   │     │     返回 IP
   │     │
   │     └─ 拦截域名
   │            │
   │            ▼
   │           拦截
   │
   └─ 默认
         │
         ▼
      [MosDNS]
         │ 查询分流(根据 geosite 规则)
         ├─ 国内
         │     │
         │     ▼
         │  [SmartDns:6053] (返回优选 IP)
         │     │ 解析国内域名
         │     ▼
         │  [国内 DNS 服务器]
         │
         └─ 国外
               │ 使用代理软件 DNS 查询
               ▼
            [品质路线代理软件]
               │ 转发到 SmartDns
               ▼
            [SmartDns:7053]
               │ 使用品质路线代理查询 DNS
               ▼
            [国外 DNS 服务器]

先前准备

  1. 请确保你的OpenWRT具有 SmartDNS、MosDNS、AdguardHome
  2. 请确保有 SmartDNS、MosDNS、AdguardHome 能够 正常运行
  3. 推荐所有设备使用 内网V6地址
  4. 请将 Dnsmasq 的 DNS服务器监听端口 设置为 非53端口 或者 52
  5. 请关闭 Dnsmasq 的 DNS 重定向 ,如果需要DNS重定向,请使用 防火墙规则设置

注意事项

本教程所使用 OpenWRT版本 是 ImmortalWRT 24.10.2

插件版本 是 mosdns 5.3.3-r5 、luci-app-mosdns 1.5.23-r1 、 smartdns 46.1-r1 、luci-app-smartdns 1.2024.45-r1 、 adguardhome 0.107.63-r1 、 luci-app-adguardhome 25.207.66380~64e0058

你完全可以只使用SmartDNS全包所有功能, 本教程只是使用了最简单的方法实现复杂需求


设置

SmartDNS

这个主要作用是 查询和优选 , 并不做 过滤和重定向

推荐 开启DNS缓存

推荐使用 运营商的DNS , 但不能仅 使用运营商DNS

国内DNS服务器 只使用 无加密查询 就行 , 如果有 DNS查询重定向的需求 的话 , 加密查询也无妨

国外DNS服务器 查询推荐 仅使用加密查询(DOH) , 并设置 使用代理查询 (防止DNS泄露) 和 勾选 从default默认服务器组中排除

更多配置请查阅 SmartDNS 官方文档

这里直接贴出 SmartDNS配置 , 配置路径是 /etc/config/smartdns

  • 国内DNS服务器组 是 CN
  • 国外DNS服务器组 是 GF
config smartdns
        option server_name 'smartdns'
        option auto_set_dnsmasq '0'
        option tcp_server '1'
        option ipv6_server '1'
        option bind_device '0'
        option force_https_soa '1'
        option rr_ttl_min '600'
        option seconddns_tcp_server '1'
        option seconddns_port '7053'
        option seconddns_server_group 'GF'
        option resolve_local_hostnames '0'
        option seconddns_no_speed_check '1'
        option seconddns_no_rule_nameserver '1'
        option seconddns_no_rule_ipset '1'
        option seconddns_no_dualstack_selection '1'
        option seconddns_no_cache '1'
        option enabled '1'
        option port '6053'
        option prefetch_domain '1'
        option seconddns_enabled '1'
        option proxy_server '127.0.0.1:7893'
        option seconddns_no_rule_addr '1'
        option seconddns_no_rule_soa '1'
        option dualstack_ip_selection '0'
        option serve_expired '1'
        option cache_size '1000'
        option cache_persist '0'
        option response_mode 'fastest-ip'
        option speed_check_mode 'tcp:443,tcp:80,ping'
        option tls_server '0'
        option doh_server '0'
        option seconddns_force_https_soa '0'
        option log_output_mode 'file'
        option auto_update_week_time '*'
        option auto_update_day_time '0'
        option old_port '6053'
        option old_enabled '1'
        option old_auto_set_dnsmasq '0'

config domain-rule

config client-rule
        option enabled '0'
        option dualstack_ip_selection '1'
        option force_https_soa '1'

config ip-rule
        option enabled '1'

# 字节DNS
config server
        option type 'udp'
        option name '字节'
        option ip '180.184.1.1'
        option server_group 'CN'
        option enabled '1'

# 阿里DNS
config server
        option type 'udp'
        option name '阿里'
        option ip '223.5.5.5'
        option server_group 'CN'
        option enabled '1'

config server
        option name '阿里'
        option ip '223.5.5.5'
        option type 'tls'
        option server_group 'CN'
        option tls_host_verify 'dns.alidns.com'
        option host_name 'dns.alidns.com'
        option enabled '0'

config server
        option name '阿里'
        option ip '223.5.5.5/dns-query'
        option type 'https'
        option server_group 'CN'
        option tls_host_verify 'dns.alidns.com'
        option host_name 'dns.alidns.com'
        option http_host 'dns.alidns.com'
        option enabled '1'

# 腾讯DNS
config server
        option type 'udp'
        option name '腾讯'
        option ip '119.29.29.29'
        option server_group 'CN'
        option enabled '1'

config server
        option name '腾讯'
        option ip '1.12.12.12'
        option type 'tls'
        option server_group 'CN'
        option host_name 'dot.pub'
        option enabled '0'

config server
        option name '腾讯'
        option ip '120.53.53.53/dns-query'
        option type 'https'
        option server_group 'CN'
        option host_name 'doh.pub'
        option http_host 'doh.pub'
        option enabled '1'

# 360 DNS
config server
        option type 'udp'
        option name '360'
        option ip '101.198.198.198'
        option server_group 'CN'
        option enabled '1'

config server
        option name '360'
        option ip '101.198.192.33'
        option type 'tls'
        option server_group 'CN'
        option tls_host_verify 'dot.360.cn'
        option host_name 'dot.360.cn'
        option enabled '0'

config server
        option name '360'
        option ip '218.30.118.6/dns-query'
        option type 'https'
        option server_group 'CN'
        option tls_host_verify 'doh.360.cn'
        option enabled '1'

# 谷歌DNS
config server
        option type 'udp'
        option name '谷歌'
        option ip '8.8.4.4'
        option server_group 'GF'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

config server
        option name '谷歌'
        option ip '8.8.8.8'
        option type 'tls'
        option server_group 'GF'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

config server
        option name '谷歌'
        option ip '8.8.8.8/dns-query'
        option type 'https'
        option server_group 'GF'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

# Adguard DNS
config server
        option enabled '1'
        option name 'AdGuard DNS'
        option ip '94.140.14.141'
        option type 'udp'
        option server_group 'GF'
        option exclude_default_group '1'
        option use_proxy '1'

config server
        option enabled '1'
        option name 'AdGuard DNS'
        option ip '94.140.14.140/dns-query'
        option type 'https'
        option server_group 'GF'
        option exclude_default_group '1'
        option tls_host_verify 'unfiltered.adguard-dns.com'
        option host_name 'unfiltered.adguard-dns.com'
        option http_host 'unfiltered.adguard-dns.com'
        option use_proxy '1'

config server
        option enabled '1'
        option name 'AdGuard DNS'
        option ip '94.140.14.141'
        option type 'tls'
        option server_group 'GF'
        option exclude_default_group '1'
        option tls_host_verify 'unfiltered.adguard-dns.com'
        option host_name 'unfiltered.adguard-dns.com'
        option use_proxy '1'

# CloudFlare DNS
config server
        option type 'udp'
        option name 'Cloudflare'
        option ip '1.1.1.1'
        option server_group 'GF'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

config server
        option name 'Cloudflare'
        option ip '1.0.0.1'
        option type 'tls'
        option server_group 'GF'
        option host_name '1dot1dot1dot1.cloudflare-dns.com'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

config server
        option name 'Cloudflare'
        option ip '1.1.1.1/dns-query'
        option type 'https'
        option host_name 'dns.cloudflare.com'
        option http_host 'dns.cloudflare.com'
        option use_proxy '1'
        option exclude_default_group '1'
        option enabled '1'

MosDNS

MosDNS主要作用是 分流 , 主要根据 GeoIP & GeoSite 数据库 分流 , 所以 开启 更新广告规则、GeoIP & GeoSite 数据库 是可以的 , 但如果更新失败可能会导致 MosDNS无法启动

如果需要更加精细化管理 , 可以使用 规则列表

  • 白名单 是使用 国内 DNS 服务器 查询
  • 灰名单 是使用 国外 DNS 服务器 查询
  • 以 domain: 开头,域匹配
    e.g: domain:google.com 会匹配自身 google.com,以及其子域名 www.google.com, maps.l.google.com 等
  • 以 full: 开头,完整匹配
    e.g: full:google.com 只会匹配自身
  • 以 keyword: 开头,关键字匹配
    e.g: keyword:google.com 会匹配包含这个字段的域名,如 google.com.hk, www.google.com.hk
  • 以 regexp: 开头,正则匹配(Golang 标准)
    e.g: regexp:.+.google.com$

推荐 关闭DNS缓存

更多配置请查阅 MosDNS wiki

这里直接贴出 MosDNS配置 , 配置路径是 /etc/config/mosdns

  • 国内 DNS 服务器 是 127.0.0.1:6053
  • 远程 DNS 服务器 是 127.0.0.1:7874 (品质路线代理软件的DNS服务器)
config mosdns 'config'
        option enabled '1'
        option listen_port '5335'
        option geo_auto_update '1'
        option geo_update_week_time '*'
        option geo_update_day_time '2'
        option redirect '0'
        option prefer_ipv4 '1'
        option adblock '0'
        option configfile '/var/etc/mosdns.json'
        option log_level 'info'
        option log_file '/var/log/mosdns.log'
        option cache '0'
        option concurrent '2'
        option idle_timeout '30'
        option minimal_ttl '0'
        option maximum_ttl '0'
        option custom_local_dns '1'
        option enable_pipeline '1'
        option insecure_skip_verify '0'
        option dns_leak '1'
        option cloudflare '0'
        option listen_port_api '9091'
        option custom_stream_media_dns '0'
        option bootstrap_dns '119.29.29.29'
        option apple_optimization '0'
        list local_dns '127.0.0.1:6053'
        list remote_dns '127.0.0.1:7874'
        option enable_ecs_remote '0'
        option github_proxy 'https://gh-proxy.com'

AdguardHome

AdguardHome作用主要是 匹配客户端和域名做精细化调整 并 记录设备查询日志 , 个人推荐 关闭DNS缓存 、 查询日志保留 和 查询统计数据保留 设置为24小时 (防止爆存储)

不推荐 使用 广告过滤功能 , 开启后会导致各种各样的访问问题

推荐 所有设备使用内网V6地址 配合 NATv6 或者是 NPTv6 , 以便匹配客户端 (目前AdguardHome无法匹配IPV6后缀)

推荐 AdguardHome直接监听53端口

更多配置请查阅 Adguard DNS文档

这里直接贴出 AdGuardHome配置 , 配置路径是 /etc/AdGuardHome.yaml

  • 账号是 admin , 密码是 admin
  • 如果需要修改密码 , 请使用 htpasswd -B -C 10 -n -b <USERNAME> <PASSWORD> , 并粘贴到配置文件中 users: 下的 password 后面
http:
  address: 0.0.0.0:3000
  session_ttl: 720h
users:
  - name: admin
    password: $2y$10$B6jFQ2UyJNw3bZ1ZKc4o9eraXWUYmDZPKYPk/Pwqay/DJ9/hNqda2
auth_attempts: 5
block_auth_min: 15
http_proxy: ""
language: zh-cn
theme: auto
dns:
  bind_hosts:
    - 0.0.0.0
  port: 53
  anonymize_client_ip: false
  ratelimit: 0
  ratelimit_subnet_len_ipv4: 24
  ratelimit_subnet_len_ipv6: 56
  ratelimit_whitelist: []
  refuse_any: false
  upstream_dns:
    - 127.0.0.1:5335
  upstream_dns_file: ""
  bootstrap_dns:
    - 127.0.0.1:6053
  fallback_dns: []
  upstream_mode: load_balance
  fastest_timeout: 1s
  allowed_clients: []
  disallowed_clients: []
  blocked_hosts:
    - version.bind
    - id.server
    - hostname.bind
  trusted_proxies:
    - 127.0.0.0/8
    - ::1/128
  cache_size: 0
  cache_ttl_min: 0
  cache_ttl_max: 0
  cache_optimistic: true
  bogus_nxdomain: []
  aaaa_disabled: false
  enable_dnssec: false
  edns_client_subnet:
    custom_ip: ""
    enabled: false
    use_custom: false
  max_goroutines: 300
  handle_ddr: true
  ipset: []
  ipset_file: ""
  bootstrap_prefer_ipv6: false
  upstream_timeout: 10s
  private_networks: []
  use_private_ptr_resolvers: false
  local_ptr_upstreams: []
  use_dns64: false
  dns64_prefixes: []
  serve_http3: false
  use_http3_upstreams: false
  serve_plain_dns: true
  hostsfile_enabled: true
  pending_requests:
    enabled: true
tls:
  enabled: false
  server_name: ""
  force_https: false
  port_https: 443
  port_dns_over_tls: 853
  port_dns_over_quic: 784
  port_dnscrypt: 0
  dnscrypt_config_file: ""
  allow_unencrypted_doh: false
  certificate_chain: ""
  private_key: ""
  certificate_path: ""
  private_key_path: ""
  strict_sni_check: false
querylog:
  dir_path: ""
  ignored: []
  interval: 6h
  size_memory: 1000
  enabled: true
  file_enabled: true
statistics:
  dir_path: ""
  ignored: []
  interval: 720h
  enabled: true
filters: []
whitelist_filters: []
user_rules: []
dhcp:
  enabled: false
  interface_name: ""
  local_domain_name: lan
  dhcpv4:
    gateway_ip: ""
    subnet_mask: ""
    range_start: ""
    range_end: ""
    lease_duration: 86400
    icmp_timeout_msec: 1000
    options: []
  dhcpv6:
    range_start: ""
    lease_duration: 86400
    ra_slaac_only: false
    ra_allow_slaac: false
filtering:
  blocking_ipv4: ""
  blocking_ipv6: ""
  blocked_services:
    schedule:
      time_zone: Local
    ids: []
  protection_disabled_until: null
  safe_search:
    enabled: false
    bing: true
    duckduckgo: true
    ecosia: true
    google: true
    pixabay: true
    yandex: true
    youtube: true
  blocking_mode: default
  parental_block_host: family-block.dns.adguard.com
  safebrowsing_block_host: standard-block.dns.adguard.com
  rewrites: []
  safe_fs_patterns:
    - /usr/bin/data/userfilters/*
  safebrowsing_cache_size: 1048576
  safesearch_cache_size: 1048576
  parental_cache_size: 1048576
  cache_time: 30
  filters_update_interval: 24
  blocked_response_ttl: 60
  filtering_enabled: true
  parental_enabled: false
  safebrowsing_enabled: false
  protection_enabled: true
clients:
  runtime_sources:
    whois: true
    arp: true
    rdns: true
    dhcp: true
    hosts: true
  persistent: []
log:
  enabled: true
  file: ""
  max_backups: 0
  max_size: 100
  max_age: 3
  compress: false
  local_time: false
  verbose: false
os:
  group: ""
  user: ""
  rlimit_nofile: 0
schema_version: 29

其他注意事项

  1. 可以尝试DNS重定向 劫持53查询 , 但不要陷入 先有鸡还是先有蛋 的问题中
  2. 内网DNS服务器可以 搭建两个 (使用vrrp ip做热备) 或者 一个DNS服务器 设置两个IP , 这样可以在 DHCP服务器 中 设置两个DNS服务器 防止 终端设备使用公共DNS服务器作为备用DNS服务器
  3. 品质线路代理软件 推荐使用 品质线路查询DNS , 可以 有效解决DNS泄露问题
  4. 可以使用 IPLEAK 、 dnsleaktest 、 browserleaks , 测试结果中 只能是 非大陆查询结果 。请勿掩耳盗铃让 dns泄露检查网站走品质线路 ,这样测试是无效的
posted @ 2025-08-18 22:39  MAENE  阅读(3288)  评论(0)    收藏  举报