-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
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: Add the FQDN and hostname to /etc/hosts #76542
Conversation
I've tested/verified this with the following (very hacky) C program: hostname-test.c Result: [root@nixos-unstable:~]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost
127.0.0.1 nixos-unstable.test.tld nixos-unstable
::1 nixos-unstable.test.tld nixos-unstable
[root@nixos-unstable:~]# ./hostname-test
Hostname (IPv4): nixos-unstable
- Canonical name: nixos-unstable.test.tld
- Aliases: nixos-unstable nixos-unstable
Hostname (IPv6): nixos-unstable
- Canonical name: nixos-unstable.test.tld
- Aliases: nixos-unstable
Address: 127.0.0.1
- Canonical name: localhost
- Aliases:
Address: ::1
- Canonical name: localhost
- Aliases:
[root@nixos-unstable:~]# hostname -f
nixos-unstable.test.tld Note: With [root@nixos-unstable:~]# cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 nixos-unstable.test.tld nixos-unstable
[root@nixos-unstable:~]# ./hostname-test
Hostname (IPv4): nixos-unstable
- Canonical name: nixos-unstable.test.tld
- Aliases: nixos-unstable
Hostname (IPv6): nixos-unstable
- Canonical name: nixos-unstable
- Aliases: localhost
Address: 127.0.0.1
- Canonical name: localhost
- Aliases:
Address: ::1
- Canonical name: localhost
- Aliases: nixos-unstable
[root@nixos-unstable:~]# hostname -f
nixos-unstable But that could be fixed by adding the entry for |
We should really look at other distros to see what they are doing. Eg: Ubuntu 18.04 cloud image's
It looks like they don't even map the hostname. There are so many use-cases that it's hard to make it work for all of them.
/cc @globin and @fpletz who know much more about networking. |
/cc @oxij |
This may also solve the problem with kerberos: #10183 |
If I get this right, it's just nss being broken, and it'd preferrable to have these records provided by nss-myhostname, as discussed in #10183? |
a0a0ab5
to
5b7cb6d
Compare
I've rebased this PR to resolve the merge conflict.
@flokli could you explain that a bit more? I've only had a quick look at #10183 but that looks to me like the changes to Also: It shouldn't be possible to fix this via $ cat /etc/nsswitch.conf | grep hosts
hosts: files mymachines dns myhostname Personally I think that this approach is the best way to go and it should (almost) always work (of course there are corner cases if users make changes that negate this PR). I'd really like to finally resolve this long-standing issue. If someone noticed a bug/regression with this PR I'm happy to take a look and try to find a solution. If someone doesn't like this approach than please make your own PR so that we can properly discuss this. Thanks. Edit:
|
@primeos Sorry, I mistakenly assumed Can you cherry-pick blitz@efd537c into this PR (and re-enable the fqdn-specific test)? That way, we could tinker around with the PR a bit, and easier spot breakages. |
Done
I'm also sorry, I was pretty angry (not at you but this PR cost me multiple hours to research and test and this topic is IMO pretty annoying to deal with as every distribution does it's own thing and there isn't good documentation for this) and replied a bit too "soon". I decided to look into |
@GrahamcOfBorg test hostname 2 |
currently staging fails to build due to #86954 (comment). I poked in that issue. Anyways, as this is mostly a module update, and not a world rebuild, it can probably target |
@zimbatm do you rely on the FQDN being mapped to the public IP address or would
@flokli I also think that |
We can't know all "public IP adresses" at configuration time anyways, as they can also be provided dynamically via DHCP or router advertisements. Mapping hostname and fqdn to In NixOS, we already do have However, I still think keeping |
261ce95
to
9b323c3
Compare
@flokli agreed, this was only about potential regressions, I didn't intend to implement something for this use case. I've also documented this change in the changelog and squashed the commits. From my point of view this should be ready now. |
dee70d0
to
7195c84
Compare
My 2 cents since I'm now rebasing my network on top of this change: it's very common to have to refer to the machine's FQDN in various config locations. Previously I was able to just pull in The release notes also don't really mention how to migrate configs to comply to the new requirements (e.g. the rlnotes item doesn't even list |
Good point. Maybe we should add an option like
Right, seems like we forgot this in the second item. I can open a followup PR (probably in a few days, if no one else does this before then). |
Yeah, Regarding the release notes, the paragraph before that already explicitly mentions |
Good question. I initially thought the hostname and optionally (i.e. if set) the domain. Thinking more about it I would set it to |
Yeah, I'd personally prefer Looking further to nixpkgs, and how |
I'd |
It looks like the regex does not allow to set the |
This fixes a regression from 993baa5 which requires networking.hostName to be a valid DNS label [0]. Unfortunately we missed the fact that the hostnames may also be empty, if the user wants to obtain it from a DHCP server. This is even required by a few modules/images (e.g. Amazon EC2, Azure, and Google Compute). [0]: NixOS#76542 (comment)
@zhenyavinogradov thanks, I missed that case :o I've opened #89407 to fix this. Note to self: I should also try to add a test case for empty hostnames (I'll do that together with the documentation improvements and read-only |
One more issue I noticed: postfix seems to rely on gethostname() returning a fqdn, and now doesn't infer the correct myhostname/mydomain anymore:
This is documented in https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=214741 where it seems like at some point Debian was carrying a custom patch. I can't find that patch in their current version though... wonder if it got lost or something. https://git.launchpad.net/postfix/commit/?id=ebe0c0010f461ed5986a09ea686f191d601a4fd7 seems like the patch they were carrying in the past. |
@delroth after this update, my mail works fine. I use the parameter
Checked with this patch,
Is this fix needing? |
If it's a patch debian still applies, we can definitely If debian doesn't apply this anymore, we should research why they don't anymore - maybe it already landed in upstream one way or another, and we only need to bump the package. |
It's been such a long time – I'm puzzled why it hasn't been addressed upstream. |
I wonder if we could first deprecate and then break? 993baa5 is a bit heavy-handed, and might be a major breaking change for an org. |
Deprecate... meaning to throw an eval-time warning that some stuff won't work correctly? (when hostname contains dots) EDIT: I hope not something crazy like |
Not sure if it's really that much of a problem considering our other braking changes and that it should be pretty easy to find and revert that commit if necessary. But then again I'm also a bit concerned as some applications might still make wrong assumptions on Linux (e.g. Bash, ZSH, and possibly Postfix where mentioned here). And technically there aren't any strict requirements for the hostname apart from the maximum length, which is probably the biggest problem. If we deem it necessary/useful I wouldn't mind turning the evaluation error into a warning (actually I don't really have a preference here). |
I kinda agree with @primeos here. It's in the release notes, it produces a meaningful error during configuration, and most people set the fqdn in their postfix/apache/… configurations too anyways. |
Since NixOS#76542 this workaround is required to use a FQDN as hostname. See NixOS#94011 and NixOS#94022 for the related discussion. Due to some potential/unresolved issues (legacy software, backward compatibility, etc.) we're documenting this workaround [0]. [0]: NixOS#94011 (comment)
Since #76542 this workaround is required to use a FQDN as hostname. See #94011 and #94022 for the related discussion. Due to some potential/unresolved issues (legacy software, backward compatibility, etc.) we're documenting this workaround [0]. [0]: #94011 (comment)
Since NixOS#76542 this workaround is required to use a FQDN as hostname. See NixOS#94011 and NixOS#94022 for the related discussion. Due to some potential/unresolved issues (legacy software, backward compatibility, etc.) we're documenting this workaround [0]. [0]: NixOS#94011 (comment) (cherry picked from commit 4a600af)
…esolved See details in comment, I think this would fix NixOS#132646. See NixOS#76542 for more history.
This fixes the output of "hostname --fqdn" (previously the domain name
was not appended). Additionally it's now possible to use the FQDN.
This works by unconditionally adding two entries to /etc/hosts:
127.0.0.1 localhost
::1 localhost
These are the first two entries and therefore gethostbyaddr() will
always resolve "127.0.0.1" and "::1" to "localhost".
This works because nscd (or rather the nss-files module) returns the
first matching row from /etc/hosts (and ignores the rest).
The FQDN and hostname entries are appended later to /etc/hosts, e.g.:
127.0.0.2 nixos-unstable.test.tld nixos-unstable
::1 nixos-unstable.test.tld nixos-unstable
Note: We use 127.0.0.2 here to follow nss-myhostname (systemd) as close
as possible. This has the advantage that 127.0.0.2 can be resolved back
to the FQDN but also the drawback that applications that only listen to
127.0.0.1 (and not additionally ::1) cannot be reached via the FQDN.
If you would like this to work you can use the following configuration:
Therefore gethostbyname() resolves "nixos-unstable" to the FQDN
(canonical name): "nixos-unstable.test.tld".
Advantages to the previous behaviour:
Drawbacks:
$FQDN
should resolve to the public IP address instead of127.0.0.1
)$HOSTNAME
(without the domain part) that resolves to127.0.1.1
(!=127.0.0.1
).care was taken).
Optional TODOs:
nixos/tests/hostname
: Also check that127.0.0.1
and::1
still resolve back tolocalhost
as this is apparently required by some applications (see c578924)networking.hostName
contains a dot: fc7a9b8/etc/hostname
)Future improvements:
nss-myhostname
(part of systemd) but unfortunately it only returns the hostname without the domain partgetent hosts 127.0.0.2
nss-myhostname
might be replaced bynss-resolved
soon (references: https://github.com/systemd/systemd/pull/6231/files, Document intersection of nss-myhostname and nss-resolve systemd/systemd#1605 (comment), https://github.com/systemd/systemd/blob/v245/TODO#L537) - but: we don't usenss-resolved
yetnss-myhostname
would require patchingsystemd
: src/basic/hostname-util.c#L43 or maybe better onlygethostname_malloc()
insrc/nss-myhostname/nss-myhostname.c
uname --nodename
corresponds tosysctl kernel.hostname
which we could change but it shouldn't contain the domain part admin-guide/sysctl/kernel.html#domainname-hostnameMotivation for this change
Things done
sandbox
innix.conf
on non-NixOS linux)nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
./result/bin/
)nix path-info -S
before and after)Notify maintainers
cc @