Skip to content

Instantly share code, notes, and snippets.

@marcy-terui
Created March 10, 2014 07:13
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save marcy-terui/9460706 to your computer and use it in GitHub Desktop.
Save marcy-terui/9460706 to your computer and use it in GitHub Desktop.
Disable PasswordAuthentication and Enable PubkeyAuthentication on Sakura VPS.
USER_NAME=marcy
sed -i "s/.*RSAAuthentication.*/RSAAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/.*PubkeyAuthentication.*/PubkeyAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/.*PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config
sed -i "s/.*AuthorizedKeysFile.*/AuthorizedKeysFile\t\.ssh\/authorized_keys/g" /etc/ssh/sshd_config
sed -i "s/.*PermitRootLogin.*/PermitRootLogin no/g" /etc/ssh/sshd_config
echo "${USER_NAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
service sshd restart
useradd -p "" $USER_NAME
passwd -fu $USER_NAME
sudo -u $USER_NAME mkdir /home/$USER_NAME/.ssh
sudo -u $USER_NAME chmod 700 /home/$USER_NAME/.ssh
sudo -u $USER_NAME ssh-keygen -t rsa -b 2048 -N "" -f /home/$USER_NAME/.ssh/id_rsa
cat /home/$USER_NAME/.ssh/id_rsa.pub > /home/$USER_NAME/.ssh/authorized_keys
chmod 600 /home/$USER_NAME/.ssh/authorized_keys
chown $USER_NAME:$USER_NAME /home/$USER_NAME/.ssh/authorized_keys
@guettli
Copy link

guettli commented Jan 4, 2024

After executing this line,

sed -i "s/.*PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config

the sshd_config looks like this:

root@virtual-pc:~# diff ~me/sshd_config /etc/ssh/
57c57
< #PasswordAuthentication yes
---
> PasswordAuthentication no
79c79
< # PasswordAuthentication.  Depending on your PAM configuration,
---
> PasswordAuthentication no
83c83
< # PAM authentication, then enable this but set PasswordAuthentication
---
> PasswordAuthentication no

@guettli
Copy link

guettli commented Jan 4, 2024

I use this now:

sed -ri "s/^#? *PasswordAuthentication *yes.*/PasswordAuthentication no/" /etc/ssh/sshd_config

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