Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to test in a script whether a hostname resolves to something other than as a pre-flight test for an install script. The tools I know (dig, nslookup) specifically query DNS. In my situation a correct entry in /etc/hosts is a valid configuration. So the question is:

Is there a dig-like tool, ideally already on the system, that does the full name-resolution process via nsswitch.conf, like (for example) ping does?

dig my-test-server-that-only-exists-in-etc-hosts 

returns NXDOMAIN.

share|improve this question
up vote 1 down vote accepted

This already has an answer on SU:

Bash: lookup an IP for a host name, including /etc/hosts in search

Basically, you use

getent ahosts host_name

where host_name can either be an entry in your /etc/hosts, in which case it will resolve to that, or a host that your DNS can resolve.

Quoting the getent(1) man page:

When no key is provided, use sethostent(3), gethostent(3), and endhostent(3) to enumerate the hosts database. This is identical to using hosts. When one or more key argu‐ ments are provided, pass each key in succession to getad‐ drinfo(3) with the address family AF_UNSPEC, enumerating each socket address structure returned

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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