0

My /etc/hosts file looks like this:

# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
127.0.1.1 ansible-server ansible-server
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
ff02::3 ip6-allhosts

node1 0.0.0.0
node2 0.0.0.0

I have added the node1 and node2 and naturally the IP 0.0.0.0 is replaced by the IP of the node.

I would assume this works perfectly fine, however it doesn't. I thought SSH simply ignores the hosts file:

root@ansible-server:~# ssh root@node1
ssh: Could not resolve hostname node1: Name or service not known
root@ansible-server:~# ssh root@node2
ssh: Could not resolve hostname node2: Name or service not known

However, I can't ping these servers by their name either:

root@ansible-server:~# ping node1
ping: unknown host node1
root@ansible-server:~# ping node2
ping: unknown host node2

It is pretty clear I'm doing something really stupid here... but what?

Additional information: this server runs Ubuntu 14.04.2 LTS and is hosted on DigitalOcean. The server this is occurring on an Ansible server.

  • 11
    Put the address FIRST and the name (or optionally multiple names) SECOND. – dave_thompson_085 Aug 6 '15 at 8:35
  • 2
    @dave_thompson_085: IMCO you should convert that comment to an answer... (look how many upvotes you're loosing to a comment) – Fabby Aug 6 '15 at 8:38
  • @Fabby Please do because I did what you said and I quickly realised how stupid I was. – William Edwards Aug 6 '15 at 8:39
  • 1
    @dave_thompson_085 8 upvoted wasted >:=D (and counting) – Rinzwind Aug 6 '15 at 8:44
  • @Archemar nslookup and dig (and host) only talk to the DNS server(s), they never use the hosts file even when normal name resolution does. serverfault.com/questions/303716/… – dave_thompson_085 Aug 6 '15 at 8:53
10

Since @William still got it wrong (!) here we go:

The format of lines in /etc/hosts is address first and name(s) second

0.0.0.0 node1 
0.0.0.0 node2 
192.168.1.1 myroutermaybe
8.8.8.8 googledns # in case DNS doesn't work for DNS???
127.0.0.1 localhost 

or where several names map to the same address

0.0.0.0 node1 node2 node3 stitch626 

ADDED, thanks to reminder by fpmurphy1:

The first name (if more than one) is used as the canonical or "official" name for gethostbyaddr etc, so if you have a domain name assigned to this machine/address it is usually clearest and most useful to put the Fully Qualified Domain Name (FQDN) as the first name.

  • Actually the order is ip address followed by canonical (FQDN) name followed by zero or more aliases – fpmurphy Feb 5 '16 at 4:56
  • @fpmurphy1 not always FQDN, but you're right it's worth noting; edited – dave_thompson_085 Feb 7 '16 at 21:55

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.