On Wed, Apr 22, 2020 at 08:18:24PM +0200, Thomas HUMMEL wrote:
Hello, using NetworkManager-1.20.0-5.el8_1.x86_64 on CentOS 8.1 I tried to figure out the hostname-mode semantics beyond what the manual says (I've got another thread for this but somehow unrelated to the following question). In this post, I'd like to clarify this NetworkManager.conf(5) quote for default : "If not present, the hostname will be updated to the last one set outside NetworkManager." For what I saw when tracing, nm-policy reads the hostname-original and restores this hostname when the DHCP connection gets deactivated. Looking at the source, it seems to match this part in nm-policy.c:update_system_hostname() : if (!priv->default_ac4 && !priv->default_ac6) { /* No best device; fall back to the last hostname set externally * to NM or if there wasn't one, 'localhost.localdomain' */ _set_hostname (self, priv->orig_hostname, "no default device"); return; } Same kind of mechanism without default fallbacks for hostname-mode=dhcp What's not clear to me is : what if the transient hostname is changed outside NetworkManager after hostname-original gets read at NM start ? Let's say someone issues hostname foobar (instead of hostnamctl --transient set-hostname foobar) while the DHCP connection is still up : would that become the new set-out-of-NM hostname wich would be restored when the connection is deactivated ?
Yes, NM always re-reads the kernel hostname to detect whether it has changed before trying to set a new one. When the DHCP connection gets deactivated, NM should notice that the hostname was changed externally and use it as the 'last one set outside NetworkManager' mentioned in the man page.
Technically NM could notice it directly issuing gethostname(3) but my understanding is that it would rather rely on systemd-hostnamed/dbus for that. The latter seems to have in some cases some delay to catch up with transient hostanme.
It uses systemd-hostnamed, and falls back to gethostname() in case of failure [1]. [1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.24-rc1/src/nm-policy.c#L550 Beniamino
Attachment:
signature.asc
Description: PGP signature