在尝试确定DNS查询何时超时时,我迷失了方向。尝试了多种场景(在Linux上):
###################### curl ####################### WRITE_OUT="%{http_code}\t%{time_namelookup}\t%{time_connect}\t\t%{time_starttransfer}\t\t%{time_total}\n“time curl -w "$WRITE_OUT”https://www.google.com/ 000 0.000 0.0000.000 0.000 curl:(6)无法解析主机: www.goole.com;未知错误real 0m0.009s用户0m0.000s sys 0m0.006s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m24.012s用户0m0.004s sys 0m0.009s
正如我们所看到的,curl立即返回(9ms),而nslookup需要更长的时间(24s)。这让我非常困惑,curl的行为更有意义,因为主机上没有指定名称服务器。
###################### curl ####################### time curl -k -w "$WRITE_OUT“https://www.google.com/ 000 0.000 0.000 0.000 19.529 curl:(6)无法解析主机: www.goole.com;未知错误real 0m20.535s用户0m0.003s sys 0m0.005s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m20.008s用户0m0.006s sys 0m0.003s
万岁!看起来curl和nslookup在同一个页面上。
###################### curl ####################### time curl -k -w "$WRITE_OUT“https://www.google.com/ 000 0.000 0.000 0.000 4.513 curl:(6)无法解析主机: www.goole.com;未知错误real 0m5.520s用户0m0.004s sys 0m0.005s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m20.010s用户0m0.006s sys 0m0.005s
又迷惑了!
最让人困惑的部分是,从resolv.conf的手册页面可以看出,timeout
的默认值是5秒,attempts
是2倍。所以我认为超时应该是5秒*2= 10秒。But...confusing...
编辑:重试修改/etc/nsswitch.conf
,仅使用dns
方法。hosts: dns
场景1:
###################### curl #######################
time curl -k -w "$WRITE_OUT" https://www.google.com/
000 0.000 0.000 0.000 0.000
curl: (6) Could not resolve host: www.google.com; Unknown error
real 0m0.051s
user 0m0.004s
sys 0m0.002s
##################### nslookup ####################
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real 0m24.287s
user 0m0.005s
sys 0m0.014s
######################## dig ######################
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real 0m18.041s
user 0m0.005s
sys 0m0.005s
场景2:
time curl -k -w "$WRITE_OUT" https://www.google.com/
000 0.000 0.000 0.000 19.527
curl: (6) Could not resolve host: www.google.com; Unknown error
real 0m20.533s
user 0m0.003s
sys 0m0.004s
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real 0m20.009s
user 0m0.005s
sys 0m0.005s
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real 0m15.008s
user 0m0.005s
sys 0m0.003s
场景3:
time curl -k -w "$WRITE_OUT" https://www.google.com/
000 0.000 0.000 0.000 4.512
curl: (6) Could not resolve host: www.google.com; Unknown error
real 0m5.518s
user 0m0.004s
sys 0m0.003s
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real 0m20.009s
user 0m0.005s
sys 0m0.005s
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real 0m15.009s
user 0m0.005s
sys 0m0.005s
dig
有自己的超时机制,超时(5s)*重试次数(3)= 15s。
发布于 2019-06-19 05:25:18
虽然这是一个古老的帖子,但我想插话,因为它不止一次出现在我身上,所以我想分享它。
需要指出的一个区别是应用程序(即nslookup或curl)用于DNS查找的是什么,即libresolv.so
或libbind.so
。似乎nslookup专门做后者,所以也许这就是为什么它比curl更快超时的原因。要确保在您的系统上查看,您应该运行
strace -o curl.out curl www.google.com
strace -o dig.out dig www.google.com
grep libresolv *.out
grep libbind *.out
并进行比较。
虽然有点神秘,但strace输出应该显示每个部分等待的时间,以及底层系统调用正在执行的工作。
发布于 2018-01-20 06:58:08
nslookup和类似的工具直接查询DNS,而curl首先检查本地/etc/hosts
,然后再查询DNS。因此,这可能是手头问题的一个线索。
我读到过nslookup已被弃用。你有权限去挖掘吗?
https://stackoverflow.com/questions/48333686
复制相似问题