From 4b68890429ef91e94bba120b50046cf7069c56b5 Mon Sep 17 00:00:00 2001 From: sky Date: Fri, 13 Dec 2024 21:28:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20simpleadmin/console/menu/c?= =?UTF-8?q?hange=5Fhostname.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simpleadmin/console/menu/change_hostname.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/simpleadmin/console/menu/change_hostname.sh b/simpleadmin/console/menu/change_hostname.sh index a3c3b4d..ccd7639 100644 --- a/simpleadmin/console/menu/change_hostname.sh +++ b/simpleadmin/console/menu/change_hostname.sh @@ -34,6 +34,26 @@ remount_rw echo "$new_hostname" > /etc/hostname sed -i "s/127.0.1.1.*$/127.0.1.1\t$new_hostname/g" /etc/hosts hostname "$new_hostname" + +# Create and enable the hostname service +cat > /lib/systemd/system/set-hostname.service << EOF +[Unit] +Description=Set system hostname at boot +After=network.target + +[Service] +Type=oneshot +ExecStart=/bin/sh -c 'hostname \$(cat /etc/hostname)' +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target +EOF + +systemctl daemon-reload +systemctl enable set-hostname.service +systemctl start set-hostname.service + remount_ro echo -e "\033[0;32mHostname has been changed to: $new_hostname\033[0m"