添加 simpleadmin/console/menu/change_hostname.sh
This commit is contained in:
parent
25677bdb67
commit
7121eee2ad
40
simpleadmin/console/menu/change_hostname.sh
Normal file
40
simpleadmin/console/menu/change_hostname.sh
Normal file
@ -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"
|
||||
Loading…
Reference in New Issue
Block a user