hostname is set to localhost on our nixos machines #163

Open
opened 2024-09-28 20:43:29 +02:00 by felixalb · 1 comment
Owner
[nix-shell:~/pvv-nixos-config]$ hostname
bekkalokk

[nix-shell:~/pvv-nixos-config]$ hostname -d

[nix-shell:~/pvv-nixos-config]$ hostname -f
localhost
``` [nix-shell:~/pvv-nixos-config]$ hostname bekkalokk [nix-shell:~/pvv-nixos-config]$ hostname -d [nix-shell:~/pvv-nixos-config]$ hostname -f localhost ```
felixalb added the
good first issue
bug
nixos
labels 2024-09-28 20:43:29 +02:00
felixalb added this to the Kanban project 2024-09-28 20:43:29 +02:00
Owner

Dumping a bit of research I've done here. I think this might be highly relevant to get heimdal to work properly as well. This is incomplete.

inetutils hostname

[oysteikt@bekkalokk:~]$ hostname -v -f
gethostname()=`bekkalokk'
Resolving `bekkalokk' ...
Result: h_name=`localhost'
Result: h_addr_list=`::1'
localhost

During the resolving step, gethostbyname is being used

The manpage marks this function as outdated.

Debian hostname

Debian provides a newer hostname command that uses more modern syscalls.

Debians hostname command goes a little bit something like this:

hostname

Just gethostname() with some memory and error handling

bekkalokk output

[oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname'
bekkalokk

hostname --fqdn

hostname = gethostname(); # bekkalokk
hints = {
  .ai_socktype = SOCK_DGRAM;
  .ai_flags = AI_CANONNAME;
};
result = getaddrinfo(hostname, hints);

bekkalokk output

[oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname --fqdn'
localhost

hostname --all-fqdns

interface_addresses = getifaddrs();
for if_addr in interface_addresses {
   # skip interfaces without addresses
   # skip loopback interfaces
   # skip interfaces that are down
   # skip non-ipv4/ipv6 addresses
   # skip link-local ipv6 addresses
   result = getnameinfo(if_addr->ifa_addr, NI_NAMEREQD) # will error if no name for ip, also skip
}

bekkalokk output

[oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname --all-fqdns'
bekkalokk

Considering ip a's output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:25:90:57:54:4d brd ff:ff:ff:ff:ff:ff
    altname enp0s25
3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:25:90:57:54:4c brd ff:ff:ff:ff:ff:ff
    inet 129.241.210.168/25 brd 129.241.210.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 2001:700:300:1900::168/64 scope global
       valid_lft forever preferred_lft forever
    inet6 fe80::225:90ff:fe57:544c/64 scope link proto kernel_ll
       valid_lft forever preferred_lft forever

I suppose that means that getnameinfo("129.241.210.168") results in bekkalokk?

Behind getaddrinfo, getaddrinfo and getnameinfo

getnameinfo seems to get its information from NSS but do the others?

With systemd-resolved enabled, it states itself that it would prefer to come first in the list of host resolvers (nss-resolve(8))

systemd-resolved will handle querying /etc/hosts and other sources.

NixOS follows up on this recommendation

bekkalokk /etc/nsswitch.conf

passwd:    files systemd
group:     files [success=merge] systemd
shadow:    files
sudoers:   files

hosts:     mymachines resolve [!UNAVAIL=return] files myhostname dns
networks:  files

ethers:    files
services:  files
protocols: files
rpc:       files

bekkalokk /etc/hosts

NixOS puts localhost first in queue, before fqdn, with comments stating that fqdn is there for nss, but that other programs might expect localhost to be first

e2605d0744/nixos/modules/config/networking.nix (L158-L185)

127.0.0.1 localhost
::1 localhost
127.0.0.2 bekkalokk.pvv.ntnu.no bekkalokk
::1 bekkalokk.pvv.ntnu.no bekkalokk

systemd-resolved nss-resolve module

https://github.com/systemd/systemd/blob/main/src/nss-resolve/nss-resolve.c

Systemd nss resolve talks to resolved via varlink.

Dumping a bit of research I've done here. I think this might be highly relevant to get heimdal to work properly as well. This is incomplete. ## inetutils `hostname` ```bash [oysteikt@bekkalokk:~]$ hostname -v -f gethostname()=`bekkalokk' Resolving `bekkalokk' ... Result: h_name=`localhost' Result: h_addr_list=`::1' localhost ``` During the resolving step, [`gethostbyname` is being used](https://github.com/ecki/net-tools/blob/9ee12437b677869ecd2e82415af891dd85ea96cc/hostname.c#L155C16-L155C29) The manpage marks this function as outdated. ## Debian `hostname` Debian provides [a newer `hostname` command that uses more modern syscalls.](https://packages.debian.org/unstable/hostname) Debians `hostname` command goes a little bit something like this: ### `hostname` Just `gethostname()` with some memory and error handling #### bekkalokk output ```bash [oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname' bekkalokk ``` ### `hostname --fqdn` ``` hostname = gethostname(); # bekkalokk hints = { .ai_socktype = SOCK_DGRAM; .ai_flags = AI_CANONNAME; }; result = getaddrinfo(hostname, hints); ``` #### bekkalokk output ```bash [oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname --fqdn' localhost ``` ### `hostname --all-fqdns` ``` interface_addresses = getifaddrs(); for if_addr in interface_addresses { # skip interfaces without addresses # skip loopback interfaces # skip interfaces that are down # skip non-ipv4/ipv6 addresses # skip link-local ipv6 addresses result = getnameinfo(if_addr->ifa_addr, NI_NAMEREQD) # will error if no name for ip, also skip } ``` #### bekkalokk output ```bash [oysteikt@bekkalokk:~]$ nix-shell -p hostname-debian --run 'hostname --all-fqdns' bekkalokk ``` Considering `ip a`'s output: ``` 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host noprefixroute valid_lft forever preferred_lft forever 2: eno1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 00:25:90:57:54:4d brd ff:ff:ff:ff:ff:ff altname enp0s25 3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:25:90:57:54:4c brd ff:ff:ff:ff:ff:ff inet 129.241.210.168/25 brd 129.241.210.255 scope global enp2s0 valid_lft forever preferred_lft forever inet6 2001:700:300:1900::168/64 scope global valid_lft forever preferred_lft forever inet6 fe80::225:90ff:fe57:544c/64 scope link proto kernel_ll valid_lft forever preferred_lft forever ``` I suppose that means that `getnameinfo("129.241.210.168")` results in `bekkalokk`? ## Behind `getaddrinfo`, `getaddrinfo` and `getnameinfo` `getnameinfo` seems to get its information from NSS but do the others? With systemd-resolved enabled, it states itself that it would prefer to come first in the list of host resolvers ([nss-resolve(8)](https://www.freedesktop.org/software/systemd/man/latest/nss-resolve.html#Description)) systemd-resolved will handle querying `/etc/hosts` and other sources. [NixOS follows up on this recommendation](https://github.com/NixOS/nixpkgs/blob/1caf076d6ba460ffe0b7b13cf60599fd49b2b28d/nixos/modules/system/boot/resolved.nix#L165-L168) ### bekkalokk `/etc/nsswitch.conf` ``` passwd: files systemd group: files [success=merge] systemd shadow: files sudoers: files hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns networks: files ethers: files services: files protocols: files rpc: files ``` ### bekkalokk `/etc/hosts` NixOS puts localhost first in queue, before fqdn, with comments stating that fqdn is there for nss, but that other programs might expect localhost to be first https://github.com/NixOS/nixpkgs/blob/e2605d0744c2417b09f8bf850dfca42fcf537d34/nixos/modules/config/networking.nix#L158-L185 ``` 127.0.0.1 localhost ::1 localhost 127.0.0.2 bekkalokk.pvv.ntnu.no bekkalokk ::1 bekkalokk.pvv.ntnu.no bekkalokk ``` ### systemd-resolved nss-resolve module https://github.com/systemd/systemd/blob/main/src/nss-resolve/nss-resolve.c Systemd nss resolve talks to resolved via varlink.
oysteikt removed the
good first issue
label 2024-12-10 19:32:02 +01:00
oysteikt added the
networking
label 2025-01-09 13:49:38 +01:00
Sign in to join this conversation.
No description provided.