≡ Menu

Linux: DNS Search List in resolv.conf

Quesetion – I want to setup a search list for my dns resolution. For example, I want all hostnames without a dot to be appended by my domain name e.g. if I do a lookup on mail, I want the resolver to search for mail.mydomain.com. Is there a way to do this quickly and easily?

Yes! There is a search option that allows you to append domain names to host names.

For example, see the following shell session:

[root@server ~]# host mail
Host mail not found: 3(NXDOMAIN)
[root@server ~]# cat /etc/resolv.conf
nameserver 127.0.0.1
nameserver 199.167.198.1
nameserver 4.2.2.1
search freednsreport.net
[root@server ~]# host mail
mail.freednsreport.net mail is handled by 0 honeypot.bordeaux.35.com.
[root@server ~]#

When I did not have the search directive in /etc/resolv.conf, I got a domain name not found (NXDOMAIN). After I added the following line, it was able to resolve the host name!

search freednsreport.net

So, if you want mydomain.com appended to each query, just add the following line to your /etc/resolv.conf file:

search mydomain.com

You can add six search paths in your file. The DNS resolver will search for a valid host name by appending each of the six search domain names and stop when a match is found.

Now, there is also another directive that can be used – the ndots directive. The default value of ndots is 1 which means that any host name having fewer than ndots dots will be appended by the search paths. If the host name being resolved has a dot, it will be resolved in iteself first, before appending the search domain names.

So, settings ndots to 2 will try any host name with 2 dots in itself and if the host name does not resolve, it will append the search list and try again. Keeping ndots to the default value of 1 is the most sensible thing to do.

options ndots:2
search freednsreport.net

For more information on the resolv.conf file, please refer to its man page:

man resolv.conf

Comments on this entry are closed.