一、功能介紹
CoreDNS 是一個靈活、可擴充的開源 DNS 伺服器,它支援多種協定和後端儲存,使其成為建構現代化、可自訂 DNS 基礎架構的理想選擇。本文將介紹如何安裝和設定 CoreDNS,讓你可以快速架設一個強大而穩定的 DNS 服務。
二、操作流程
-
下載 CoreDNS 並將二進位檔案放到 bin 資料夾,這裡我們以 1.11.1 為例,下載連結:Github
wget https://github.com/coredns/coredns/releases/download/v1.11.1/coredns_1.11.1_linux_amd64.tgz tar -xvf coredns_1.11.1_linux_amd64.tgz cd coredns_1.11.1_linux_amd64 cp coredns /usr/bin/coredns -
建立啟動檔案
vi /etc/systemd/system/coredns.service輸入以下內容:
[Unit] Description=CoreDNS DNS server Documentation=https://coredns.io After=network.target [Service] PermissionsStartOnly=true LimitNOFILE=1048576 LimitNPROC=512 CapabilityBoundingSet=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE NoNewPrivileges=true User=root WorkingDirectory=~ ExecStart=/usr/bin/coredns -conf=/etc/coredns/Corefile ExecReload=/bin/kill -SIGUSR1 Restart=on-failure [Install] WantedBy=multi-user.target儲存完成後重新載入 systemd 設定:
systemctl daemon-reload -
建立設定檔 Corefile
首先建立資料夾用於存放設定檔
mkdir /etc/coredns新增設定檔
vi /etc/coredns/Corefile這是一個簡單的設定,它使用 114 的公共 DNS 伺服器進行網域名稱解析,並啟用了一些基本的外掛。
. { errors log health ready forward . 114.114.114.114 cache }儲存檔案
-
確保 53 連接埠未被佔用
lsof -i:53dnsmasq 佔用 53 連接埠(CentOS 7 常見) 首先查看狀態是否啟用 systemctl status dnsmasq.service。 若已啟用,先 kill -9 該處理程序,停止服務
systemctl stop dnsmasq.service,再停用開機自動啟動systemctl disable dnsmasq.service。 停用後重新啟動伺服器測試,確保服務不會再啟動。systemd-resolved 佔用 53 連接埠(Ubuntu Server 常見) 首先修改 /etc/systemd/resolved.conf,內容如下:
[Resolve] DNS=114.114.114.114 DNSStubListener=noDNS 是設定本機指向的 DNS 伺服器,建議設定電信業者或公共 DNS。 DNSStubListener=no 是指不監聽 53 連接埠。 設定完成後執行指令重新啟動系統服務:
sudo systemctl daemon-reloadsudo systemctl restart systemd-resolved查看 /etc/resolv.conf 符號連結位置,執行指令ll /etc/resolv.conf,如果符號連結顯示如下,則不需要變更:/etc/resolv.conf -> /run/systemd/resolve/resolv.conf,否則設定符號連結sudo ln -s -f /run/systemd/resolve/resolv.conf /etc/resolv.conf。 -
設定開機自動啟動並啟動 CoreDNS
systemctl enable coredns systemctl start coredns
三、FAQ
問題 1:設定後依舊無法存取怎麼辦? 回答 1:檢查 CoreDNS 是否監聽 53 連接埠,如果正常,請檢查防火牆是否允許存取 53 連接埠。