Is it possible to list multiple nameservers in /etc/resolv.conf on a Unix system and resolve dns queries using a random nameserver when making a dns query and not just the first one?
For Linux systems, I use the
rotate options in
/etc/resolv.conf... I usually lower the DNS timeout to 1 second.
timeout:n sets the amount of time the resolver will wait for a response from a remote name server before retrying the query via a different name server. Measured in seconds, the default is RES_TIMEOUT (currently 5, see <resolv.h>). The maximum value for this option is silently capped to 30.
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.
There are different kinds of randomization or pseudo-randomization of DNS resolving on Linux.
With libc resolver and
"options rotate" is a kind a client-side round-robin and as such can be considered as a poor-man randomization. However, libc resolver supports at most 3 different servers. Here is a sample
/etc/resolv.conf that uses 3 popular and reliable public DNS servers.
options timeout:1 options rotate # resolver1.level3.net nameserver 188.8.131.52 # resolver1.opendns.com nameserver 184.108.40.206 # google-public-dns-a.google.com nameserver 220.127.116.11
With dnsmasq, one can use more than 3 forward name servers.
The server is chosen not purely randomly, but it is indeed pseudo-randomly changed every 50 queries or 10 seconds have elapsed. This can be checked by logging queries (
# dns1.alternate-dns.com nameserver 18.104.22.168 # resolver1.dyndnsinternetguide.com nameserver 22.214.171.124 # dns1.safedns.com nameserver 126.96.36.199 # resolver1.level3.net nameserver 188.8.131.52 # ns1.recursive.dnsbycomodo.com nameserver 184.108.40.206 # ordns.he.net nameserver 220.127.116.11 # dns.yandex.ru nameserver 18.104.22.168 # resolver1.opendns.com nameserver 22.214.171.124 # google-public-dns-a.google.com nameserver 126.96.36.199 # rdns1.ultradns.net nameserver 188.8.131.52
With unbound, one can also specify multiple resolvers (more than 3). The randomization is documented as follows:
The fastest server (randomly picked within a so-called RTT band of 400 msec) is selected when a query has to be sent out
forward-zone: name: "." # google #forward-addr: 184.108.40.206 # fnd.org forward-addr: 220.127.116.11 # comodo forward-addr: 18.104.22.168 # level 3 forward-addr: 22.214.171.124
Short answer: No, it's not.
Longer answer: While nearly all Unix variants use the file /etc/resolv.conf for global name resolution configuration, nowhere close to all of them actually use the same resolver library to do the job. It may be that your particular variant of Unix can do what you want, but to find that out you'll have to read its own documentation. Also, it will not be portable at all.