From 3c26b2be3b1cc2fb92715d4aa470124252899ff3 Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Fri, 21 Aug 2026 19:37:47 +0200 Subject: [PATCH] Fix FQDN detection on systemd systems MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⓘ Do not return a short name from /etc/hostname when the full name is requested; resolve it with hostname -f first, honoring the no_hostname_f option, and otherwise fall back to the existing file-based logic. Also check for /run/systemd/system instead of reading /proc/1/comm as it's much cheaper. Fixes #2054 --- web-lib-funcs.pl | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/web-lib-funcs.pl b/web-lib-funcs.pl index ecac86ab3..03310b033 100755 --- a/web-lib-funcs.pl +++ b/web-lib-funcs.pl @@ -4859,11 +4859,25 @@ return if ($nocache == 2); if (!$system_hostname[$m]) { if ($gconfig{'os_type'} ne 'windows') { # If systemd system try /etc/hostname straight away - my $initsys = &trim(&backquote_command("cat /proc/1/comm 2>/dev/null")); - if ($initsys eq 'systemd') { + if (-d "/run/systemd/system") { my $hostname = &read_file_contents("/etc/hostname"); $hostname =~ s/\r|\n//g; + # Cache the short name before resolving the full name if ($hostname) { + my $shortname = $hostname; + $shortname =~ s/\..*$//; + $system_hostname[1] = $shortname; + } + # Resolve a short static hostname before caching it + if ($hostname && !$m && $hostname !~ /\./ && + !$gconfig{'no_hostname_f'}) { + my $fqdn; + my $ex = &execute_command("hostname -f", undef, + \$fqdn, undef, 0, 1); + $fqdn = &trim($fqdn); + $hostname = $fqdn if (!$ex && $fqdn =~ /\./); + } + if ($hostname && ($m || $hostname =~ /\./)) { $hostname =~ s/\..*$// if ($m); $system_hostname[$m] = $hostname; return $hostname;