Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

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

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.