title: What's the name lookup differences between curl and nslookup
body: <p>I'm lost when trying to figure out when a DNS query will be timed out. Tried multiple scenarios(on Linux):</p>
  
  <ol>
  <li><p>No name server configured in /etc/resolv.conf</p>
  
  <pre><code>###################### curl #######################
  WRITE_OUT="%{http_code}\t%{time_namelookup}\t%{time_connect}\t\t%{time_starttransfer}\t\t%{time_total}\n"
  
  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.goole.com; Unknown error
  
  real    0m0.009s
  user    0m0.000s
  sys     0m0.006s
  
  ##################### nslookup ####################
  time nslookup www.google.com
  ;; connection timed out; trying next origin
  ;; connection timed out; no servers could be reached
  
  real    0m24.012s
  user    0m0.004s
  sys     0m0.009s
  </code></pre>
  
  <p>As we can see, curl returns immediately(9ms), while nslookup takes much longer(24s). This makes me very confused, curl's behavior makes more sense as there is no name server specified on the host.</p></li>
  <li><p>Add an unreachable host IP in /etc/resolv.conf, cannot ping to simulate name server down scenario</p>
  
  <pre><code>###################### curl #######################
  time curl -k -w "$WRITE_OUT" https://www.google.com/
  000     0.000   0.000           0.000           19.529  
  curl: (6) Could not resolve host: www.goole.com; Unknown error
  
  real    0m20.535s
  user    0m0.003s
  sys     0m0.005s
  
  ##################### nslookup ####################
  time nslookup www.google.com
  ;; connection timed out; trying next origin
  ;; connection timed out; no servers could be reached
  
  real    0m20.008s
  user    0m0.006s
  sys     0m0.003s
  </code></pre>
  
  <p>Hurray! Looks like curl and nslookup on the same page.</p></li>
  <li><p>Add an host IP address which can be pinged, but without DNS services, to simulate server is alive but name server service is down</p>
  
  <pre><code>###################### curl #######################
  time curl -k -w "$WRITE_OUT" https://www.google.com/
  000     0.000   0.000           0.000           4.513
  curl: (6) Could not resolve host: www.goole.com; Unknown error
  
  real    0m5.520s
  user    0m0.004s
  sys     0m0.005s
  
  ##################### nslookup ####################
  time nslookup www.google.com
  ;; connection timed out; trying next origin
  ;; connection timed out; no servers could be reached
  
  
  real    0m20.010s
  user    0m0.006s
  sys     0m0.005s
  </code></pre>
  
  <p>Confused again!</p></li>
  </ol>
  
  <p>The most confusing part is, from the Manual page of <a href="http://man7.org/linux/man-pages/man5/resolv.conf.5.html" rel="nofollow noreferrer">resolv.conf</a>, we can that the default value of <code>timeout</code> is 5 seconds, and <code>attempts</code> is 2 times. So I suppose the timeout should be 5 seconds * 2 = 10 seconds. But...confusing...</p>
  
  <p>Edit:
  Tried again with modifying <code>/etc/nsswitch.conf</code>, only <code>dns</code> method is used. <code>hosts:      dns</code></p>
  
  <p>Scenario 1:</p>
  
  <pre><code>###################### 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
  
  ; &lt;&lt;&gt;&gt; DiG 9.9.4-RedHat-9.9.4-51.el7 &lt;&lt;&gt;&gt; www.google.com
  ;; global options: +cmd
  ;; connection timed out; no servers could be reached
  
  real    0m18.041s
  user    0m0.005s
  sys     0m0.005s
  </code></pre>
  
  <p>Scenario 2:</p>
  
  <pre><code>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
  ; &lt;&lt;&gt;&gt; DiG 9.9.4-RedHat-9.9.4-51.el7 &lt;&lt;&gt;&gt; www.google.com
  ;; global options: +cmd
  ;; connection timed out; no servers could be reached
  
  real    0m15.008s
  user    0m0.005s
  sys     0m0.003s
  </code></pre>
  
  <p>Scenario 3:</p>
  
  <pre><code>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
  
  ; &lt;&lt;&gt;&gt; DiG 9.9.4-RedHat-9.9.4-51.el7 &lt;&lt;&gt;&gt; www.google.com
  ;; global options: +cmd
  ;; connection timed out; no servers could be reached
  
  real    0m15.009s
  user    0m0.005s
  sys     0m0.005s
  </code></pre>
  
  <p><code>dig</code> has its own timeout mechanism, timeout(5s) * retries(3) = 15s.</p>