From 7121eee2adb36cabb4f040e5f9d2477971705a9e Mon Sep 17 00:00:00 2001 From: sky Date: Fri, 13 Dec 2024 19:40:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=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 | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 simpleadmin/console/menu/change_hostname.sh diff --git a/simpleadmin/console/menu/change_hostname.sh b/simpleadmin/console/menu/change_hostname.sh new file mode 100644 index 0000000..a3c3b4d --- /dev/null +++ b/simpleadmin/console/menu/change_hostname.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Function to remount file system as read-write +remount_rw() { + mount -o remount,rw / +} + +# Function to remount file system as read-only +remount_ro() { + mount -o remount,ro / +} + +# Display current hostname +current_hostname=$(cat /etc/hostname) +echo -e "\033[0;32mCurrent hostname: $current_hostname\033[0m" + +# Prompt for new hostname +echo -e "\033[0;32mEnter new hostname (or 'exit' to cancel):\033[0m" +read new_hostname + +if [ "$new_hostname" = "exit" ]; then + echo -e "\033[0;31mHostname change cancelled.\033[0m" + exit 0 +fi + +# Validate hostname format +if ! echo "$new_hostname" | grep -qE '^[a-zA-Z0-9-]+$'; then + echo -e "\033[0;31mInvalid hostname. Hostname can only contain letters, numbers, and hyphens.\033[0m" + exit 1 +fi + +# Change hostname +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" +remount_ro + +echo -e "\033[0;32mHostname has been changed to: $new_hostname\033[0m" +echo -e "\033[0;33mPlease reboot the device for the change to take full effect.\033[0m" \ No newline at end of file