欢迎光临

VPS搭建私有DNS与广告过滤系统:AdGuard Home + SmartDNS 完整部署指南

在使用VPS的过程中,DNS解析质量直接影响着网络体验。公共DNS(如8.8.8.8、114.114.114.114)虽然稳定,但无法做到广告过滤、智能分流,且存在隐私泄露风险。本文将详细介绍如何在一台VPS上部署AdGuard Home和SmartDNS,构建一个既具备广告过滤能力、又拥有智能DNS解析优化的私有DNS系统。

一、为什么选择AdGuard Home + SmartDNS组合

AdGuard Home和SmartDNS各有侧重,两者组合可以形成互补的DNS服务架构。AdGuard Home擅长广告过滤和DNS级别的内容拦截,而SmartDNS擅长DNS智能解析和加速。

1.1 AdGuard Home的核心优势

AdGuard Home是一款开源的DNS服务器,兼具广告过滤和隐私保护功能。与浏览器插件不同,它工作在DNS层面,可以为整个网络中的所有设备提供广告拦截,包括手机、智能电视、IoT设备等无法安装广告插件的终端。

  • DNS级广告过滤:通过维护庞大的过滤规则库,直接在DNS层面屏蔽广告域名,无需在设备端安装任何软件
  • 支持DoH/DoT/DoQ:提供DNS-over-HTTPS、DNS-over-TLS、DNS-over-QUIC加密查询,防止DNS劫持和窃听
  • Web管理界面:自带可视化后台,可实时查看查询日志、被拦截的域名统计
  • 上游DNS配置灵活:支持负载均衡、并行查询、DNSCrypt协议
  • 支持DHCP服务:可作为轻量级DHCP服务器使用

1.2 SmartDNS的核心优势

SmartDNS由国内开发者Nick Yang开发,专注于DNS智能解析。它的核心能力是从多个上游DNS服务器获取解析结果,然后根据延迟、路由质量选择最快的IP返回给客户端。

  • 多DNS上游测速:同时向多个DNS服务器查询,自动测速返回最快IP
  • 分组解析规则:可根据域名分组,将特定域名路由到特定DNS上游
  • IP地址过滤:支持过滤特定IP段的解析结果(如屏蔽CDN节点,获取直连IP)
  • 支持IPv4/IPv6双栈:自动适配双栈环境

两者的协作模式是:SmartDNS作为前置DNS,负责智能解析和测速;AdGuard Home作为下游DNS,负责广告过滤和客户端管理。客户端查询AdGuard Home,AdGuard Home将非广告域名转发给SmartDNS,SmartDNS测速后返回最优IP。

二、VPS环境准备与基础配置

2.1 VPS选型建议

DNS服务对带宽和CPU的要求不高,但对网络质量和稳定性要求较高。建议选择以下配置:

项目 最低要求 推荐配置
CPU 1核 1-2核
内存 512MB 1GB
硬盘 5GB 10GB(含日志)
带宽 不限流量 1Gbps端口
系统 Debian 11+ Debian 12 / Ubuntu 22.04

重要提示:DNS服务使用UDP 53端口,部分云厂商(如AWS、阿里云)默认会封禁53端口,需要提工单申请解封或使用非标准端口。

2.2 系统初始化

首先更新系统并安装必要依赖:


1
2
3
4
5
6
7
8
9
10
11
12
13
# 更新系统包
apt update && apt upgrade -y

# 安装基础工具
apt install -y curl wget tar jq dnsutils ufw

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 确保systemd-resolved不占用53端口
systemctl disable systemd-resolved --now 2>/dev/null
rm -f /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

如果VPS上运行了其他服务(如Docker容器),需检查53端口是否被占用:


1
ss -tulnp | grep ':53 '

若有进程占用,需先停止或迁移该服务。常见占用源包括systemd-resolved、dnsmasq、Pi-hole等。

2.3 防火墙配置

开放DNS所需的端口:


1
2
3
4
5
6
7
ufw allow 53/tcp comment 'DNS'
ufw allow 53/udp comment 'DNS'
ufw allow 80/tcp comment 'AdGuard HTTP'
ufw allow 443/tcp comment 'AdGuard HTTPS'
ufw allow 853/tcp comment 'DNS-over-TLS'
ufw allow 784/udp comment 'DNS-over-QUIC'
ufw enable

