utils/ssh-port.sh
2024-07-06 19:30:41 +00:00

26 lines
626 B
Bash

#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script requires superuser rights. Running with sudo..."
exec sudo "$0" "$@"
fi
if [ -z "$1" ]; then
echo "Please provide the port as an argument"
exit 1
fi
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT
set -e
NEW_PORT="$1"
SSH_CONFIG_FILE="/etc/ssh/sshd_config"
echo "Change SSH port to $NEW_PORT..."
cp $SSH_CONFIG_FILE $SSH_CONFIG_FILE.bak
sed -i "s/^#Port 22/Port $NEW_PORT/" $SSH_CONFIG_FILE
sed -i "s/^Port 22/Port $NEW_PORT/" $SSH_CONFIG_FILE
systemctl restart sshd
trap - EXIT
echo "SSH port successfully changed to $NEW_PORT."