Linux

使用動態 IP 地址時使用 /etc/hosts 繞過 DNS 伺服器查找

  • July 5, 2013

據我了解,如果 /etc/hosts 中存在這些常用名稱,則可以減少查詢外部 DNS 伺服器以獲取常用名稱的需求。

現在,我有一個帶有動態 IP 地址的嵌入式 Linux 機器。假設這個動態 IP 地址目前是 206.190.36.105。

這是我的 /etc/hosts 文件的內容:

[root@zop]# cat /etc/hosts
127.0.0.1       localhost
192.168.0.1     mydevice
173.194.33.18   somesite.com

但是,當我執行 tcpdump 並 ping somesite.com 時,我仍然看到 somesite.com 正在通過 DNS 查找進行解析。

17:28:48.330535 IP 206.190.36.105 > somesite.com: ICMP echo request, id 14880, seq 0, length 64
17:28:48.333465 IP 206.190.36.105.57201 > resolver1.opendns.com.domain: 2+ PTR? 204.220.167.10.in-addr.arpa. (45)
17:28:49.312286 IP somesite.com > 206.190.36.105: ICMP echo reply, id 14880, seq 0, length 64
17:28:49.335601 IP 206.190.36.105 > somesite.com: ICMP echo request, id 14880, seq 1, length 64
17:28:49.366973 IP resolver1.opendns.com.domain > 206.190.36.105.57201: 2* 0/1/0 (104)
17:28:49.368286 IP 206.190.36.105.59381 > resolver1.opendns.com.domain: 3+ PTR? 204.220.167.10.in-addr.arpa. (45)
17:28:49.664215 IP somesite.com > 206.190.36.105: ICMP echo reply, id 14880, seq 1, length 64
17:28:49.742004 IP resolver1.opendns.com.domain > 206.190.36.105.59381: 3* 0/1/0 (104)
17:28:49.743194 IP 206.190.36.105.57388 > resolver1.opendns.com.domain: 4+ PTR? 204.220.167.10.in-addr.arpa. (45)
17:28:50.038848 IP resolver1.opendns.com.domain > 206.190.36.105.57388: 4* 0/1/0 (104)
17:28:50.040069 IP 206.190.36.105.53513 > resolver1.opendns.com.domain: 5+ PTR? 204.220.167.10.in-addr.arpa. (45)
17:28:50.335815 IP resolver1.opendns.com.domain > 206.190.36.105.53513: 5* 0/1/0 (104)
17:28:50.337036 IP 206.190.36.105.54248 > resolver1.opendns.com.domain: 6+ PTR? 204.220.167.10.in-addr.arpa. (45)

如果我在 /etc/hosts 中為 Linux 框的目前 IP 地址創建一個條目,如下所示:

[root@zop]# cat /etc/hosts
127.0.0.1       localhost
192.168.0.101   mydevice
173.194.33.18   somesite.com
206.190.36.105   whatismyip

然後 tcpdump 與對 somesite.com 的 ping 一起顯示,現在繞過了 DNS 查找

17:15:35.795013 IP whatismyip > somesite.com: ICMP echo request, id 61212, seq 0, length 64
17:15:36.648193 IP somesite.com > whatismyip: ICMP echo reply, id 61212, seq 0, length 64
17:15:36.809234 IP whatismyip > somesite.com: ICMP echo request, id 61212, seq 1, length 64
17:15:37.164276 IP somesite.com > whatismyip: ICMP echo reply, id 61212, seq 1, length 64
17:15:37.819915 IP whatismyip > somesite.com: ICMP echo request, id 61212, seq 2, length 64
17:15:38.148193 IP somesite.com > whatismyip: ICMP echo reply, id 61212, seq 2, length 64
17:15:38.827728 IP whatismyip > somesite.com: ICMP echo request, id 61212, seq 3, length 64

我有興趣了解這種觀察到的行為背後的基本原理。嵌入式 Linux 供應商聲稱這種行為是正常和預期的行為 - 但理性地說,如果只有目標 IP 地址不在 /etc/hosts 文件中,難道不應該繞過 DNS 查找嗎?

我認為您將正向 DNS 查找與反向 DNS 查找混淆了。

正向 DNS 查找從名稱到 IP 地址。如果您查看第一個 tcpdump 中的 DNS 數據包,您會看到PTR?(指針請求),這是一個將 IP 轉換為名稱的請求。

z.y.x.w.in-addr.arpa是以反向查找表示法請求的 IP。如果你顛倒這個順序,你會得到 wxyz,它試圖查找的 IP 地址。

我懷疑tcpdump是反向查找請求的來源,而不是ping,因為它不需要對您的 IP 執行反向查找。當您將 IP 添加到 時/etc/hoststcpdump不再需要對您的 IP 執行反向查找,因為您的解析器庫可以在不執行 DNS 查詢的情況下找到它。

tcpdump使用該-n選項執行以避免這些查找通常是一個好主意。它們通常不是必需的。

引用自:https://serverfault.com/questions/520710