Skip to content

Instantly share code, notes, and snippets.

@AdamQuixote
Created December 4, 2022 19:25
Show Gist options
  • Select an option

  • Save AdamQuixote/573b29a3429602713a825c2fdd5beb91 to your computer and use it in GitHub Desktop.

Select an option

Save AdamQuixote/573b29a3429602713a825c2fdd5beb91 to your computer and use it in GitHub Desktop.
Linux ssh password authentication toggle script
#!/bin/bash
# make a backup copy of the sshd_config file
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# check if the first argument is "enable" or "disable"
if [ "$1" == "enable" ]; then
# enable password authentication
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/; s/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
echo "Password authentication has been enabled"
systemctl restart ssh.service
elif [ "$1" == "disable" ]; then
# disable password authentication
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/; s/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
echo "Password authentication has been disabled"
systemctl restart ssh.service
else
# print an error message if the first argument is not "enable" or "disable"
echo "Error: Invalid argument. Please specify either 'enable' or 'disable' as the first argument."
exit 1
fi
@AdamQuixote
Copy link
Copy Markdown
Author

only tested with debian and ubuntu so far

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