Lazy loaded image
外部节点配置 smartdns 提供内网 DNS 分流
Words 927Read Time 3 min
2025-10-2
2025-11-10
date
related_level
slug
type
relate_date
summary
status
tags
category
last_updated
Nov 10, 2025 10:02 PM
是否已更新
orginal_page
是否推荐
ubuntu 安装 smartdns
  • apt install smartdns
    • 一般不是最新版本,最新版本需要到 github 下载安装包
  • mv /etc/smartdns/smartdns.conf /etc/smartdns/smartdns.conf.bak
vi /etc/smartdns/smartdns.conf
  • (可选) 均仅监听本地地址,通过 nginx stream 模块提供 ACL 和反代
    • 上游以 腾讯公共 DNS 为主,阿里公共 DNS 为 fallback
    • 在 internal.conf 中加入内网 DNS 服务器
      • 所有上游服务器组都会加入默认组,为了避免泄露需要把内网上游排除
        • -exclude-default-group -g localns
    • 对于需要代理的外网地址可以使用 proxy 服务的 dns 端口进行查询并通过 tproxy 透明代理转发
      • 如何配置 tproxy 透明代理查看别的文章
    vi /etc/smartdns/internal.conf
    • 如果用 ddns 会暴露了内网 DNS 服务器地址, 所以需要添加对应地址的解析
      • 一般都可以拿到公网 ipv6 地址
    vi /etc/smartdns/internal.ipv6.conf
    • 在内网 DNS 服务器通过脚本监控 ipv6 公网地址变化, 在发生变化之后通过脚本更新该文件并通知 smartdns 重启即可
    服务器如何返回当前租约时间最长的 ipv6 公网地址
    • 写入到特定文件并定期比对即可
    • 发生变化时通过 ssh 重启远程 smartdns 服务
    (可选) vi /etc/smartdns/acl.conf
    • 可选, 可以通过 nginx stream 模块提供 acl
    (可选) vi /etc/smartdns/domain-proxy.conf
    • 可选, github 地址域名查询转发到 proxy dns 端口, 加速访问
    vi /etc/default/smartdns
    停用 systemd-resolved 改用 smartdns
    • systemctl disable systemd-resolved --now
    • systemctl enable smartdns --now
    测试 DNS 分流效果
    • Windows 上需要先安装 bind 再使用 dig
      • scoop install main/bind
    • 查看端口绑定情况
      • lsof -i :53,5335
    • 域名测试(假设监听 5353 端口)
      • dig @localhost -p 5353 baidu.com
      • dig @localhost -p 5353 AAAA github.com
      • dig @localhost -p 5353 <internal-server.home>
        • 测试内网地址是否能正常解析
    (可选) nginx 监听 5353 端口提供 udp 反代和 ACL
    • 假设 smartdns 监听本地 53 端口,nginx 通过 5353 端口提供 udp 反代和 ACL
    核心配置文件
    /etc/nginx/conf.stream.d/upstream_localhost.stream.conf
    /etc/nginx/stream-available/dns.conf
    /etc/nginx/nginx.conf
    acl 及日志格式
    /etc/nginx/acl.d/allow_ipv6_china
    /etc/nginx/acl.d/restrict_locally
    /etc/nginx/conf.sites.d/stream-logs-format.conf
    • ln -s /etc/nginx/stream-available/dns.conf /etc/nginx/stream-enabled/dns.conf
    • service nginx reload
    上一篇
    Docker Swarm 网络调试及问题修复
    下一篇
    step-ca 配置及使用

    Comments
    Loading...