I have a server with search example.com in resolv.conf, and it works correctly for DNS lookups. That is, if I ping host, and host1.example.com is in DNS, it is found.

But if host1.example.com is in /etc/hosts instead of DNS, it is not found. I assume the entry in resolv.conf only applies to DNS.

Is there a way to make a domain search path that works for /etc/hosts entries, and if not, why not?

  • You could have aliases in the hosts file, e.g. the entry host1.example.com host1. – Torin Apr 5 '18 at 17:09
  • You write ”if I ping host […]“. Don't you mean host1? – myrdd May 15 '19 at 16:38

The simple and generally used method is to include both host1 and host1.example.com to /etc/hosts.

However, you can reach your goal using dnsmasq. dnsmasq will read your hosts file (configurable,this is the default), you just have to set your original ns as upstream in dnsmasq and localhost as ns in resolv.conf and you can keep your search option. You will get the added benefit of locally cached name service.

This is the most basic (probably server) setup, if you are using resolvconf like ns manager you have to configure that instead.

It's worth to keep in mind, if you make changes in /etc/hosts you must restart dnsmasq.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.