Home UbuntuDisable SSH Password Login on Ubuntu 24.04: Key-Only Access

Disable SSH Password Login on Ubuntu 24.04: Key-Only Access

by Knowledge Base Linux
Server secured with key-only access after you disable SSH password login Ubuntu authentication

Password authentication is the weakest link in most SSH setups. Bots scan the internet around the clock, trying common usernames and passwords against port 22, and a single weak or reused password can hand an attacker a shell on your server. If you want to disable SSH password login Ubuntu administrators rely on for hardening, switching to key-only access is one of the highest-impact changes you can make. This guide shows you how to disable SSH password login Ubuntu 24.04 servers safely, without locking yourself out, and how to verify the change before you close your last session.

SSH key authentication diagram showing how to disable SSH password login Ubuntu access safely

When you disable SSH password login Ubuntu systems stop accepting password guesses entirely. Only clients that present a private key matching an authorized public key can connect. That single change removes brute-force password attacks from your threat model, even if an attacker knows a valid username. It also forces a better workflow: keys are longer, harder to guess, and easier to rotate or revoke per device than shared passwords.

Prerequisites

Before you begin, make sure you have the following in place:

  • An Ubuntu 24.04 server with root or sudo access.
  • An SSH key pair already generated on your local machine. If you have not created one yet, follow our guide on how to generate SSH keys on Linux first.
  • Your public key already copied to the server, so you can log in with the key today.
  • A second, already-open SSH session to the server, kept open while you make changes. This is your safety net: if something goes wrong, you can fix it from the existing session instead of being locked out.

Never disable password authentication before you have confirmed key-based login works. When you disable SSH password login Ubuntu hardening goes smoothly only if that order is respected; skipping it is what turns a routine task into an emergency console recovery.

Disable SSH Password Login Ubuntu 24.04 Step by Step

The steps below edit the OpenSSH server configuration, test it, and then apply it. Ubuntu 24.04 ships OpenSSH 9.x, where the main configuration file is /etc/ssh/sshd_config, with additional drop-in files under /etc/ssh/sshd_config.d/. Drop-in files can override the main file, so always check them first.

Step 1: Confirm key-based login works

From your local machine, log in using your key explicitly and confirm you are not prompted for a password:

ssh -i ~/.ssh/id_ed25519 username@your-server-ip
whoami

If you are asked for a password, your key is not yet authorized. Copy it with ssh-copy-id and try again before continuing:

ssh-copy-id -i ~/.ssh/id_ed25519.pub username@your-server-ip
ssh -i ~/.ssh/id_ed25519 username@your-server-ip

Step 2: Inspect the current SSH configuration

Check both the main file and any drop-in overrides so a later file does not silently re-enable passwords:

sudo grep -R -i -n "PasswordAuthentication\|ChallengeResponseAuthentication\|KbdInteractiveAuthentication\|UsePAM" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
sudo sshd -T | grep -i -E "passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication"

The sshd -T output shows the effective configuration after all files are merged. If it reports passwordauthentication yes, something is still enabling it, and you need to find that line before you proceed.

Step 3: Disable password authentication

Open the main configuration file in an editor:

sudo nano /etc/ssh/sshd_config

Find or add the following lines. Setting both password and keyboard-interactive authentication to no closes the two common password paths:

PasswordAuthentication no
ChallengeResponseAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

Note that on newer OpenSSH versions, ChallengeResponseAuthentication is an alias for KbdInteractiveAuthentication, but setting both is harmless and keeps the intent clear. Leave PubkeyAuthentication set to yes explicitly so there is no doubt about the intended login method. These lines are the core of any plan to disable SSH password login Ubuntu administrators deploy in production.

If a drop-in file such as 60-cloudimg-settings.conf sets PasswordAuthentication yes, edit or remove that override as well. The first match in the merged configuration wins for many options, and files in sshd_config.d/ are read before the main file, so an override there will beat your change.

Step 4: Validate the configuration and restart SSH

Always validate before restarting. A syntax error in sshd_config can prevent the service from starting:

sudo sshd -t
sudo systemctl restart ssh
sudo systemctl status ssh --no-pager

On Ubuntu 24.04 the service is named ssh, with sshd kept as an alias. If sshd -t reports no output, the configuration is valid, and your attempt to disable SSH password login Ubuntu access is ready for live testing.

Step 5: Verify you can disable SSH password login Ubuntu access without lockout

Now test from a NEW terminal window, while keeping your original session open. First, confirm key login still works:

ssh -i ~/.ssh/id_ed25519 username@your-server-ip

Then confirm password login is rejected. Try forcing password authentication:

ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no username@your-server-ip

You should see a Permission denied (publickey) message. That is the expected result: the server no longer offers password authentication at all. Only after both tests pass should you close your original safety-net session.

Optional Hardening After You Disable Password Authentication

Key-only access is a major improvement, but a few companion settings make the setup stronger still. Consider disabling root login entirely, since attackers already know the root username:

PermitRootLogin no

You can also restrict SSH to specific users, change the default port to reduce log noise, and enable a firewall rule that only allows SSH from known addresses. Once you disable SSH password login Ubuntu brute-force noise in your auth logs should drop sharply. For a complete reference on the available options, see the official OpenSSH manual and the Ubuntu Server OpenSSH documentation. Each change should be followed by sshd -t and a fresh login test, just as above.

Protect your private keys as carefully as you protected your passwords. Use a passphrase on the key itself, store backups of the key in an encrypted location, and remove old public keys from ~/.ssh/authorized_keys when a laptop is replaced or a team member leaves. A key without a passphrase on a stolen laptop is nearly as bad as a weak password.

Troubleshooting

Locked out after the change. If you still have your safety-net session open, revert the setting temporarily, restart SSH, and re-check that your key is in the right authorized_keys file for the right user. If you have no open session, use your cloud provider console access or recovery console to edit /etc/ssh/sshd_config and restart SSH. This is the main risk when you disable SSH password login Ubuntu servers for the first time, and it is exactly why the safety-net session matters.

Password login still works. Run sshd -T | grep -i passwordauthentication. If it still says yes, a drop-in file or an included file is overriding your change. Search the whole /etc/ssh/ directory and fix the overriding line.

Key login fails with “Permission denied (publickey)”. Check file permissions: ~/.ssh must be 700 and authorized_keys must be 600, both owned by the login user. Also check /var/log/auth.log on the server for the exact reason OpenSSH rejected the key.

SSH service fails to restart. Run sudo sshd -t and read the error. It usually names the bad line and line number. Fix it, validate again, and then restart.

Conclusion

Learning how to disable SSH password login Ubuntu servers use in production is a fundamental sysadmin skill, and it takes only a few minutes once key authentication is working. The recipe is simple: confirm key login first, set PasswordAuthentication no and KbdInteractiveAuthentication no, check for drop-in overrides, validate with sshd -t, and verify from a new session before closing the old one. Done in that order, you remove an entire class of brute-force attacks without any risk of locking yourself out. Apply the same steps to every server you manage, and make key-only SSH part of your standard server setup checklist. If you disable SSH password login Ubuntu hosts one at a time, testing each as you go, you can harden a whole fleet in an afternoon.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.