如何在Ubuntu 20.04上使用Dnsmasq設置本地DNS解析器

如何在Ubuntu 20.04上使用Dnsmasq設置本地DNS解析器

Dnsmasq代表「 DNS偽裝」的縮寫,是一種用於小型網路的簡單,輕便且易於使用的DNS轉發器。 可以將其配置為DNS緩存和DHCP伺服器,並支持IPv4和IPv6協議。 當它收到任何DNS查詢時,它將從其緩存中答覆它們,或轉發到其他DNS伺服器。

Dnsmasq由三個子系統組成:

  • DNS子系統:用於緩存不同的記錄類型,包括A,AAAA,CNAME和PTR。
  • DHCP子系統:支持DHCPv4,DHCPv6,BOOTP和PXE
  • 路由器廣告子系統:它為IPv6主機提供基本的自動配置。 它可以獨立使用,也可以與DHCPv6結合使用。

在本教程中,我們將向您展示如何在Ubuntu 20.04伺服器上使用Dnsmasq設置本地DNS伺服器。

先決條件

  • 運行Ubuntu 20.04的伺服器。
  • 為伺服器配置了root密碼。

入門

首先,建議將系統軟體包更新為最新版本。 您可以通過運行以下命令來更新所有軟體包:

apt-get update -y

更新所有軟體包後,您將需要在系統中禁用Systemd解析的服務。 Systemd解析的服務用於本地應用程序的網路名稱解析。

您可以通過運行以下命令禁用它:

systemctl disable --now systemd-resolved

禁用該服務後,您將需要刪除默認的resolv.conf文件,並使用您的自定義DNS伺服器詳細信息創建一個新文件。

您可以使用以下命令刪除默認的resolv.conf文件:

rm -rf /etc/resolv.conf

接下來,使用以下命令將Google DNS伺服器添加到resolv.conf文件:

echo "nameserver 8.8.8.8" > /etc/resolv.conf

完成後,您可以繼續下一步。

安裝Dnsmasq

默認情況下,Ubuntu 20.04默認存儲庫中提供Dnsmasq。 您可以通過運行以下命令來安裝它:

apt-get install dnsmasq dnsutils ldnsutils -y

安裝完成後,Dnsmasq服務將自動啟動。 您可以使用以下命令檢查Dnsmasq的狀態:

systemctl status dnsmasq

您應該獲得以下輸出:

dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
     Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-11-02 11:02:01 UTC; 15s ago
   Main PID: 17726 (dnsmasq)
      Tasks: 1 (limit: 2282)
     Memory: 868.0K
     CGroup: /system.slice/dnsmasq.service
             ??17726 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new --local-service --trust-anchor=.,20326,8,2,e>

Nov 02 11:02:12 ubuntu2004 systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Nov 02 11:02:12 ubuntu2004 dnsmasq[17705]: dnsmasq: syntax check OK.
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: started, version 2.80 cachesize 150
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: DNS service limited to local subnets
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify dumpfi>
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: reading /etc/resolv.conf
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: using nameserver 8.8.8.8#53
Nov 02 11:02:12 ubuntu2004 dnsmasq[17726]: read /etc/hosts - 7 addresses
Nov 02 11:02:12 ubuntu2004 systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.

完成後,您可以繼續下一步。

配置Dnsmasq

接下來,您將需要將Dnsmasq配置為本地DNS伺服器。 您可以通過編輯Dnsmasq主配置文件來做到這一點:

nano /etc/dnsmasq.conf

更改以下行:

port=53
domain-needed
bogus-priv
listen-address=127.0.0.1,your-server-ip
expand-hosts
domain=dns-example.com
cache-size=1000

完成後,保存並關閉文件。

接下來,您需要在resolv.conf文件中將伺服器的IP地址添加為主域名伺服器。 您可以使用以下命令添加它:

nano /etc/resolv.conf

在「名稱伺服器8.8.8.8」行上方添加以下行:

nameserver your-server-ip

