How to Disable SSH Password Authentication and Use Key-Only Login
Password logins are the weakest part of most servers. Bots try thousands of common passwords against port 22 every day, and one weak password on one account can hand over the whole machine. To disable ssh password authentication means the server stops accepting passwords at login and accepts only cryptographic keys, which cannot be guessed by brute force. This guide walks through the change on Ubuntu, with a test step that keeps you from locking yourself out.
Before you change anything, make sure you already have a working key login. If you have never set one up, follow a full walkthrough first, such as our guide on how to set up SSH keys on a new VPS. The order matters: key access first, password off second. Reversing that order is how admins lock themselves out of their own servers.
Disable SSH Password Authentication in sshd_config
SSH server settings live in /etc/ssh/sshd_config on Ubuntu. Open the file with sudo and look for the line that controls password logins. On a fresh install it often reads #PasswordAuthentication yes, with the hash mark meaning the line is commented out and the default applies. The default allows passwords, so leaving the line commented does not protect you.
sudo nano /etc/ssh/sshd_config
Change the line to the setting below, removing the hash mark if one is present. The sshd_config PasswordAuthentication directive is the single switch for this change, and it must read exactly as shown, with no second copy of the line later in the file overriding it.
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Check the file for duplicate entries before you save. Some distributions ship an include directory, /etc/ssh/sshd_config.d/, and a file inside it can override the main file. Search the whole directory for a second PasswordAuthentication line and fix it there too, or the server will keep accepting passwords and you will wonder why the change did nothing.
While you are in the file, confirm two related settings. PubkeyAuthentication yes must be present so key logins keep working. PermitRootLogin prohibit-password or no stops direct root password logins as well. Our broader SSH hardening guide for Ubuntu 24.04 covers these and other settings in one pass, if you want to harden the service in full rather than changing one line.
Set Up Key-Only Login Before You Switch
Key-only login, also called ssh key-only login, works because the server holds your public key and your laptop holds the matching private key. The private key never crosses the network. An attacker who does not hold it cannot log in, no matter how many passwords they try. That is the whole security gain in one sentence.
On your own computer, generate a key if you do not have one, then copy the public half to the server with the command below. Replace the user and host with your own values.
ssh-keygen -t ed25519 -C "admin@myserver"
ssh-copy-id user@your-server-ip
The ssh authorized_keys setup on the server is the file ~/.ssh/authorized_keys in the login user’s home directory. Check that it holds your public key, that the .ssh directory has mode 700, and that the file itself has mode 600. SSH refuses keys when these permissions are too open, and the failure message in the log is easy to miss. Fix permissions with the commands below if a key login fails for no clear reason.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
For a team, each person gets their own key and their own account. Never share one private key among several people, because you lose the ability to remove one person’s access without breaking access for everyone else. Keep a backup of your private key in a safe place, such as an encrypted drive, because a lost private key with passwords disabled means no way in except through the provider console.
Test the Change Without Locking Yourself Out
After you edit the config, test it with sshd -t to catch syntax errors, then restart the service. Do not close your current session while you test. Open a second terminal window and try a fresh login with your key. If the new login works, the change is safe.
sudo sshd -t
sudo systemctl restart ssh
ssh -i ~/.ssh/id_ed25519 user@your-server-ip
Then confirm passwords are really rejected. Try a login with password authentication forced, and expect a clear refusal rather than a password prompt.
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@your-server-ip
If the test login fails, your first session is still open, so you can fix the config and try again. Only close the old session after the new key login works. Admins who skip this step end up using the hosting provider rescue console, which is slow and, on some providers, costs money. Our guide on creating sudo users and disabling root login pairs well with this change, since key-only login plus a non-root sudo account removes the two easiest targets at once.
What Breaks When Password Login Goes Away
Some tools and habits stop working once passwords are off. File transfer clients that stored a server password will need a key configured. Scripts that used sshpass will fail, and that is a good thing, since a password stored in a script is a password waiting to leak. CI jobs that deploy over SSH need a deploy key with limited permissions, not a shared account password.
Password authentication also served as a fallback for people who lost keys. With it gone, your recovery path is the provider console, an out-of-band key, or a second admin account with its own key. Set one of those up before you need it, not after. Fail2ban still has value after this change, since it cuts log noise and blocks scanning, and our Fail2ban setup guide for SSH and Nginx shows how to keep it running alongside key-only login.
One more limit deserves a plain statement. Key-only login stops password guessing, but it does not stop a stolen private key. Protect the private key with a passphrase, keep it off shared machines, and remove the public key from the server the day someone leaves the team. A key without a passphrase on a stolen laptop is as good as a password written on a note.
Done in the right order, the change takes about ten minutes: key in place, config edited, service restarted, new login tested, old session closed. The server then ignores the password guessing that fills its logs every day, and the only way in is a key you control.