Skip to content

Commit

Permalink
nixos/networking: don't add extra names to ::1
Browse files Browse the repository at this point in the history
From hosts(5):

> For each host a single line should be present with the following
> information:
>
>               IP_address canonical_hostname [aliases...]

With lines like "::1 localhost ahost.adomin ahost", we were saying
that the canonical name for "ahost" was "localhost", the opposite of a
canonical name.  This is why a second loopback address (127.0.0.2) is
used for hostnames with IPv4 — if they were put after "localhost" on
the 127.0.0.1 line, the same thing would happen.  With IPv6 we can't
do the same thing as there's only a single loopback address, so
instead the right thing to do is to simply not list the hostnames in
/etc/hosts, and rely on the myhostname NSS plugin, which will handle
this correctly.

(Note that the examples in hosts(5) also do not include IPv6 FQDN or
hostname entries.)
  • Loading branch information
alyssais committed Dec 11, 2024
1 parent 4c43880 commit e9eff47
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 0 additions & 2 deletions nixos/modules/config/networking.nix
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ in
++ lib.optional (cfg.hostName != "") cfg.hostName; # Then the hostname (without the domain)
in {
"127.0.0.2" = hostnames;
} // lib.optionalAttrs cfg.enableIPv6 {
"::1" = hostnames;
};

networking.hostFiles = let
Expand Down
2 changes: 2 additions & 0 deletions nixos/tests/hostname.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ let
fqdn_and_host_name
== machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip()
)
assert "${fqdn}" == machine.succeed("getent hosts ${hostName} | awk '{print $2}'").strip()
'';
};

Expand Down

0 comments on commit e9eff47

Please sign in to comment.