三、部署SmartDNS:智能解析层

3.1 下载并安装SmartDNS

从GitHub Release页面获取最新版本:


1
2
3
4
5
6
7
8
9
# 获取最新版本号
SMARTDNS_VERSION=$(curl -s https://api.github.com/repos/pymumu/smartdns/releases/latest | jq -r '.tag_name' | sed 's/v//')

# 下载deb包(Debian/Ubuntu)
wget https://github.com/pymumu/smartdns/releases/download/v${SMARTDNS_VERSION}/smartdns.${SMARTDNS_VERSION}.x86_64-linux-all.deb -O /tmp/smartdns.deb

# 安装
dpkg -i /tmp/smartdns.deb
systemctl enable smartdns

3.2 SmartDNS配置详解

SmartDNS的主配置文件位于

1
/etc/smartdns/smartdns.conf

。下面是一份经过验证的生产配置:


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# /etc/smartdns/smartdns.conf

# 监听端口(仅本机,供AdGuard Home转发)
bind [::]:6053

# 启用IPv4
force-AAAA-SOA no

# 测速模式:ping和tcp ping组合
speed-check-mode ping,tcp:443

# 测速间隔(毫秒)
speed-check-mode-timeout 500

# 缓存大小
cache-size 32768

# 预获取常用域名
cache-persist yes
cache-file /etc/smartdns/smartdns.cache

# 日志级别
log-level error
log-file /var/log/smartdns/smartdns.log
log-size 128K
log-num 2

# === 国内DNS上游 ===
server-https https://doh.pub/dns-query -group cn -exclude-default-group
server-https https://dns.alidns.com/dns-query -group cn -exclude-default-group

# === 国际DNS上游 ===
server-https https://dns.google/dns-query -group overseas -exclude-default-group
server-https https://cloudflare-dns.com/dns-query -group overseas -exclude-default-group

# === 智能分流规则 ===
# 国内域名使用国内DNS
domain-set -name cn-list -file /etc/smartdns/cn-domain.list
domain-rules /domain-set:cn-list/ -n -g cn

# Google/Cloudflare系域名使用海外DNS
domain-rules /google.com/ -g overseas
domain-rules /youtube.com/ -g overseas
domain-rules /github.com/ -g overseas
domain-rules /githubusercontent.com/ -g overseas
domain-rules /cloudflare.com/ -g overseas

# 双栈偏好:优先IPv4
dualstack-ip-selection yes
dualstack-ip-selection-threshold 100
dualstack-ip-allow-force-AAAA no

3.3 生成国内域名列表

使用felixonmars维护的dnsmasq-china-list项目生成国内域名集:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mkdir -p /etc/smartdns
cd /etc/smartdns

# 下载加速域名列表
curl -sL https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf -o /tmp/cn.conf

# 转换为SmartDNS格式(domain-set)
awk -F/ '/^server=/ {print $2}' /tmp/cn.conf > /etc/smartdns/cn-domain.list

# 下载Google域名列表
curl -sL https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/google.china.conf -o /tmp/google.conf
awk -F/ '/^server=/ {print $2}' /tmp/google.conf >> /etc/smartdns/cn-domain.list

# 去重
sort -u /etc/smartdns/cn-domain.list -o /etc/smartdns/cn-domain.list
wc -l /etc/smartdns/cn-domain.list

启动并验证SmartDNS:


1
2
3
4
5
6
systemctl restart smartdns
systemctl status smartdns

# 测试解析(应返回多个IP的测试结果)
dig @127.0.0.1 -p 6053 www.baidu.com +short
dig @127.0.0.1 -p 6053 www.google.com +short

如果Google域名能解析、国内域名也能解析,说明分流规则生效。

四、部署AdGuard Home:广告过滤层

4.1 Docker方式部署(推荐)

使用Docker Compose部署AdGuard Home便于管理和升级。首先确保Docker已安装:


1
2
curl -fsSL https://get.docker.com | bash
systemctl enable docker --now

创建AdGuard Home的工作目录和compose文件:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
mkdir -p /opt/adguardhome/conf /opt/adguardhome/work

cat > /opt/adguardhome/docker-compose.yml << 'EOF'
version: '3.8'
services:
  adguardhome:
    image: adguard/adguardhome:latest
    container_name: adguardhome
    restart: unless-stopped
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "80:80/tcp"
      - "443:443/tcp"
      - "853:853/tcp"
    volumes:
      - ./work:/opt/adguardhome/work
      - ./conf:/opt/adguardhome/conf
    environment:
      - TZ=Asia/Shanghai
EOF

首次启动AdGuard Home以生成默认配置:


1
2
3
4
5
cd /opt/adguardhome
docker compose up -d

# 查看日志确认启动
docker logs adguardhome

4.2 配置AdGuard Home

首次启动后,访问

1
http://你的VPS_IP:80

进入安装向导。设置好管理员账号密码后,将监听端口改为53,点击完成。

然后编辑AdGuard Home的配置文件

1
/opt/adguardhome/conf/AdGuardHome.yaml

,配置上游DNS指向SmartDNS:


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
dns:
  bind_hosts:
    - 0.0.0.0
  port: 53
  protection_enabled: true
  filtering_enabled: true
  upstream_dns:
    - 127.0.0.1:6053   # 转发到SmartDNS
  fallback_dns:
    - 223.5.5.5
    - 1.1.1.1
  bootstrap_dns:
    - 223.5.5.5
    - 119.29.29.29
  cache_size: 4194304   # 4MB缓存
  cache_ttl_min: 60
  cache_ttl_max: 86400
  cache_optimistic: true
  enable_dnssec: false
  edns_client_subnet:
    enabled: true
  ratelimit: 100
  ratelimit_subnet_len: 24
  blocked_response_ttl: 10
  safe_search:
    enabled: false
  upstream_mode: load_balance
  local_ptr_upstreams: []
  use_private_ptr_resolvers: true

重启AdGuard Home使配置生效:


1
docker compose restart

4.3 添加广告过滤规则

AdGuard Home的核心价值在于过滤规则。以下推荐几套高质量的规则源,在Web界面过滤器中添加过滤规则:

规则名称 订阅地址 用途
AdGuard DNS Filter https://adguardteam.github.io/AdGuardDNSFilter/Filters/filter.txt 官方基础过滤
AdGuard Base https://raw.githubusercontent.com/AdguardTeam/AdguardFilters/master/BaseFilter/sections/filter.txt 广告基础规则
anti-AD https://anti-ad.net/easylist.txt 国内广告与隐私追踪
CHN-AD规则 https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjx-annoyance.txt 国内网站烦扰元素
EasyPrivacy https://easylist.to/easylist/easyprivacy.txt 隐私追踪防护
大圣净化规则 https://raw.githubusercontent.com/jdlingyu/ad-wars/master/hosts 国内应用广告

添加规则后,点击更新按钮拉取最新规则。建议设置每周自动更新一次。

4.4 自定义过滤规则

除了订阅规则外,AdGuard Home还支持自定义过滤规则,语法兼容AdGuard/Adblock Plus格式:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 屏蔽特定域名
||example-ads.com^

# 屏蔽特定子域名及所有子域
||*.tracking.example.com^

# 允许特定域名(白名单,优先级最高)
@@||cdn.example.com^

# 按通配符屏蔽
||*.ads.*^$important

# 屏蔽特定路径(使用正则)
/example\.com\/ads\//$important

五、启用DNS加密:DoH与DoT

5.1 获取SSL证书

使用Let’s Encrypt为你的域名签发证书。假设你已将

1
dns.example.com

解析到VPS:


1
2
3
4
5
6
7
8
9
10
11
12
apt install -y certbot

# 使用standalone模式获取证书(需先停止80端口服务)
docker compose stop
certbot certonly --standalone -d dns.example.com --non-interactive --agree-tos -m admin@example.com

# 复制证书到AdGuard Home目录
cp /etc/letsencrypt/live/dns.example.com/fullchain.pem /opt/adguardhome/conf/cert.crt
cp /etc/letsencrypt/live/dns.example.com/privkey.pem /opt/adguardhome/conf/private.key
chmod 644 /opt/adguardhome/conf/cert.crt
chmod 600 /opt/adguardhome/conf/private.key
docker compose start

5.2 配置DoT和DoH

编辑

1
AdGuardHome.yaml

,添加TLS配置:


1
2
3
4
5
6
7
8
9
10
11
12
tls:
  enabled: true
  server_name: dns.example.com
  force_https: false
  port_https: 443
  port_dns_over_tls: 853
  port_dns_over_quic: 784
  certificate_chain_file: /opt/adguardhome/conf/cert.crt
  private_key_file: /opt/adguardhome/conf/private.key
  valid_chain: true
  strict_sni: false
  allow_unencrypted_doh: false

设置证书自动续期:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建续期脚本
cat > /opt/adguardhome/renew-cert.sh << 'EOF'
#!/bin/bash
docker compose -f /opt/adguardhome/docker-compose.yml stop
certbot renew --quiet
cp /etc/letsencrypt/live/dns.example.com/fullchain.pem /opt/adguardhome/conf/cert.crt
cp /etc/letsencrypt/live/dns.example.com/privkey.pem /opt/adguardhome/conf/private.key
docker compose -f /opt/adguardhome/docker-compose.yml start
EOF

chmod +x /opt/adguardhome/renew-cert.sh

# 添加到crontab
echo '0 3 1 * * /opt/adguardhome/renew-cert.sh' | crontab -

5.3 客户端配置DoH/DoT

在不同设备上使用加密DNS:

Android:设置 – 网络 – 私人DNS – 输入

1
dns.example.com

(自动使用DoT)

iOS(iOS 14+):安装描述文件

1
https://dns.example.com/install.mobileconfig

(需配置Web服务返回描述文件)

Windows 11:设置 – 网络和Internet – 以太网/Wi-Fi – DNS服务器分配 – 编辑 – 选择手动 – 首选DNS为

1
dns.example.com

– DNS over HTTPS开启

浏览器:Chrome – 设置 – 安全 – 使用安全DNS – 自定义 – https://dns.example.com/dns-query

六、性能调优与维护

6.1 SmartDNS性能调优

如果VPS负载较高,可以调整SmartDNS的并发查询数和缓存策略:


1
2
3
4
5
6
7
8
9
10
11
# 增大并发查询数
dnsmasq-args --max-concurrent-queries 200

# 调整缓存参数
cache-size 65536
cache-memory 33554432  # 32MB

# 启用持久化缓存(重启不丢失)
cache-persist yes
cache-file /etc/smartdns/smartdns.cache
cache-auto-quit yes

6.2 AdGuard Home查询日志维护

查询日志会占用磁盘空间,需定期清理。在AdGuard Home配置文件中设置日志轮转:


1
2
3
4
5
6
log:
  file: "/opt/adguardhome/work/querylog.jsonl"
  max_backups: 7
  max_size: 100  # MB
  max_age: 30    # days
  compress: true

6.3 监控关键指标

AdGuard Home自带Prometheus指标端点,地址为

1
http://VPS_IP:80/metrics

。可以在Prometheus中添加抓取任务:


1
2
3
4
5
scrape_configs:
  - job_name: 'adguardhome'
    static_configs:
      - targets: ['VPS_IP:80']
    metrics_path: /metrics

关键监控指标:

  • 1
    adg_dns_queries_total

    — 累计查询数

  • 1
    adg_dns_queries_blocked_total

    — 被拦截查询数

  • 1
    adg_dns_queries_avg_duration

    — 平均查询延迟

  • 1
    adg_dns_cache_size

    — 缓存大小

  • 1
    adg_dns_upstreams

    — 上游DNS状态

6.4 定期更新过滤规则

AdGuard Home默认每24小时更新一次过滤规则。如果需要更频繁的更新,可以手动触发或调整更新间隔:


1
2
3
4
5
6
7
# 在AdGuardHome.yaml的filters部分
filters:
  - enabled: true
    url: https://adguardteam.github.io/AdGuardDNSFilter/Filters/filter.txt
    name: AdGuard DNS Filter
    id: 1
    interval: 168  # 小时,168=每周一次

对国内用户而言,anti-AD和CHN-AD规则建议每周更新一次,以跟得上新出现的广告域名。

七、典型问题排查

7.1 53端口被占用

常见于Ubuntu系统,systemd-resolved会占用53端口:


1
2
3
4
5
6
7
# 停止并禁用systemd-resolved
systemctl stop systemd-resolved
systemctl disable systemd-resolved

# 重新配置resolv.conf
rm /etc/resolv.conf
echo 'nameserver 127.0.0.1' > /etc/resolv.conf

7.2 DNS解析慢或超时

排查步骤:


1
2
3
4
5
6
7
8
9
10
11
12
# 1. 检查SmartDNS是否正常
systemctl status smartdns
dig @127.0.0.1 -p 6053 example.com

# 2. 检查上游DNS连通性
curl -sI https://doh.pub/dns-query

# 3. 检查AdGuard Home日志
docker logs adguardhome --tail 50

# 4. 检查是否有过多被拦截的查询导致延迟
docker exec adguardhome cat /opt/adguardhome/work/querylog.jsonl | tail -100

7.3 过滤规则失效

常见原因和解决方法:

  • 规则未更新:手动点击更新按钮,或检查更新间隔配置
  • 规则冲突:某些白名单规则可能覆盖了黑名单,使用
    1
    $important

    标记强制生效

  • 客户端使用DoH绕过:某些应用(如Chrome)内置了DoH,会绕过系统DNS,需在客户端禁用或配置强制透明代理
  • CNAME解析绕过:某些广告域名通过CNAME指向普通域名,需启用拦截CNAME类型记录选项

7.4 解析结果不优

如果SmartDNS返回的IP速度不理想:


1
2
3
4
5
6
7
8
# 调整测速方法,使用tcp_ping(更贴近HTTP场景)
speed-check-mode tcp:443,ping

# 增加测速候选IP数量
speed-check-mode-timeout 1000

# 调整双栈选择阈值
dualstack-ip-selection-threshold 50

也可以为特定域名指定上游DNS,绕过测速:


1
2
# 强制github.com走Google DNS
domain-rules /github.com/ -n -a 8.8.8.8 -g fixed

八、安全加固建议

8.1 限制访问来源

如果DNS仅供自己使用,建议在防火墙层面限制53端口的访问来源:


1
2
3
4
5
# 仅允许指定IP段访问
ufw delete allow 53/udp
ufw delete allow 53/tcp
ufw allow from 203.0.113.0/24 to any port 53 proto any comment 'MyDNS'
ufw allow from 198.51.100.0/24 to any port 53 proto any comment 'OfficeDNS'

8.2 启用DNS查询速率限制

防止DNS放大攻击和异常查询:


1
2
3
4
5
6
# AdGuardHome.yaml
  ratelimit: 50
  ratelimit_subnet_len: 24
  ratelimit_whitelist:
    - 127.0.0.1
    - 203.0.113.1

8.3 启用EDNS Client Subnet

对CDN类域名,启用EDNS Client Subnet让CDN返回距离用户最近的节点:


1
2
3
edns_client_subnet:
  enabled: true
  custom_ip: ''  # 留空则使用客户端真实IP

但需注意,EDNS会向DNS上游暴露你的客户端IP前缀,对隐私敏感的用户可以禁用。

九、总结

AdGuard Home + SmartDNS的组合提供了一套完整的私有DNS解决方案。AdGuard Home负责广告过滤和客户端管理,SmartDNS负责智能解析和测速,两者协同既能为整个网络提供广告屏蔽,又能针对国内/海外域名做智能分流加速。

整个系统的资源占用很低,在一台512MB内存的VPS上完全可以稳定运行。配合DoH/DoT加密后,可以安全地在公共网络使用,避免DNS劫持和运营商的DNS污染。

实际使用中建议关注的几个要点:定期更新过滤规则、监控查询量异常、备份配置文件。如果VPS有多个IP,还可以为AdGuard Home配置多IP绑定,实现不同设备路由到不同DNS策略的精细化控制。

【本站文章皆为原创,未经允许不得转载】:汤不热吧 » VPS搭建私有DNS与广告过滤系统:AdGuard Home + SmartDNS 完整部署指南
分享到: 更多 (0)