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.

0

2 Answers 2

14

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.

0
0

Asked 7 years, 5 months ago

Why is my /etc/hosts file not being read?

look at /etc/nsswitch.conf

this line

hosts:      files dns myhostname

if it doesn't have files there then it definitely will not be read; if files doesn't come first then whatever comes first will be used first and can give the impression that your /etc/hosts file is not being read because whatever was found in whatever else before it got to the hosts file.

my example was taken from RHEL 7.9. files almost always comes first, at least by default from a clean linux install. But this can and will be changed when linux is configured for whatever and wherever it is being used.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .