Solved: Net::Domain hostname FQDN contains domain suffix twice
Issue: Perl’s Net::Domain functions report full-qualified hostname incorrectly, containing domain name twice. At the same time, the output of hostname -s
(just <hosname>) and hostname -f
(<hostname>.<domainname> without any duplication) are working correctly. Systems tested: Debian 11 and 12.
Observed behavior | Expected behavior | |
Host FQDN: | <hostname>.<domainname>.<domainname> | <hostname>.<domainname> |
Hostname: | <hostname> | <hostname> |
Hostdomain: | <domainname>.<domainname> | <domainame> |
Reason: The perl lib tries multiple alternatives to identify the local hostname based on detected system, for Debian it was using the following branch, which calls the system function gethostname. Thus, the reported hostname of this system function is the source of the devil.
eval {
package main;
require "sys/syscall.ph"; ## no critic (Modules::RequireBarewordIncludes)
defined(&main::SYS_gethostname);
}
and $host = (syscall(&main::SYS_gethostname, $tmp, 256) == 0) ? $tmp : undef;
Solution: Please check the content of /etc/hostname
, which is typically set once during system setup. It is mandatory to contain just the hostname without the domain suffix (not the FQDN) to work correctly with perl’s Net::Domain implementation. The system’s hostname is typically automatically set during boot sequence, e.g. by /etc/init.d/hostname.sh
, which reads contents from /etc/hostname
. After changing the content of /etc/hostname
you might either run /etc/init.d/hostname.sh
or just sth. like sudo hostname $(cat /etc/hostname)
Alternatively, a quick workaround / fix for Net::Domain is the following: remove everything after the first dot from the hostname, see patch (minor disadvantage: hostnames just containing an IPv4 address will be truncated to the first tuple of the IPv4 address):
--- Net::Domain.pm 2023-07-03 12:12:03.457201483 +0200
+++ Net::Domain.pm.new 2023-07-03 12:12:53.331898622 +0200
@@ -106,6 +106,7 @@
$host =~ s/[\0\r\n]+//go;
$host =~ s/(\A\.+|\.+\Z)//go;
$host =~ s/\.\.+/\./go;
+ $host =~ s/([^\.]+)\..+/\1/go;
$host;
}