Skip to content

Instantly share code, notes, and snippets.

@shiwildy
Last active January 27, 2025 08:56
Show Gist options
  • Save shiwildy/b6bf2cf2ce02f24d503f9bcce39b7fe6 to your computer and use it in GitHub Desktop.
Save shiwildy/b6bf2cf2ce02f24d503f9bcce39b7fe6 to your computer and use it in GitHub Desktop.
Simple Bash script to enable ssh remote in Linux server
#!/bin/bash
if [[ $(whoami) != 'root' ]]; then
echo "Please run this script as root (or with sudo)."
exit 1
fi
read -p "New Password [or press Enter to generate a random password]: " newpass
if [[ -z "${newpass}" ]]; then
newpass=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 12)
echo "No password entered. A random password has been generated."
fi
rm -rf /etc/ssh/*
cat > /etc/ssh/sshd_config << 'EOF'
Port 22
ListenAddress 0.0.0.0
ListenAddress ::
UsePAM yes
UseDNS no
TCPKeepAlive yes
X11Forwarding yes
PermitRootLogin yes
PrintMotd no
AcceptEnv LANG LC_*
ChallengeResponseAuthentication no
EOF
ssh-keygen -A
systemctl restart ssh &> /dev/null
systemctl restart sshd &> /dev/null
echo "root:${newpass}" | chpasswd
rm -rf change.sh
clear
echo "Root password successfully updated."
echo "==================================="
echo " New Root Password: ${newpass}"
echo "==================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment