28

My resolv.conf looks like this:

; generated by /sbin/dhclient-script
search mcdc
nameserver 10.0.4.48
nameserver 8.8.8.8

if I do nslookup www.google.com it works

nslookup www.google.com
;; Got SERVFAIL reply from 10.0.4.48, trying next server
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
www.google.com  canonical name = www.l.google.com.

but when I curl www.google.com, it cannot resolve the host.

I tried running curl under strace, and found curl was only using the first nameserver in resolv.conf, not the second. If I switch the two nameserver lines around, www.google.com resolves, but internal DNS names do not, so thats not a good workaround.

How can I fix resolv.conf to use both nameservers?

benhsu
  • 413
  • 1
  • 5
  • 7

6 Answers6

35

The default behavior for resolv.conf and the resolver is to try the servers in the order listed. The resolver will only try the next nameserver if the first nameserver times out. The resolv.conf manpage says:

nameserver Name server IP address

Internet address (in dot notation) of a name server that the resolver should query. Up to MAXNS (currently 3, see ) name servers may be listed, one per keyword. If there are multiple servers, the resolver library queries them in the order listed.

And:

(The algorithm used is to try a name server, and if the query times out, try the next, until out of name servers, then repeat trying all the name servers until a maximum number of retries are made.)

Also see the resolver(5) manual page for more information.

You can alter the resolver's behavior using rotate, which will query the Nameservers in a round-robin order:

rotate sets RES_ROTATE in _res.options, which causes round robin selection of nameservers from among those listed. This has the effect of spreading the query load among all listed servers, rather than having all clients try the first listed server first every time.

However, nslookup will use the second nameserver if it receives a SERVFAIL from the first nameserver. From the nslookup manpage:

[no]fail Try the next nameserver if a nameserver responds with SERVFAIL or a referral (nofail) or terminate query (fail) on such a response.

(Default = nofail)

Stefan Lasiewski
  • 22,949
  • 38
  • 129
  • 184
11

yes you could use "rotate" and timeout setting to improve DNS lookups, below is the example,

Ex:

[root@centos-xxxxxx ~]# cat /etc/resolv.conf
options rotate
options timeout:1
search xyz.abc.local
nameserver 192.168.56.3
nameserver 10.0.2.4
Harry
  • 111
  • 1
  • 2
  • 4
    For me, this sort of works, but not reliably. It basically relies on the client retrying until it gets the right name server, which sometimes works, sometimes doesn't. – Ben Aveling Apr 28 '21 at 12:00
6

So to make it work as expected install dnsmasq or other lightweight DNS repeater (or a full blown DNS server). See Comparison of DNS server software.

For dnsmasq configuration is as simple as:

server=10.0.4.48
server=8.8.8.8

You can also specify which DNS should be used for which domain . E.g.:

server=/mcdc/10.0.4.48
server=8.8.8.8

This will make dnsmasq look for *.mcdc in 10.0.4.48 DNS server and any other in 8.8.8.8.

In /etc/resolv.conf you just use your local DNS:

nameserver 127.0.0.1

For more details on dnsmasq setup see my answer here: https://unix.stackexchange.com/questions/55090/change-default-dns-on-openvpn-connect/545591#545591.

Nux
  • 541
  • 3
  • 12
  • 21
1

adding the below command in starting of resolv conf worked in ubuntu 18.04 LTS

options rotate
Uwe Keim
  • 2,370
  • 4
  • 29
  • 46
1

Is 10.0.4.48 a recursive dns server aka resolver?

Or is it only an authoritative server for your internal zones?

You should set up an internal resolver that might also be holding your authoritative data.

Michuelnik
  • 3,260
  • 3
  • 18
  • 24
-3

If you can, I would configure it in this fashion.

search mcdc
nameserver 127.0.0.1
nameserver 8.8.8.8

Blake
  • 122
  • 1
  • 5
  • 6
    Which part of the question indicates they run a DNS server on their machine, or want to? – Jay Jun 14 '12 at 16:57
  • 4
    [***WHY*** do you think querying localhost will improve their situation?](http://blog.serverfault.com/2011/06/09/press-the-green-button-twice/) – voretaq7 Jun 14 '12 at 17:17
  • 1
    Blake: Note that 127.0.0.1 is typically only used for systems which have a name server caching daemon on the local host. See http://tldp.org/HOWTO/DNS-HOWTO-3.html – Stefan Lasiewski Jun 14 '12 at 18:18