Skip to content

Instantly share code, notes, and snippets.

@pcmehrdad
Last active June 8, 2023 11:10
Show Gist options
  • Save pcmehrdad/2fbc9651a6cff249f0576b784fdadef0 to your computer and use it in GitHub Desktop.
Save pcmehrdad/2fbc9651a6cff249f0576b784fdadef0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Run one of these following commands as root:
# bash <(curl -Ls https://git.io/J9OER)
# bash <(curl -Ls https://gist.github.com/pcmehrdad/2fbc9651a6cff249f0576b784fdadef0/raw)
#RED="\e[31m"
#GREEN="\e[32m"
CLR_RED="\033[1;31m"
CLR_GREEN="\033[1;32m"
CLR_YELLOW="\033[1;33m"
CLR_BLUE="\033[1;34m"
CLR_PURPLE="\033[1;35m"
CLR_CYAN="\033[1;36m"
CLR_GREY="\033[0;37m"
CLR_RESET="\033[m"
CLR_END="\e[0m"
clear
if [[ $EUID != 0 ]]; then
echo -e "Please run this as ${CLR_RED}root${CLR_END} user"
exit 1
fi
echo -n -e "Please enter ${CLR_RED}new ssh port number${CLR_END} between 1024 and 65535 \n>"
while read SSHPORT; do
if [[ "$SSHPORT" =~ ^[0-9]{2,5}$ || "$SSHPORT" = 22 ]]; then
if [[ "$SSHPORT" -ge 1024 && "$SSHPORT" -le 65535 || "$SSHPORT" = 22 ]]; then
# Backup current sshd config file
# NOW_TIME=$(date +"%m_%d_%Y-%H_%M_%S")
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.inst.bckup.$(date +"%m_%d_%Y-%H_%M_%S")
sed -i -e "/Port /c\Port $SSHPORT" /etc/ssh/sshd_config
echo -e "Restarting SSH service. Please wait 5 seconds...\n"
sleep 5
service sshd restart
echo ""
echo -e "${CLR_GREEN}The SSH port has been changed to $SSHPORT ${CLR_END}.\n${CLR_YELLOW}Please login using that port to test BEFORE ending this session${CLR_END}.\n"
exit 0
else
echo -e "${CLR_YELLOW}Invalid port${CLR_END}: It must be 22, or between 1024 and 65535."
echo -n -e "Please enter ${CLR_RED}new ssh port number${CLR_END} between 1024 and 65535 \n>"
fi
else
echo -e "${CLR_YELLOW}Invalid port${CLR_END}: Port number must be numeric!"
echo -n -e "Please enter ${CLR_RED}new ssh port number${CLR_END} between 1024 and 65535 \n>"
fi
done
echo ""
@pcmehrdad
Copy link
Author

orginal script was here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment