PortGate

Resources · Security guides · Security guide

Turn off SSH passwords without locking yourself out

Install and test a key, write a drop-in that sshd reads first, check the effective config with sshd -T, and keep a second session and the firewall rule in place until it works.

Check command

sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '

Copied.

Password logins are what SSH brute-force traffic is trying. Key-only authentication ends that, but the change is also the easiest way to lock yourself out of a VPS. Do it in this order and keep an escape route open the whole time.

1. Install a key and prove it works

On your laptop, create a key if you do not have one, then copy it to the server:

ssh-keygen -t ed25519 -C "you@laptop"
ssh-copy-id [email protected]

Open a new terminal and log in with the key only:

ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]

If that does not give you a shell, stop here and fix the key. Check ~/.ssh/authorized_keys permissions: the directory 700, the file 600, both owned by the user.

2. Write a drop-in that wins

On Debian and Ubuntu, /etc/ssh/sshd_config includes /etc/ssh/sshd_config.d/*.conf near the top, and sshd uses the first value it reads for most options. Cloud images often ship 50-cloud-init.conf with PasswordAuthentication yes, which silently beats a change at the bottom of the main file. Use a file that sorts first:

sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
EOF

prohibit-password lets root log in with a key only. Use no if you always log in as a normal user and use sudo.

3. Validate before you reload

sudo sshd -t
sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '

sshd -t must print nothing. sshd -T shows the effective values, after every include. Only reload when they read no, no and prohibit-password (or no).

sudo systemctl reload ssh     # Debian and Ubuntu
sudo systemctl reload sshd    # RHEL, Rocky, Alma

Keep your current session open, and log in again from a new terminal. If the new login fails, the open session is how you undo the change.

4. Keep the firewall rule in place

If you enable UFW in the same session, allow SSH first:

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose

If you moved SSH to another port, allow that port number before enabling the firewall and before removing the old rule. Know where your provider's web console is: it is the way back in when both SSH and the firewall are wrong.

What changes afterwards

Failed logins drop to bots trying keys they do not have, and journalctl -u ssh gets quieter. Brute force no longer matters, but a stolen private key still does: protect it with a passphrase, and remove keys for people who leave from every authorized_keys file.