utils/ssh-port.sh

33 lines
766 B
Bash
Raw Normal View History

2024-07-07 00:30:41 +05:00
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
2024-07-27 04:15:43 +05:00
echo -e "\033[31mThis script requires superuser rights.\033[0m"
exit 0
2024-07-07 00:30:41 +05:00
fi
if [ -z "$1" ]; then
echo "Please provide the port as an argument"
exit 1
fi
NEW_PORT="$1"
SSH_CONFIG_FILE="/etc/ssh/sshd_config"
2024-07-27 04:15:43 +05:00
# Проверяем, что параметр является числом
if ! [[ "$NEW_PORT" =~ ^[0-9]+$ ]]; then
echo -e "\033[31mPort must be a number\033[0m"
exit 1
fi
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT
set -e
2024-07-07 00:30:41 +05:00
echo "Change SSH port to $NEW_PORT..."
cp $SSH_CONFIG_FILE $SSH_CONFIG_FILE.bak
2024-07-27 04:15:43 +05:00
sed -i "s/^#\?Port [0-9]*/Port $NEW_PORT/" $SSH_CONFIG_FILE
systemctl daemon-reload
systemctl restart ssh
2024-07-07 00:30:41 +05:00
trap - EXIT
echo "SSH port successfully changed to $NEW_PORT."