utils/ssh-pw.sh

46 lines
1.1 KiB
Bash
Raw Normal View History

2024-07-27 04:15:43 +05:00
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo -e "\033[31mThis script requires superuser rights\033[0m"
exit 0
fi
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <y/n>"
exit 1
fi
if [ "$1" != "y" ] && [ "$1" != "n" ]; then
echo "Invalid argument. Use 'y' to enable password authentication and 'n' to disable it"
exit 1
fi
2024-08-05 07:01:48 +05:00
ENABLE_PASSWORD=$1
2024-07-27 04:15:43 +05:00
SSHD_CONFIG="/etc/ssh/sshd_config"
2024-08-05 07:01:48 +05:00
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' ERR
set -e
2024-07-27 04:15:43 +05:00
2024-08-05 07:01:48 +05:00
echo "Backup $SSHD_CONFIG..."
cp $SSHD_CONFIG $SSHD_CONFIG.backup
2024-07-27 04:15:43 +05:00
2024-08-05 07:01:48 +05:00
if [ "$ENABLE_PASSWORD" == "y" ]; then
sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' $SSHD_CONFIG
sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' $SSHD_CONFIG
else
sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG
sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG
fi
2024-07-27 04:15:43 +05:00
2024-08-05 07:01:48 +05:00
echo "Restart SSH for apply password $1"
systemctl daemon-reload
systemctl restart ssh
2024-07-27 04:15:43 +05:00
2024-08-05 07:01:48 +05:00
trap - ERR
2024-07-27 04:15:43 +05:00
if [ "$1" == "y" ]; then
echo "Password authentication has been enabled"
else
echo "Password authentication has been disabled"
fi