From 1555ec02ca22236b67ae563963380d2e77db3c42 Mon Sep 17 00:00:00 2001 From: CallMeR <9463297+callmer@user.noreply.gitee.com> Date: Sun, 10 Dec 2023 18:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20SmartDNS=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.PVE制作DNS服务器.md | 149 ++++++++++++++++++-- src/debian/debian_dns_dnsmasq_cron.conf | 2 +- src/debian/debian_dns_dnsmasq_smartdns.conf | 2 +- src/debian/debian_dns_smartdns.conf | 50 ------- src/debian/debian_dns_smartdns_cron.conf | 12 -- 5 files changed, 138 insertions(+), 77 deletions(-) delete mode 100644 src/debian/debian_dns_smartdns.conf delete mode 100644 src/debian/debian_dns_smartdns_cron.conf diff --git a/06.PVE制作DNS服务器.md b/06.PVE制作DNS服务器.md index 5971450..41d1162 100644 --- a/06.PVE制作DNS服务器.md +++ b/06.PVE制作DNS服务器.md @@ -179,12 +179,24 @@ $ sudo crontab -e 若需使用 `SmartDNS` 代替 `Adguard Home` ,可使用 Debian 官方源进行安装,但其版本通常较为 “过时” 。 +因此,更推荐使用其 Github 仓库中的最新稳定版进行安装,官方仓库请参阅 [pymumu/smartdns](https://github.com/pymumu/smartdns/releases) 。 + +多数情况下,`SmartDNS` 足以提供良好的 DNS 解析服务,但为了进一步优化 DNS 解析流程,推荐与 `Dnsmasq` 嵌套使用。 + ```bash -## 安装 SmartDNS -$ sudo apt install smartdns +## 安装 Dnsmasq +$ sudo apt install dnsmasq ``` -因此,更推荐使用其 Github 仓库中的最新稳定版进行安装,官方仓库请参阅 [pymumu/smartdns](https://github.com/pymumu/smartdns/releases) 。 +检查 `dnsmasq.service` 服务状态,确保该服务开机自启。 + +```bash +## 检查 Dnsmasq 服务 +$ sudo systemctl status dnsmasq.service + +## 设置 Dnsmasq 服务开机自启 +$ sudo systemctl enable dnsmasq.service +``` 下载 `SmartDNS` 最新版本时,请根据系统架构选择合适的版本,逐行执行以下命令。 @@ -211,7 +223,7 @@ $ chmod +x ./install $ sudo ./install -i ``` -修改 `SmartDNS` 配置之前,需检查 `smartdns.service` 服务状态,确保该服务开机自启动。 +修改 `SmartDNS` 配置之前,需检查 `smartdns.service` 服务状态,确保该服务开机自启。 ```bash ## 检查 SmartDNS 服务 @@ -228,8 +240,11 @@ $ sudo systemctl enable smartdns.service 若需使用 `SmartDNS` 过滤广告,则需下载其广告过滤配置文件,为可选操作。 ```bash +## 创建 SmartDNS 配置文件目录 +$ sudo mkdir -p /etc/smartdns.d + ## 下载广告过滤配置文件 -$ sudo wget https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf +$ sudo wget https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns.d/anti-ad-smartdns.conf ``` 修改 `SmartDNS` 主配置文件之前,建议关闭 `SmartDNS` 并清理 DNS 缓存文件。 @@ -260,10 +275,10 @@ $ sudo nvim /etc/smartdns/smartdns.conf **额外说明:** -- 当不使用 `anti-ad-smartdns.conf` 进行广告过滤时,需移除主配置文件中对应配置项。 - - 由于修改了缓存路径,`SmartDNS` 的缓存将在系统重启时自动删除,请根据实际情况进行调整。 +- `SmartDNS` 端口监听参数为 `6053@lo` ,请根据实际情况进行调整。 + - 检查配置文件中关于本地域名及其上游 DNS 服务器相关配置,请根据实际情况进行调整。 ```bash @@ -281,13 +296,13 @@ $ sudo nvim /etc/smartdns/smartdns.conf # server 2402:4e00:: # server 2400:3200::1 -conf-file /etc/smartdns/anti-ad-smartdns.conf +conf-file /etc/smartdns.d/*.conf cache-file /tmp/smartdns.cache log-level notice -bind [::]:53 -bind-tcp [::]:53 +bind [::]:6053@lo +bind-tcp [::]:6053@lo serve-expired yes serve-expired-ttl 57600 @@ -319,7 +334,31 @@ server-https https://dns.alidns.com/dns-query ``` -### 6.2.定时任务 +### 6.2. SmartDNS 插件 + +该插件是一组为 `SmartDNS` 特别定制的配置文件,由 `bash` 脚本生成,用于加速中国境内域名的解析速度。 + +关于插件的详细介绍,请参阅 [SmartDNS China List 安装脚本](https://gitee.com/callmer/smartdns_china_list_installer) 。 + +若需使用该插件进行域名加速解析,则需下载其安装脚本,为可选操作。 + +脚本执行完成后,生成的 `SmartDNS` 配置文件存放于 `/etc/smartdns.d` 目录下。 + +```bash +## 下载插件安装脚本 +$ sudo wget https://gitee.com/callmer/smartdns_china_list_installer/raw/main/smartdns_plugin.sh -O /opt/smartdns_plugin.sh + +## 设置安装脚本可执行权限 +$ sudo chmod +x /opt/smartdns_plugin.sh + +## 安装脚本文件防篡改 +$ sudo chattr +i /opt/smartdns_plugin.sh + +## 执行安装脚本 +$ sudo bash /opt/smartdns_plugin.sh +``` + +### 6.3.定时任务 本步骤为可选操作,主要设置 `SmartDNS` 定时更新广告过滤配置文件和定时重启。 @@ -333,9 +372,93 @@ $ sudo crontab -e ```bash ## 定时任务配置项 -15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf +15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns.d/anti-ad-smartdns.conf -25 5 * * * /usr/bin/systemctl restart smartdns.service +20 5 * * * /usr/bin/systemctl restart smartdns.service + +``` + +若使用了 `SmartDNS` 插件,由于插件自带服务重启功能,因此定时任务可为如下配置。 + +```bash +## 定时任务配置项 +15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns.d/anti-ad-smartdns.conf + +20 5 * * * /usr/bin/bash /opt/smartdns_plugin.sh +``` + +### 6.4.配置 Dnsmasq + +`Dnsmasq` 的主配置文件一般位于 `/etc` 目录下,修改配置文件之前,使用以下命令将其备份。 + +```bash +## 备份 Dnsmasq 主配置文件 +$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak +``` + +使用 `neovim` 编辑器创建 `Dnsmasq` 主配置文件,执行以下命令。 + +```bash +## 创建 Dnsmasq 主配置文件 +$ sudo nvim /etc/dnsmasq.conf +``` + +在编辑器对话框中输入以下内容,并保存。 + +**额外说明:** + +- 请根据系统内存使用情况,调整缓存参数 `cache-size` 。 + +- `Dnsmasq` 有且仅有 `SmartDNS` 作为上游 DNS 服务器。 + +```bash +# This configuration file is customized by fox, +# Optimize dnsmasq parameters for local DNS server. + +# Main Config + +conf-dir=/etc/dnsmasq.d/,*.conf +conf-file=/etc/dnsmasq.conf + +log-facility=/var/log/dnsmasq.log +log-async=20 + +cache-size=1024 +edns-packet-max=1232 +rebind-domain-ok=/fox.home.arpa/ + +bind-dynamic +bogus-priv +domain-needed +localise-queries +local-service +no-hosts +no-negcache +no-resolv +no-round-robin +rebind-localhost-ok +stop-dns-rebind + +# DNS Filter + +server=/alt/ +server=/home.arpa/ +server=/ipv4only.arpa/ +server=/resolver.arpa/ +server=/example/ +server=/bind/ +server=/invalid/ +server=/local/ +server=/localhost/ +server=/onion/ +server=/test/ + +# DNS Server + +server=/fox.home.arpa/172.16.1.1 +server=/fox.home.arpa/fdac::1 +server=127.0.0.1#6053 +server=::1#6053 ``` diff --git a/src/debian/debian_dns_dnsmasq_cron.conf b/src/debian/debian_dns_dnsmasq_cron.conf index 013daf2..3bdd451 100644 --- a/src/debian/debian_dns_dnsmasq_cron.conf +++ b/src/debian/debian_dns_dnsmasq_cron.conf @@ -6,7 +6,7 @@ $ sudo chmod +x /opt/dnsmasq_plugin.sh ## 设置 crontab -15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf +15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns.d/anti-ad-smartdns.conf 20 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-dnsmasq.conf -O /etc/dnsmasq.d/anti-ad-dnsmasq.conf diff --git a/src/debian/debian_dns_dnsmasq_smartdns.conf b/src/debian/debian_dns_dnsmasq_smartdns.conf index 73569dd..1dd45d6 100644 --- a/src/debian/debian_dns_dnsmasq_smartdns.conf +++ b/src/debian/debian_dns_dnsmasq_smartdns.conf @@ -12,7 +12,7 @@ # server 2402:4e00:: # server 2400:3200::1 -conf-file /etc/smartdns/anti-ad-smartdns.conf +conf-file /etc/smartdns.d/*.conf cache-file /tmp/smartdns.cache log-level notice diff --git a/src/debian/debian_dns_smartdns.conf b/src/debian/debian_dns_smartdns.conf deleted file mode 100644 index 7b959bb..0000000 --- a/src/debian/debian_dns_smartdns.conf +++ /dev/null @@ -1,50 +0,0 @@ -# This configuration file is customized by fox, -# Optimize SmartDNS parameters for local DNS server. -# -# For use common DNS server as upstream DNS server, -# please modify 'server' parameter according to -# your network environment. -# -# eg: -# server 119.29.29.29 -# server 223.5.5.5 -# server 114.114.114.114 -# server 2402:4e00:: -# server 2400:3200::1 - -conf-file /etc/smartdns/anti-ad-smartdns.conf -cache-file /tmp/smartdns.cache - -log-level notice - -bind [::]:53 -bind-tcp [::]:53 - -serve-expired yes -serve-expired-ttl 57600 -serve-expired-reply-ttl 3 -prefetch-domain yes -serve-expired-prefetch-time 28800 - -force-qtype-SOA 65 - -server-tcp 119.29.29.29 -group dnspod -exclude-default-group -server-tcp 2402:4e00:: -group dnspod -exclude-default-group -nameserver /doh.pub/dnspod -nameserver /dot.pub/dnspod - -server-tcp 223.5.5.5 -group alidns -exclude-default-group -server-tcp 2400:3200::1 -group alidns -exclude-default-group -nameserver /dns.alidns.com/alidns - -server 172.16.1.1 -group intranet -exclude-default-group -server fdac::1 -group intranet -exclude-default-group -nameserver /fox.home.arpa/intranet -domain-rules /fox.home.arpa/ -speed-check-mode none -no-cache - -server-tls dot.pub -server-tls dns.alidns.com - -server-https https://doh.pub/dns-query -server-https https://dns.alidns.com/dns-query - diff --git a/src/debian/debian_dns_smartdns_cron.conf b/src/debian/debian_dns_smartdns_cron.conf deleted file mode 100644 index 6bce2b7..0000000 --- a/src/debian/debian_dns_smartdns_cron.conf +++ /dev/null @@ -1,12 +0,0 @@ -## 下载 smartdns 插件安装脚本 -$ sudo wget https://gitee.com/callmer/pve_toss_notes/raw/master/src/debian/debian_dns_smartdns_plugin.conf -O /opt/smartdns_plugin.sh - -## 设置安装脚本权限 -$ sudo chmod +x /opt/smartdns_plugin.sh - -## 设置 crontab - -15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf - -20 5 * * * /usr/bin/bash /opt/smartdns_plugin.sh -