AdGuard Home (AGH) 預設是做為「DNS 轉發器」,將網域解析工作交給外部公共 DNS 伺服器。
但這意味著所有的瀏覽足跡(使用者在查任何網址)都會暴露給第三方(如 Google/ISP網路供應商)。
所以,若在本地追加架設 Unbound 做為 AGH 的唯一上游,這樣會有什麼好處呢:
網路瀏覽隱私與安全: 透過啟用 DNSSEC(防止 DNS 污染/篡改)與 QNAME Minimisation(最小化權威查詢,極致保護瀏覽隱私)。
極速的本地快取機制: 透過快取(Cache)設計,Unbound 與 AGH 會將常用網域的 IP 紀錄強制留存在本機記憶體(RAM)中。後續全家內網設備再次訪問相同網站時,直接在區域網路內以 0ms 秒回,大幅壓低 DNS 解析階段的延遲。
兩者搭配使用是兼顧「網路控制權(AGH 擋廣告)」與「底層解析安全(Unbound 防污染)」的自建DNS伺服器的優質組合。
網址:
https://unbound.docs.nlnetlabs.nl/en/latest/index.html
安裝套件:
sudo apt update
sudo apt install unbound
# 持續利用 CPU 的硬體隨機數產生器(或者是偽隨機優化),改善DNSSEC查詢領票的性能瓶頸。
sudo apt install rng-tools-debian
PS. WIN10/11也有套件能裝,詳細可以看官方網站:
https://nlnetlabs.nl/projects/unbound/about/
安裝好套件後,我們要新增一個適用的設定檔:
sudo nano /etc/unbound/unbound.conf.d/00-JIRcustom.conf
server:
# 只在本機回應查詢
interface: 127.0.0.1
port: 5335
# 僅允許本機程式(如 AdGuard Home)查詢
access-control: 127.0.0.0/8 allow
# 啟用 DNSSEC 驗證與安全加固
val-log-level: 1
harden-dnssec-stripped: yes
harden-glue: yes
harden-below-nxdomain: yes
harden-referral-path: yes
harden-algo-downgrade: yes
harden-large-queries: yes
# 啟用 QNAME minimisation(隱私強化)
qname-minimisation: yes
aggressive-nsec: yes
# 快取優化(依 RAM 調整)
msg-cache-size: 50m
rrset-cache-size: 100m
cache-min-ttl: 360
cache-max-ttl: 86400
# 網路性能設定
num-threads: 2
so-rcvbuf: 4m
so-sndbuf: 4m
# 縮短對卡頓/沒回應上游 DNS 的探測等待時間,預設 900 (90秒),改成 10 或 15 (秒)
# 這樣只要中華電信或 Google 某個 IP 晚上爆 ping 沒回應,Unbound 就會更果斷地跳過它
infra-host-ttl: 15
# 允許 Unbound 記住那些反應很慢的 IP 的時間(秒),改成 60 秒即可
infra-cache-numhosts: 10000
# 稍微再加大每個執行緒允許的併發請求數(防範 exceeded 再次發生)
num-queries-per-thread: 4096
#系統檔案描述符限制:
# ulimit -n
#取第一個 PID 再查:
# cat /proc/$(pgrep unbound | head -1)/limits | grep "open files"
#Unbound 實際運行時的限制比 ulimit -n,outgoing-range: 8192 可以安全加入設定。
outgoing-range: 8192
# 日誌(可選)
#logfile: "/var/log/unbound/unbound.log"
#verbosity: 1
# 根伺服器查詢(預設遞迴)
# 不使用任何上游,直接查 Root DNS
# 變更為轉發模式,改由台灣最快、全球最穩的公共 DNS 幫我們跑腿
forward-zone:
name: "."
# 台灣本地極速:中華電信 IPv4 & IPv6
forward-addr: 168.95.1.1
forward-addr: 168.95.192.1
forward-addr: 2001:b000:168::1
forward-addr: 2001:b000:168::2
# 全球最穩:Google DNS IPv4 & IPv6
forward-addr: 8.8.8.8
forward-addr: 8.8.4.4
forward-addr: 2001:4860:4860::8888
forward-addr: 2001:4860:4860::8844
解析的緩衝區快取配合設定檔,改成4MB佔用空間。
# 暫時生效
sudo sysctl -w net.core.rmem_max=4194304
sudo sysctl -w net.core.wmem_max=4194304
# 永久生效(寫入 sysctl.conf)
echo "net.core.rmem_max=4194304" | sudo tee -a /etc/sysctl.conf
echo "net.core.wmem_max=4194304" | sudo tee -a /etc/sysctl.conf
設定好了以後,可以啟動和確認是不是有其他問題:
sudo unbound-checkconf
sudo systemctl restart unbound
sudo systemctl status unbound
如果啟用後,沒有異常問題的話。
可以先這樣驗證解析的速度。
第一次:
dig @127.0.0.1 -p 5335 google.com | grep "Query time"
隔個幾秒後再一次:
dig @127.0.0.1 -p 5335 google.com | grep "Query time"
應該會看到有快取紀錄後,解析網址的速度會變超快。
接著就可以把AGH2上游的 DNS 伺服器,全部就只留一個127.0.0.1:5335。
讓他專心地處理比對封鎖的網址就好,而DNS解析的部分就交給unbound本機端處理了。
然後,可以檢查是不是解析的速度和封包比率有沒有隨著時間改善:
sudo unbound-control stats_noreset | grep -E "exceeded|recursion.time.avg|cachehits|cachemiss"
還有追加一個,如果設備會定期重開機,可以寫一個簡單的設定檔。
這樣可以把之前已經校正調教後的快取狀態,備份起來。
後面完成重開機後,就把資料放回去繼續使用已經記憶的服務資料。
減少因為重開機後,又要把unbound重新建構快取資料庫的等待預熱狀態。
sudo nano /etc/systemd/system/unbound-cache-persist.service
[Unit]
Description=Persist Unbound DNS cache across restarts
# 啟動時:Unbound 先啟動,之後載入
After=unbound.service
# 停止時:先 dump備份,Unbound 之後才關閉
Before=unbound.service
# 系統重開機、還是手動 restart unbound,都同步跟隨
BindsTo=unbound.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c '\
sleep 5; \
if [ -f /var/lib/unbound/cache.dump ]; then \
/usr/sbin/unbound-control load_cache < /var/lib/unbound/cache.dump \
&& rm /var/lib/unbound/cache.dump; \
fi'
ExecStop=/bin/bash -c '\
/usr/sbin/unbound-control dump_cache > /var/lib/unbound/cache.dump'
[Install]
WantedBy=multi-user.target
存檔後,把設定服務的檔案啟用。
sudo systemctl daemon-reload
sudo systemctl enable unbound-cache-persist.service
sudo systemctl start unbound-cache-persist.service
sudo systemctl status unbound-cache-persist.service
這樣應該就高枕無憂了。