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:

192.168.10.10   mongodb
192.168.10.10   localhost
127.0.0.1       localhost
#127.0.0.1 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 192.168.10.10? Instead, when I run host localhost, it can be resolved and shows:

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

share|improve this question
add comment

2 Answers

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 127.0.0.1, 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:

192.168.10.10 localhost 127.0.0.1 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 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 at 17:16
add comment

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
add comment

Your Answer

 
discard

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.