Last active
January 27, 2025 08:56
-
-
Save shiwildy/b6bf2cf2ce02f24d503f9bcce39b7fe6 to your computer and use it in GitHub Desktop.
Simple Bash script to enable ssh remote in Linux server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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