smartdns自建DNS

SmartDNS

参考官方文档 https://pymumu.github.io/smartdns/download/

下载

Linux ECS非Debian系部署需要在GitHub下载静态编译包https://github.com/pymumu/smartdns/releases

Debian系已经加入APT源

1
apt install smartdns

配置

配置文件默认只有一个

1
/etc/smartdns/smartdns.conf

为了方便管理,可以拆分配置,常见的配置有

  • smartdns : 总配置
  • rule: 规则配置
  • group : 策略组配置
  • address: 解析配置

主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Smartdns.conf
user nobody
bind 0.0.0.0:53
# 关闭IPV6
force-AAAA-SOA yes
conf-file conf.d/*.conf
conf-file group.gconf -group zerotier

# 测速模式
speed-check-mode ping,tcp:80,tcp:443
response-mode fastest-ip

# 关闭双栈优选,因为没有 v6
dualstack-ip-selection no

# 审计日志
audit-enable yes
audit-size 5m
audit-file logs/
audit-num 5

# 最小TTL
rr-ttl-min 60
rr-ttl-max 3600

# 启用ACL访问控制,只允许特定网段的请求
acl-enable yes
client-rules 192.168.1.2/24
  • config-file 从一个位置导入配置文件,默认为 ./
    • 只有绝对位置和不写,自动补全默认值,如果你写 ./exam.conf 会被拼接为 /etc/smartdns/./exam.conf 从而报错
  • config-file -group 从配置文件导入 预配置好的规则组并生效
  • speed-check-mode 测速模式,返回解析后按照顺序进行测速
  • response-mode 返回模式,按照,这里按照最快访问IP筛选解析
  • bind:设置监听端口,
    • 还有很多花样玩法

      • 其他配置
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      # 指定interface
      bind-tcp 0.0.0.0:53@eth0
      bind-tcp [::]:53@eth0
      # 同时监听v6和v4的53端口绑定interface
      bind-tcp :53@eth0
      # TCP空闲断开连接时间
      tcp-idle-time 120
      # DOT 服务器
      bind-tls 0.0.0.0:853@eth0
      bind-tls [::]:853@eth0
      bind-tls :853@eth0

      # DOH 服务器
      bind-https 0.0.0.0:443@eth0
      bind-https [::]:443@eth0
      bind-https :443@eth0
      ## 证书路径和秘钥密码
      bind-cert-file smartdns-cert.pem
      bind-cert-key-file smartdns-key.pem
      bind-cert-key-pass pass

上游DNS配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# upstream.conf
# CF
server https://cloudflare-dns.com/dns-query
server https://1.0.0.1/dns-query
server https://1.1.1.1/dns-query
# Quad9
server https://dns11.quad9.net/dns-query
server https://dns.quad9.net/dns-query
# OpenDNS
server https://doh.opendns.com/dns-query
# Google
server https://dns.google/dns-query
# AD Guard
server https://dns.adguard.com/dns-query
# 腾讯国密
server https://sm2.doh.pub/dns-query
server https://120.53.53.53/dns-query
# 阿里系DOH
server https://dns.alidns.com/dns-query
server https://223.5.5.5/dns-query
server https://223.5.5.5/dns-query
# 腾讯系
server https://doh.pub/dns-query
server https://1.12.12.12/dns-query
# 日本 IIJ
server https://public.dns.iij.jp/dns-query
# UDP
server 119.29.29.29
server 223.5.5.5
server 223.6.6.6
server 1.1.1.1
server 9.9.9.9
server 8.8.8.8
server 8.8.4.4
# Local
server 192.168.1.1:11053 -group home -exclude-default-group
server 192.168.1.1:12053 -group office -exclude-default-group
  • 支持DNS/udp DoT DoH,优先使用DoH,按照顺序进行查询
  • -group zerotier :将上游DNS服务器进行分组,方便后续操作,不写将默认归类为 默认组
  • -exclude-default-group :从默认组中排除,也就是说,除非特别指定某个域名使用这个组的DNS进行查询,否则都不会使用

缓存配置文件

1
2
3
4
5
6
7
8
9
# Cache.conf
prefetch-domain yes
serve-expired yes
serve-expired-ttl 172800
serve-expired-reply-ttl 3
serve-expired-prefetch-time 3600
cache-size 1200
cache-checkpoint-time 86400
cache-persist yes
  • prefetch-domain :开取域名预取,会设置域名TTL-5,这样SmartDNS会提前5s去重新获取解析,会耗费更多的CPU
  • serve-expired-prefetch-time :域名超过指定时间未访问后主动进行刷新,需要开启域名预取才生效
  • serve-expired :乐观缓存,当域名过期后不删除,被请求时立即返回TTL= serve-expired-reply-ttl 指定值的过期缓存,避免等待,同时立即去刷新这个域名
  • serve-expired-ttl : 缓存超期释放时间,当缓存超过这个时间未访问时释放掉
  • cache-size:配置缓存域名记录数, 0 为根据内存自适应
  • cache-persist:缓存持久化
  • cache-checkpoint-time :检查点,每个间隔将自动保存一次

广告屏蔽

可以直接下载已经准备好的屏蔽文件 conf 进行引入

1
wget https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf

或者手动对单个域名进行屏蔽

1
2
3
4
# 对 *example.com 进行解析屏蔽
address /example.com/#
# 仅对一个子域名进行屏蔽
address /sub.examle.com/#

或者引入已经准备好的hosts文件

1
hosts-file <yourHostFile>

或者将标准的hosts格式转换为smart conf的host格式后引入

1
2
3
cat /path/to/hosts/file | grep -v "^#" | awk '{print "address /"$2"/#"}' > anti-ad-smartdns.conf
# 转换dmsmasq格式
cat /path/to/dnsmasq/file | grep address | awk -F= '{print "address "$2"#"}' > anti-ad-smartdns.conf

DNS分流

将特定的域名使用特定的DNS查询器查询来完成分流,有两种方法

1
2
3
# 将不同的域名后缀交给特定DNS服务器解析
.com -> 192.168.1.1
example.com -> 1.1.1.1

或者使用上述提到的服务器组的概念,将某个以特定域名结尾的域名交给这个服务器组进行解析,注意规则交叉

1
2
nameserver /example.cn/home
nameserver /com/default

或者按照端口分流

1
2
# 来自7753端口的DNS查询都使用Home服务组
bind :7753 -group home

特别的,我们希望按照来源IP分流,这需要用到 客户端 规则

可以查询 白名单分流 章节 https://pymumu.github.io/smartdns/config/forwarding-with-ipset/

这里将所有来自 192.168.1.1/24 的DNS请求、查询域名为 *com 的请求转交给 1.1.1.1 处理,还可以写别的规则,最后需要引入

1
2
3
4
group-begin GroupName
client-rules 192.168.1.1/24
nameserver /com/1.1.1.1
group-end

域名规则

对于域名可以设置一些规则,比较复杂

基本语法

1
2
3
4
5
6
7
8
domain-rule /sub.example.com/ [OptionalRule]
# 通配规则
## 全域名
domain-rule /*-a.example.com/
## 仅子域名
domain-rule /*.example.com/
## 仅主域名
domain-fule /-.example.com/
参数 功能
-group 设置对应的规则组
-address 指定域名地址
-nameserver 指定上游服务器组
-speed-check-mode 测速模式
-dualstack-ip-selection 双栈优选
-no-cache 停止缓存
-no-ip-alias 忽略ip集合规则
-ipset [ipsetname] 将对应请求的结果放入指定ipset
-nftset [nftsetname] 将对应的请求结果放入指定的nftset

根据域名组配置域名规则

1
2
3
domain-set -name ADdomain -file ad-list.conf
domain-rule /domain-set:ad/ -a
nameserver /domain-set:ad/adServer

后端DNS服务

常见的自建DNS排名TOP5 https://opensourcegeeks.net/5-top-self-hosted-opensource-dns-servers-like-pi-hole/

  • Pi-Hole
    • 非常小的麻雀,具备基本的域名劫持功能,但是管理起来比较麻烦
  • Technitium DNS
    • 非常好用,正在探索分布式部署
  • Adguard
    • 暂时没试过,感觉跟PiHole差不多,主要目的的拦截广告之类的,而不是提供DNS解析服务
  • Bind9
    • 非常专业的DNS解析服务,但是没有GUI WebUI,需要修改配置
  • CoreDNS
    • 还没试过,感觉也没有UI

smartdns自建DNS
https://blog.sukiloli.com/2024/12/01/smartdns自建DNS/
作者
QLozin
发布于
2024年12月1日
许可协议