Add myhostname to nsswitch.conf to ensure resolvable hostname #686
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #148 and discourse.roots.io/t/deploy-fails-at-ssmtp/6281.
The problem
New servers occasionally have an unresolvable hostname, causing the ssmtp role to fail with the message
hostname: Name or service not known
. Affected servers will produce this same message in response to a command checking the FQDN.An explanation
The Debian hostname resolver consults the
hosts
parameter of/etc/nsswitch.conf
for where to look for hostname information (ref). The parameter value is typicallyfiles dns
. Thefiles
checked are/etc/hostname
and/etc/hosts
./etc/hosts
The hostname is resolvable if
/etc/hosts
has a line with something fqdn-like, e.g.:Some new servers don't have such a line in
/etc/hosts
(e.g., DreamCompute servers in the discourse thread mentioned above)./etc/hostname
New servers typically do have a name in the other file:
/etc/hostname
. If this name is fqdn-like (name.xyx
), the resolver may use this name forhostname --fqdn
but not if the name is merelyshortname
. In this latter case ofshortname
, thefiles
consulted do not resolve the hostname fqdn and perhaps neither does thedns
consulted, resulting inhostname: Name or service not known
.Digital Ocean and DreamCompute put a name in
/etc/hostname
based on the user-defined server name. For example, if the user assigned the server the the namehostname.domain.com
, then/etc/hostname
would contain justhostname
. This is not fqdn-like, so DreamCompute has the unresolvable hostname problem.myhostname
Even if
/etc/hostname
has onlyshortname
, Digital Ocean manages to still resolve the hostname on Ubuntu images by addingmyhostname
to thehosts
parameter in/etc/nsswitch.conf
.This
myhostname
relies on the presence of "nss-myhostname
[which] is a plugin for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc), primarily providing hostname resolution for the locally configured system hostname" (manpage). Continuing,A solution
This PR follows Digital Ocean's lead by ensuring that
libnss-myhostname
is installed (Debian'snss-myhostname
) and that thehosts
parameter in/etc/nsswitch.conf
containsmyhostname
.The proposed
lineinfile
task checks thehosts: ...
line in/etc/nsswitch.conf
and appendsmyhostname
to the end of the line if missing. The/etc/nsswitch.conf
file itself is unlikely to be missing (would cause the task to fail) because it is part of the base-files.Users running into the
hostname: Name or service not known
for the ssmtp role will have to apply the changes of this PR then probably either start with a fresh server (recommended) or runsudo apt-get purge ssmtp
on the affected server and retry theserver.yml
playbook.Some suggest that instead of using
nss-myhostname
one should just properly set up/etc/hosts
and/etc/hostname
. Trellis could certainly template the/etc/hosts
file and set the hostname in/etc/hostname
, but I figured we would prefer not to handle/etc/hosts
. Our problem is exactly whatnss-myhostname
is meant to resolve.AWS EC2 hostname
#148 reported the unresolvable hostname when launching an EC2 instance to an AWS VPC. I think this problem is more likely to occur when launching an instance to a custom VPC instead of to an account's default VPC.
When creating a custom VPC, there is a yes/no option to "Enable DNS hostname." It currently defaults to "yes", but if set to "no", instances launched to the VPC will have the unresolvable hostname problem. This PR successfully prevents the problem.
DNS resolution
As a debugging note for posterity, when I created a VPC with "Enable DNS hostname" set to "no", an instance launched to the VPC exhibited the confounding Ansible error
Timeout (12s) waiting for privilege escalation prompt
. I was able to resolve the error by navigating to the AWS VPC console, selecting the affected VPC, choosing the "Action" of "Edit DNS Resolution" and setting "DNS Resolution" to "yes".DNS hostnames
The unresolvable hostname fqdn problem solved by this PR could also be resolved for the VPC by selecting the VPC and choosing the "Action" of "Edit DNS Hostnames" and setting "DNS Hostnames" to "yes". If set to "no", a fix like this PR will be required for the hostname fqdn to be resolvable.