#!/bin/bash if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights.\033[0m" exit 0 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" # Проверяем, что параметр является числом 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 echo "Change SSH port to $NEW_PORT..." cp $SSH_CONFIG_FILE $SSH_CONFIG_FILE.bak sed -i "s/^#\?Port [0-9]*/Port $NEW_PORT/" $SSH_CONFIG_FILE systemctl daemon-reload systemctl restart ssh trap - EXIT echo "SSH port successfully changed to $NEW_PORT."