🐧 Linux DNS Cache Server 架設(Unbound / dnsmasq)
透過 DNS 快取伺服器(Cache Server)可有效減少查詢延遲、降低外部流量負擔,常見方案包含 Unbound 與 dnsmasq。本文將逐步說明在 Linux 環境中部署、設定與驗證的方法。
🧱 一、什麼是 DNS Cache Server?
DNS Cache Server(域名快取伺服器)負責在使用者查詢網域名稱時,暫存解析結果,以便相同請求可直接從快取回應。此舉不僅能節省外部 DNS 查詢時間,也能降低網路延遲與頻寬消耗。
- Unbound:高安全性、支援 DNSSEC 驗證。
- dnsmasq:輕量級,適合內網與小型系統。
⚙️ 二、安裝 Unbound / dnsmasq
以常見發行版為例:
# Rocky / CentOS / RHEL
sudo dnf install unbound -y
sudo systemctl enable --now unbound
# Ubuntu / Debian
sudo apt install unbound -y
sudo systemctl enable --now unbound
# 或安裝 dnsmasq
sudo apt install dnsmasq -y
sudo systemctl enable --now dnsmasq
📂 三、Unbound 基本設定
Unbound 的主設定檔位於 /etc/unbound/unbound.conf,可根據需求修改如下:
server:
interface: 0.0.0.0
access-control: 192.168.0.0/16 allow
verbosity: 1
cache-min-ttl: 3600
cache-max-ttl: 86400
prefetch: yes
num-threads: 2
forward-zone:
name: "."
forward-addr: 8.8.8.8
forward-addr: 1.1.1.1
完成後重新啟動服務:
sudo systemctl restart unbound
sudo systemctl status unbound
🧩 四、dnsmasq 快取設定範例
dnsmasq 設定檔位於 /etc/dnsmasq.conf,範例如下:
no-resolv
server=8.8.8.8
server=1.1.1.1
cache-size=1000
log-queries
log-facility=/var/log/dnsmasq.log
儲存後重新啟動服務:
sudo systemctl restart dnsmasq
🔍 五、測試 DNS 快取是否生效
可使用 dig 或 drill 測試快取回應時間:
dig @127.0.0.1 google.com
dig @127.0.0.1 google.com # 第二次應顯著加快
若啟用 log,可觀察 /var/log/messages 或 /var/log/dnsmasq.log 內是否有命中(cache hit)紀錄。
🛡 六、安全強化與封鎖惡意域名
- 在 Unbound 中啟用
auto-trust-anchor-file以支援 DNSSEC。 - 搭配黑名單(例如
/etc/unbound/adblock.conf)可過濾廣告與惡意站台。 - dnsmasq 可結合
address=/example.com/0.0.0.0阻擋指定域名。
📘 結語
無論是企業網路或家庭伺服器,部署 DNS 快取服務都能顯著提升網頁解析速度與安全性。Unbound 適合需要 DNSSEC 驗證的環境;dnsmasq 則以輕量與彈性見長。根據網路規模與維護需求選擇合適方案,即可在內網中建立高效、安全的 DNS 基礎架構。
🔗 延伸閱讀
- 🧱 Linux Proxy Server 建置教學(Squid 安裝與設定指南)
- 🧩 Linux systemd 深入解析與啟動流程管理
- 🧱 Linux Firewall + Fail2Ban 安全強化與自動封鎖設定
— WWFandy・系統與網路筆記
沒有留言: