From 7c301848cbc097d299bcb0f551fe27de2fecac28 Mon Sep 17 00:00:00 2001 From: iliajie Date: Wed, 5 Oct 2022 22:32:29 +0300 Subject: [PATCH] Fix setting `hostname` for Debian and Ubuntu using `hostnamectl` --- net/debian-linux-lib.pl | 7 +++++++ net/netplan-lib.pl | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/net/debian-linux-lib.pl b/net/debian-linux-lib.pl index fee9b8128..7dffea6df 100755 --- a/net/debian-linux-lib.pl +++ b/net/debian-linux-lib.pl @@ -571,6 +571,13 @@ foreach my $f ("/etc/hostname", "/etc/HOSTNAME", "/etc/mailname") { &close_tempfile(HOST); } } + +# Use the hostnamectl command as well +if (&has_command("hostnamectl")) { + &system_logged("hostnamectl set-hostname ".quotemeta($hostname). + " >/dev/null 2>&1"); + } + undef(@main::get_system_hostname); # clear cache } diff --git a/net/netplan-lib.pl b/net/netplan-lib.pl index 2bf3e93eb..a6ee261ad 100644 --- a/net/netplan-lib.pl +++ b/net/netplan-lib.pl @@ -387,15 +387,21 @@ return &get_system_hostname(); sub save_hostname { my ($hostname) = @_; -my (%conf, $f); &system_logged("hostname ".quotemeta($hostname)." >/dev/null 2>&1"); -foreach $f ("/etc/hostname", "/etc/HOSTNAME", "/etc/mailname") { +foreach my $f ("/etc/hostname", "/etc/HOSTNAME", "/etc/mailname") { if (-r $f) { &open_lock_tempfile(HOST, ">$f"); - &print_tempfile(HOST, $_[0],"\n"); + &print_tempfile(HOST, $hostname,"\n"); &close_tempfile(HOST); } } + +# Use the hostnamectl command as well +if (&has_command("hostnamectl")) { + &system_logged("hostnamectl set-hostname ".quotemeta($hostname). + " >/dev/null 2>&1"); + } + undef(@main::get_system_hostname); # clear cache }