]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostnamed: stop discriminating against "localhost" in /etc/hostname
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 4 Dec 2020 18:56:49 +0000 (19:56 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Dec 2020 10:02:18 +0000 (11:02 +0100)
We would sometimes ignore localhost-style names in /etc/hostname. That is
brittle. If the user configured some hostname, it's most likely because they
want to use that as the hostname. If they don't want to use such a hostname,
they should just not create the config. Everything becomes simples if we just
use the configured hostname as-is.

This behaviour seems to have been a workaround for Anaconda installer and other
tools writing out /etc/hostname with the default of "localhost.localdomain".
Anaconda PR to stop doing that: https://github.com/rhinstaller/anaconda/pull/3040.
That might have been useful as a work-around for other programs misbehaving if
/etc/hostname was not present, but nowadays it's not useful because systemd
mostly controls the hostname and it is perfectly happy without that file.

Apart from making things simpler, this allows users to set a hostname like
"localhost" and have it honoured, if such a whim strikes them.

man/hostnamectl.xml
src/hostname/hostnamed.c

index f50cefa217eac7c175a9617edc9d09f3932b2da4..224dab78a7c48574f73be27ee4d787e1bfd9713a 100644 (file)
@@ -39,8 +39,8 @@
     and this tool distinguish three different hostnames: the high-level "pretty" hostname which might include
     all kinds of special characters (e.g. "Lennart's Laptop"), the "static" hostname which is the
     user-configured hostname (e.g. "lennarts-laptop"), and the transient hostname which is a fallback value
-    received from network configuration (e.g. "node12345678"). If a static hostname is set, and is valid
-    (something other than localhost), then the transient hostname is not used.</para>
+    received from network configuration (e.g. "node12345678"). If a static hostname is set to a valid value,
+    then the transient hostname is not used.</para>
 
     <para>Note that the pretty hostname has little restrictions on the characters and length used, while the static and
     transient hostnames are limited to the usually accepted characters of Internet domain names, and 64 characters at
index 43b5c1161244776369cbeea8487ca35d8d6265ea..9e8a4dd8a4218fbe40f4982ea29131ae1994c329 100644 (file)
@@ -312,15 +312,11 @@ static char* context_fallback_icon_name(Context *c) {
         return strdup("computer");
 }
 
-static bool hostname_is_useful(const char *hn) {
-        return !isempty(hn) && !is_localhost(hn);
-}
-
 static int context_update_kernel_hostname(
                 Context *c,
                 const char *transient_hn) {
 
-        const char *static_hn, *hn;
+        const char *hn;
         struct utsname u;
         int r;
 
@@ -333,21 +329,14 @@ static int context_update_kernel_hostname(
                         isempty(u.nodename) || streq(u.nodename, "(none)") ? NULL : u.nodename;
         }
 
-        static_hn = c->data[PROP_STATIC_HOSTNAME];
-
-        /* /etc/hostname with something other than "localhost"
-         * has the highest preference ... */
-        if (hostname_is_useful(static_hn))
-                hn = static_hn;
+        /* /etc/hostname has the highest preference ... */
+        if (c->data[PROP_STATIC_HOSTNAME])
+                hn = c->data[PROP_STATIC_HOSTNAME];
 
         /* ... the transient hostname, (ie: DHCP) comes next ... */
         else if (!isempty(transient_hn))
                 hn = transient_hn;
 
-        /* ... fallback to static "localhost.*" ignored above ... */
-        else if (!isempty(static_hn))
-                hn = static_hn;
-
         /* ... and the ultimate fallback */
         else
                 hn = FALLBACK_HOSTNAME;