Skip to content

Instantly share code, notes, and snippets.

@Rigil-Kent
Created February 3, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rigil-Kent/debfdef5daa439187bb4368a90716f1f to your computer and use it in GitHub Desktop.
Save Rigil-Kent/debfdef5daa439187bb4368a90716f1f to your computer and use it in GitHub Desktop.
Change SSH Port to a random number in a specified range
# range is inclusive
# replaces the entire line of the match (uncommenting in the case of newly provisioned servers)
NEW_PORT=$(shuf -i 3101-7999 -n 1)
sudo sed -i "/Port 22/c Port $NEW_PORT" /etc/ssh/sshd_config && sudo systemctl restart sshd && echo "SSH Port has been changed to $NEW_PORT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment