1. 關(guān)于DNS解析服務(wù)
DNS(Domain Name System),即域名系統(tǒng)。
一句話總結(jié)DNS解析服務(wù)功能就是,將域名轉(zhuǎn)換為IP地址。
DNS解析服務(wù)過(guò)程中有如下幾個(gè)角色參與:
? 待解析的域名
? DNS客戶端
? DNS服務(wù)器
? 域名對(duì)應(yīng)的IP地址
DNS解析大致過(guò)程:
? 用戶在應(yīng)用程序中使用域名
? 應(yīng)用程序調(diào)用DNS客戶端申請(qǐng)域名解析
? DNS客戶端向DNS服務(wù)器發(fā)送待解析的域名(本地有解析緩存則不用再請(qǐng)求)
? DNS服務(wù)器將解析出的IP地址返回給客戶端(服務(wù)端可能會(huì)緩存DNS解析結(jié)果,避免重復(fù)查詢)
? 應(yīng)用程序使用域名對(duì)應(yīng)的IP地址(客戶端可能會(huì)緩存DNS解析結(jié)果,加快響應(yīng)速度)

Windows和Linux系統(tǒng)中都有常用的DNS解析服務(wù),例如Windows中的DNS Client,Linux中的systemd-resolved、BIND等。
而其中systemd-resolved是現(xiàn)代Linux發(fā)行版(基于systemd)中最常用的默認(rèn)DNS解析服務(wù),今天本文將分享systemd-resolved這款DNS解析服務(wù)。
2. systemd-resolved 的常用命令
2.1 查看當(dāng)前systemd-resolved的運(yùn)行狀態(tài)
systemctl status systemd-resolved
該服務(wù)在基于systemd的系統(tǒng)中是默認(rèn)開啟的,也可以將status字段更換為start或stop來(lái)控制該服務(wù)的開啟和關(guān)閉,因?yàn)楸疚闹饕獓@systemd-resolved展開,所以systemdctl的操作命令不再詳細(xì)展開。
2.2 查看systemd-resolved的統(tǒng)計(jì)信息,在輸出中可以查看緩存命中率、緩存大小、DNSSEC 驗(yàn)證情況。
systemd-resolve --statistics
2.3 檢查當(dāng)前DNS配置:
resolvectl status或systemd-resolve --status(舊版本兼容)
2.4 清理DNS緩存,systemd-resolved會(huì)緩存在內(nèi)存上,并不進(jìn)行持久化保存,若緩存過(guò)大可以使用該命令清理本地緩存。
systemd-resolve --flush-caches
2.5 手動(dòng)進(jìn)行域名解析測(cè)試,輸出中可以查看解析出來(lái)的IP、解析耗時(shí)等。
systemd-resolve www.xxx.com
2.6 臨時(shí)給端口配置DNS服務(wù)器,該配置重啟會(huì)失效,若想永久固定需修改/etc/systemd/resolved.conf等相關(guān)配置文件。
resolvectl dns eth0 8.8.8.8
3. systemd-resolved相關(guān)配置文件
3.1 /etc/resolv.conf配置文件
/etc/resolv.conf是最表面的DNS配置文件,systemd-resolved執(zhí)行DNS解析時(shí),會(huì)讀取該文件來(lái)獲取DNS服務(wù)器IP,從而進(jìn)行DNS解析。所以不難看出,/etc/resolv.conf是systemd-resolved服務(wù)中最重要的文件,因此/etc/resolv.conf會(huì)被多方控制,從而達(dá)到配置DNS解析服務(wù)的目的。若用戶想要臨時(shí)配置系統(tǒng)的DNS解析服務(wù)器,可以直接修改該文件,寫入對(duì)應(yīng)的服務(wù)器IP:
nameserver 114.114.114.114nameserver 8.8.8.8
不過(guò)以上修改操作只能是臨時(shí)的,因?yàn)?etc/resolv.conf正常情況下是動(dòng)態(tài)生成的,當(dāng)查看它的詳細(xì)信息時(shí)會(huì)發(fā)現(xiàn)是一個(gè)軟連接,它的狀態(tài)會(huì)隨著配置和用戶的動(dòng)作而變化。
lrwxrwxrwx 1 root root 37 Dec 5 14:24 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
這里所指向的stub-resolv.conf文件后面再詳細(xì)解釋。
3.2 /etc/systemd/resolved.conf配置文件
該文件是systemd-resolved的主要配置文件,通常用于設(shè)置DNS服務(wù)器、DNS解析策略等。修改這個(gè)文件后,需要重新啟動(dòng)systemd-resolved服務(wù)才能生效。
[Resolve]DNS=8.8.8.8 8.8.4.4 # DNS服務(wù)器列表FallbackDNS=114.114.114.114 # 當(dāng)主要DNS不可用時(shí),使用備用DNSDomains=example.com # 域名搜索拓展列表LLMNR=no # LLMNR(本地鏈路多播名稱解析)啟用設(shè)置DNSSEC=no # 是否啟用DNSSEC(域名系統(tǒng)安全擴(kuò)展)Cache=yes # 啟用DNS緩存
3.3 /run/systemd/resolve/stub-resolv.conf和/run/systemd/resolve/resolv.conf配置文件從3.1中可以發(fā)現(xiàn),/etc/resolv.conf就是指向了/run/systemd/resolve/stub-resolv.conf,所以/etc/resolv.conf同樣也能指向/run/systemd/resolve/resolv.conf,也證明這兩個(gè)文件才是真正的DNS服務(wù)器指定文件。這兩個(gè)文件實(shí)際由systemd-resolved服務(wù)直接管理和生成,其中:/run/systemd/resolve/resolv.conf文件:由systemd-resolved服務(wù)生成,其內(nèi)容由3.2提到的/etc/systemd/resolved.conf配置文件進(jìn)行指定,如下:
# This file is managed by man:systemd-resolved(8). Do not edit.## This is a dynamic resolv.conf file for connecting local clients directly to# all known uplink DNS servers. This file lists all configured search domains.## Third party programs must not access this file directly, but only through the# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,# replace this symlink by a static file or a different symlink.## See man:systemd-resolved.service(8) for details about the supported modes of# operation for /etc/resolv.conf.nameserver 8.8.8.8nameserver 8.8.4.4search example.com
/run/systemd/resolve/stub-resolv.conf文件:由systemd-resolved服務(wù)生成,配置使用DNS stub解析器,這種情況下應(yīng)用程序不會(huì)與systemd-resolved直接通信,而是由stub解析器轉(zhuǎn)發(fā),一般情況下應(yīng)用程序只需要向127.0.0.53查詢即可,由stub解析器轉(zhuǎn)發(fā)給systemd-resolved,避免了緊耦合:
# This file is managed by man:systemd-resolved(8). Do not edit.## This is a dynamic resolv.conf file for connecting local clients to the# internal DNS stub resolver of systemd-resolved. This file lists all# configured search domains.## Run "resolvectl status" to see details about the uplink DNS servers# currently in use.## Third party programs must not access this file directly, but only through the# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,# replace this symlink by a static file or a different symlink.## See man:systemd-resolved.service(8) for details about the supported modes of# operation for /etc/resolv.conf.nameserver 127.0.0.53options edns0 trust-adsearch example.com
4. 總結(jié)
在實(shí)際使用過(guò)程中,可以只關(guān)注/etc/resolv.conf文件即可:
? 想臨時(shí)修改系統(tǒng)DNS服務(wù)器,直接修改/etc/resolv.conf文件即可(或使用resolvectl命令修改,可以更好的控制);? 想持久化修改系統(tǒng)DNS服務(wù)器,配置/run/systemd/resolve/resolv.conf文件,并確保 /etc/resolv.conf 符號(hào)鏈接指向 /run/systemd/resolve/resolv.conf;? 想使用DNS Stub解析器,配置/run/systemd/resolve/resolv.conf文件,并確保 /etc/resolv.conf 符號(hào)鏈接指向 /run/systemd/resolve/stub-resolv.conf。在操作上,了解systemd-resolved是如何管理DNS請(qǐng)求,了解配置文件如何影響DNS解析行為,已經(jīng)足以完成日常的配置和故障排查。在理論上,若想要使用DNS解析服務(wù)更高級(jí)的功能可以深入了解DNS工作原理以及DNS協(xié)議的高級(jí)特性,并深入學(xué)習(xí)systemd-resolved服務(wù)相關(guān)的高級(jí)命令和安全防護(hù)等。
-
服務(wù)器
+關(guān)注
關(guān)注
12文章
9596瀏覽量
86970 -
域名
+關(guān)注
關(guān)注
0文章
75瀏覽量
11458 -
DNS
+關(guān)注
關(guān)注
0文章
221瀏覽量
20184
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
[討論]域名解析了,網(wǎng)站卻打不開的原因
華為mu509支持域名解析嗎?
mu203有域名解析功能嗎?
如何在 Linux 上從 NetworkManager 切換為 systemd
如何解決DNS解析錯(cuò)誤故障
使用JavaScript代碼在Rapid板子上實(shí)現(xiàn)DNS解析域名得到IP地址操作分享!
動(dòng)態(tài)域名解析、E-mail的工作原理
ip地址是從哪來(lái)的呢 DNS技術(shù)詳解

評(píng)論