前言
之前,我一直使用 Adguard Home 作為我的 DNS 伺服器,因為他具有透過 DNS 阻擋廣告的功能,他是透過設定特定的 DNS 上游完成一次 DNS 查詢。因此,當你透過各種 DNS 查詢工具來解析你是使用哪個 DNS 時,你只會看到你上游的名字,而不是你這台 DNS 主機所使用的 IP 位址。這就讓我找到 Unbound 這款軟體,可以解決這個問題。
Unbound 是由 NLnet Labs 所開發的,是個具有驗證、遞迴及快取的 DNS 伺服器。與 Adguard Home 這種 DNS 的不同之處,他並不是利用查找特定上游 DNS 的方式做解析的。而是真實的使用你電腦的網路來去對各個域名伺服器做遞迴的解析。
安裝步驟
首先我們必須先安裝必要的依賴。
sudo apt-get install -y build-essential libssl-dev libexpat1-dev bison flex
下載最新的 Unbound 並解壓縮他。
wget https://nlnetlabs.nl/downloads/unbound/unbound-latest.tar.gz
tar xzf unbound-latest.tar.gz
使用 cd
進入該目錄(文章撰寫當下是 unbound-1.22.0
)。
cd unbound-1.22.0
接著我們進行 configure。
./configure
最後編譯並安裝。
make && make install
配置文件
接著我們需要配置 Unbound 的文件讓他讀取。
在 /etc/
底下建立 Unbound 的資料夾,並在裡面新增 unbound.conf
檔案。
mkdir -p /etc/unbound && vim /etc/unbound/unbound.conf
貼上以下內容:
server:
interface: 0.0.0.0
interface: ::0
access-control: 0.0.0.0/0 allow
access-control: ::/0 allow
deny-any: yes
ip-ratelimit: 20
你可以視需求更改,完成後保存離開就可以囉!
建立使用者
Unbound 預設會以 unbound
這個使用者開始運作,因此如果你跳過建立使用者直接執行的話,他可能會發生錯誤,因為找不到使用者。
使用以下命令建立這個使用者:
sudo adduser unbound --shell /usr/sbin/nologin
密碼的部分隨你喜好輸入即可,這樣這部就完成了。
建立服務
接著,我們需要為 Unbound 配置一個新的服務,以便我們可以用命令控制他。
在 /etc/systemd/system
裡面建立一個檔案 unbound.service
。
vim /etc/systemd/system/unbound.service
貼上以下內容:
[Unit]
Description=Unbound Recursive Domain Name Server
After=syslog.target network.target
Before=nss-lookup.target
Wants=nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/local/sbin/unbound -d -c /etc/unbound/unbound.conf
Restart=always
[Install]
WantedBy=multi-user.target
保存後,使用命令讓 systemd 重新生效。
sudo systemctl daemon-reload
現在,我們可以透過指令來控制 Unbound 了。
sudo systemctl start unbound # 啟動 Unbound
sudo systemctl stop unbound # 停止 Unbound
sudo systemctl restart unbound # 重新啟動 Unbound
sudo systemctl enable unbound # 開機時啟動 Unbound
sudo systemctl disable unbound # 開機時不啟動 Unbound
結語
現在你完成了 Unbound 的設定!你可以將你電腦的 DNS 指向這台 Unbound,並前往一個我寫的小網站:https://tools.cre0809.com/myip 來看看你的 DNS 設定有沒有成功!