-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nixos/networking: don't add hostname / FQDN entires to /etc/hosts
#380987
base: master
Are you sure you want to change the base?
Conversation
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's questionable whether this line should remain. The only reason why this works is that the testing framework's base configuration includes an extraHosts
setting that includes this machine's FQDN:
nixpkgs/nixos/lib/testing/network.nix
Lines 89 to 110 in c086623
# Put the IP addresses of all VMs in this machine's | |
# /etc/hosts file. If a machine has multiple | |
# interfaces, use the IP address corresponding to | |
# the first interface (i.e. the first network in its | |
# virtualisation.vlans option). | |
networking.extraHosts = flip concatMapStrings (attrNames nodes) ( | |
m': | |
let | |
config = nodes.${m'}; | |
hostnames = | |
optionalString ( | |
config.networking.domain != null | |
) "${config.networking.hostName}.${config.networking.domain} " | |
+ "${config.networking.hostName}\n"; | |
in | |
optionalString ( | |
config.networking.primaryIPAddress != "" | |
) "${config.networking.primaryIPAddress} ${hostnames}" | |
+ optionalString (config.networking.primaryIPv6Address != "") ( | |
"${config.networking.primaryIPv6Address} ${hostnames}" | |
) | |
); |
So this is just testing the test framework itself, and not really the network configuration for regular hosts.
This adds a test that ensures that hosts can resolve their own v4 and v6 addresses as encoded in A and AAAA records of their upstream DNS server or resolver respectively. This test fails on this commit with a regular NixOS install, which contains an `/etc/hosts` mapping for FQDN to 127.0.0.2, but no such mapping for an IPv6 loopback IP (as there are no multiple loopback v6 IPs, and each address must only be listed once [1]). While for NixOS test configurations this test passes, this is only because the test networking module adds additional entries to `networking.extraHosts` which are not present in a regular NixOS configuration. [1]: NixOS#362132
PR NixOS#362132 [1] removed multiple `/etc/hosts` entires mapping to the same IPv6 loopback address (`::1`). However, a mapping from a machine's hostname and FQDN still exists for a second IPv4 loopback address (`127.0.0.2`). This causes issues when using systemd's resolved: when any `/etc/hosts` entry exists for a domain name being queried, systemd-resolved only returns entires from the `/etc/hosts` database, and no other records that can be obtained from DNS. In practice, this means that a host trying to resolve its own FQDN (which is further configured in the system's NixOS configuration through the `networking.hostName` and `networking.domain` settings) will only return its IPv4 loopback IP (`127.0.0.2`), but _no_ IPv6 address, regardless of whether a AAAA-record exists. [root@myserver:~]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost 127.0.0.2 myserver.example.org [root@myserver:~]# resolvectl query myserver.example.org myserver.example.org: 127.0.0.2 -- Information acquired via protocol DNS in 6.1ms. -- Data is authenticated: yes; Data was acquired via local or encrypted transport: yes -- Data from: synthetic This pull request removes such default hostname or FQDN hosts-entries entirely. This is similar to the default configurations on AlmaLinux or Fedora systems: root@myserver:~# cat /etc/hosts # Loopback entries; do not change. # For historical reasons, localhost precedes localhost.localdomain: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 # See hosts(5) for proper format and other examples: # 192.168.1.10 foo.example.org foo # 192.168.1.13 bar.example.org bar It does not prevent users from defining any custom such entires for the machine's public IPs, similar to the comment in the file above. We don't have a good idea to determine what the proper public IPv4 and IPv6 addresses are for a machine's FQDN, and as such this decision and responsibility should be on the user. [1]: NixOS#362132
b434288
to
ac9cc5a
Compare
I've been trying out this change, and the first thing I've noticed is that the following test, that I've been using to check that applications are able to look up their FQDN, now only passes because of the default nixosTest {
name = "getaddrinfo";
nodes.machine = {
environment.systemPackages = [ pkgs.python3 ];
networking.hostName = "ahost";
networking.domain = "adomain";
# virtualisation.vlans = [];
services.resolved.enable = true;
};
testScript = ''
machine.wait_for_unit('nss-lookup.target')
machine.succeed("""python3 -c '
import socket
for (_, _, _, name, _) in socket.getaddrinfo(socket.gethostname(), None, family=socket.AF_INET, proto=socket.IPPROTO_TCP, flags=socket.AI_CANONNAME):
if name:
exit(name != \"ahost.adomain\")
exit(2)
' >&2""")
'';
} If I uncomment the |
I wouldn't go as far as to say it's expected, but it makes sense given the way the config and modules work right now. Options walkthroughThe nixpkgs/nixos/modules/virtualisation/qemu-vm.nix Lines 717 to 729 in b4744ad
It also drives the nixpkgs/nixos/modules/virtualisation/qemu-vm.nix Lines 616 to 618 in a45fa36
That's then picked up by the testing framework's network config and converted into a set of nixpkgs/nixos/lib/testing/network.nix Lines 35 to 63 in b4744ad
Additionally, this let binding is further used to then drive the nixpkgs/nixos/lib/testing/network.nix Lines 81 to 87 in b4744ad
And, finally, that's used to create an nixpkgs/nixos/lib/testing/network.nix Lines 94 to 110 in b4744ad
We thus end up with an explicit nix-repl> nixosTests.alyssais-network.nodes.machine.virtualisation.vlans
[ 1 ]
nix-repl> nixosTests.alyssais-network.nodes.machine.networking.extraHosts
"192.168.1.1 ahost.adomain ahost\n2001:db8:1::1 ahost.adomain ahost\n\n" ...but no such entries when nix-repl> nixosTests.alyssais-network.nodes.machine.virtualisation.vlans
[ ]
nix-repl> nixosTests.alyssais-network.nodes.machine.networking.extraHosts
"\n" Now, for me there's two major takeaways from this:
The changes of this PR themselves work as expected: a machine will be unable to resolve its own FQDN when neither an explicit hosts entry, nor appropriate DNS records exists. Whether what the |
@alyssais Put in a different way, with this change your test would always be expected to fail for a regular (non-test) NixOS system that does not have explicit |
Okay, as long as it works when there are DNS entries that seems fine to me. |
I'm increasingly convinced that adding But that seems separate from what this PR is trying to do, and can easily be achieved by just setting |
😞 Double checked, it does not.
whereas
If you want This behavior is identical to that of a fresh, default installation of Fedora 41 Server Edition, with the hostname specified in the installer and public DNS records set up:
|
Yikes. That makes me a lot less confident this is the right thing. I wonder if we should ask the resolved developers how they expect this all to work…? |
PR #362132 1 removed multiple
/etc/hosts
entires mapping to the same IPv6 loopback address (::1
). However, a mapping from a machine's hostname and FQDN still exists for a second IPv4 loopback address (127.0.0.2
).This causes issues when using systemd's resolved: when any
/etc/hosts
entry exists for a domain name being queried, systemd-resolved only returns entires from the/etc/hosts
database, and no other records that can be obtained from DNS. In practice, this means that a host trying to resolve its own FQDN (which is further configured in the system's NixOS configuration through thenetworking.hostName
andnetworking.domain
settings) will only return its IPv4 loopback IP (127.0.0.2
), but no IPv6 address, regardless of whether a AAAA-record exists.This pull request removes such default hostname or FQDN hosts-entries entirely. This is similar to the default configurations on AlmaLinux or Fedora systems:
It does not prevent users from defining any custom such entires for the machine's public IPs, similar to the comment in the file above. We don't have a good idea to determine what the proper public IPv4 and IPv6 addresses are for a machine's FQDN, and as such this decision and responsibility should be on the user.
In a separate commit, this PR adds a test that ensures that hosts can resolve their own v4 and v6 addresses as encoded in A and AAAA records of their upstream DNS server or resolver respectively.
This test fails prior to this PR with a regular NixOS install, which contains an
/etc/hosts
mapping for FQDN to 127.0.0.2, but no such mapping for an IPv6 loopback IP (as there are no multiple loopback v6 IPs, and each address must only be listed once 1). While, for NixOS test configurations, this test passes, this is only because the test networking module adds additional entries tonetworking.extraHosts
which are not present in a regular NixOS configuration.Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Add a 👍 reaction to pull requests you find important.
Ping @alyssais @ElvishJerricco @primeos @blitz