完成後,保存並關閉文件。 接下來,使用以下命令驗證伺服器是否存在任何配置錯誤:Advertisement.banner-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px實心#dfdfdf} .banner-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px實心#dfdfdf}

dnsmasq --test

如果一切正常,您應該獲得以下輸出:

dnsmasq: syntax check OK.

最後,重新啟動Dnsmasq服務以應用更改:

systemctl restart dnsmasq

此時,Dnsmasq已啟動並在埠53上偵聽。您可以使用以下命令進行驗證:

ss -alnp | grep -i :53

您應該獲得以下輸出:

udp     UNCONN   0        0                                             0.0.0.0:53                                                0.0.0.0:*                      users:(("dnsmasq",pid=41051,fd=4))                                             
udp     UNCONN   0        0                                                [::]:53                                                   [::]:*                      users:(("dnsmasq",pid=41051,fd=6))                                             
tcp     LISTEN   0        32                                            0.0.0.0:53                                                0.0.0.0:*                      users:(("dnsmasq",pid=41051,fd=5))                                             
tcp     LISTEN   0        32                                               [::]:53                                                   [::]:*                      users:(("dnsmasq",pid=41051,fd=7))                                             

將DNS記錄添加到Dnsmasq伺服器

接下來,您將需要編輯/ etc / hosts文件並添加本地DNS伺服器條目。Advertisement.large-leaderboard-2{text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px實心#dfdfdf}

nano /etc/hosts

添加以下行:

your-server-ip host1.dns-example.com

完成後,保存並關閉文件。

驗證Dnsmasq伺服器解析度

此時,將安裝並配置Dnsmasq。 否,該驗證DNS解析了。

您可以使用dig命令檢查DNS解析,如下所示:

dig host1.dns-example.com +short

如果一切正常,您應該在以下輸出中看到伺服器ip:

your-server-ip

您還可以使用以下命令來驗證外部DNS解析:

dig howtoforge.com +short

您應該獲得以下輸出:

172.67.68.93
104.26.3.165
104.26.2.165

配置遠程客戶端以使用Dnsmasq DNS伺服器

接下來,您將需要配置一個遠程客戶端,以將您的Dnsmasq DNS伺服器用作默認DNS伺服器。

首先,使用以下命令安裝DNS工具:

apt-get install dnsutils ldnsutils -y

安裝後,您將需要編輯/etc/resolv.conf文件和Dnsmasq DNS伺服器條目。Advertisement.leader-1{text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px實心#dfdfdf} .leader-1 {text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px實心#dfdfdf}

nano /etc/resolv.conf

在文件開頭添加以下行:

nameserver your-server-ip

完成後,保存並關閉文件。

接下來,使用以下命令驗證本地DNS解析:

dig host1.dns-example.com

您應該看到以下輸出:

; DiG 9.9.5-3ubuntu0.4-Ubuntu host1.dns-example.com
;; global options: +cmd
;; Got answer:
;; HEADER opcode: QUERY, status: NOERROR, id: 26401
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;host1.dns-example.com.		IN	A

;; ANSWER SECTION:
host1.dns-example.com.	0	IN	A	45.58.32.165

;; Query time: 301 msec
;; SERVER: 45.58.32.165#53(45.58.32.165)
;; WHEN: Mon Nov 02 16:49:37 IST 2020
;; MSG SIZE  rcvd: 66

接下來,您將需要驗證DNS伺服器是否可以緩存。 您可以使用鑽取實用程序進行檢查。

首先,運行以下命令:

drill google.com | grep "Query time"

您應該看到以下輸出:

;; Query time: 290 msec

接下來,再次運行命令以檢查緩存是否正常工作:

drill google.com | grep "Query time"

您應該看到查詢時間現在減少到4毫秒:

;; Query time: 4 msec

結論

恭喜你 您已成功安裝Dnsmasq並將其配置為本地DNS伺服器和Ubuntu 20.04。 我希望您現在可以輕鬆地在本地網路中實現它以進行名稱解析。

資訊來源:由0x資訊編譯自HOWTOFORGE,版權歸作者所有,未經許可,不得轉載
你可能還喜歡