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

In my linux machine, when I run hostname, it shows mongodb, but when I run host mongodb, it shows:

mongodb@mongodb:/var/hadoop/hadoop-1.2.1/bin$ host mongodb
Host mongodb not found: 2(SERVFAIL)

My /etc/hosts file:   mongodb   localhost       localhost
# localhost
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
  • My first question is: Since /etc/hosts is mapping from IP to hostname, why this machine cannot resolve the hostname mongodb to IP Instead, when I run host localhost, it can be resolved and shows:

    localhost has address
  • My another question: According to the /etc/hosts file, the hostname localhost should have been resolved to instead of loopback IP address Anybody can explain this to me?

share|improve this question

The host utility explicitly queries DNS servers, and does not consult the /etc/hosts file. If you were to traceroute or ping that address, you would see it correctly resolve.

You do not want to change localhost to map to anything other than, this can have strange and subtle effects on many things. I think that /etc/hosts is parsed in the order in which it is written, and since you have: localhost localhost

..the second entry may be overriding the first.

share|improve this answer
You answer really confused me.On one hand ,you said that /etc/hosts has no effect on the output of command host,but then you said the later line override the previous one in /etc/hosts.So , what on earch does the /etc/hosts affect the output of command 'host'? – Vico_Wu Jan 14 '14 at 8:11
/etc/hosts does not have any effect on DNS resolution when querying DNS servers directly, which is what host does. However, many system tools and utilities use the localhost definition, expecting it to be a loopback address, and use system name resolution (which looks at /etc/hosts before querying DNS) to resolve names rather than strictly going to DNS, which may not always be present. – DopeGhoti Jan 14 '14 at 17:16

In addition to DopeGhoti's answer, to test your resolving, inlcuding the /etc/hosts precedence, you can use the getent hosts <some_hostname> command.

share|improve this answer

I got similar issues with a /etc/hosts containing multiple spaces between IP and hostname, instead using a TAB. After changing to TAB the hostname could be resolved by ping.        test.local
         ^^^^^^^^ → Should be a TAB not multiple spaces.

see also on http://superuser.com/a/938366/467479

share|improve this answer
This surprises me. The manpage for hosts says: Fields of the entry are separated by any number of blanks and/or tab characters. Perhaps in your case there was some extraneous non-printable character between the IP and the hostname? – dr01 Jul 9 '15 at 12:54
There weren't definitively no non-printable characters. I checked this before I posted this here ;) – Thomas Lauria Sep 29 '15 at 8:46

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.