Disable SSH password login on Ubuntu
Disabling SSH password login makes the server accept only key-based logins, which blocks password guessing attacks.
- Updated Nov 27, 2026
- Markdown
Turn off SSH password and root login on Ubuntu and confirm only keys are accepted.
- A sudo user that already logs in with an SSH key
- A second terminal to test with
Disabling SSH password login means the server accepts only keys. Bots try common passwords against port 22 on every public server, all day. With password login off, those guesses cannot succeed, because the server accepts only a private key that the attacker does not have. The change takes two lines, but it is also the easiest way to lock yourself out of a server, so the order of the steps matters.
How it works#
sshd reads /etc/ssh/sshd_config. On Ubuntu, that file starts with an Include /etc/ssh/sshd_config.d/*.conf line, so the drop-in files are read first. The rule for most options is that the first value sshd reads wins. A later line that says something different is ignored.
This explains the classic trap. Cloud images often ship a file such as 50-cloud-init.conf containing PasswordAuthentication yes. You edit the main file, set no, reload, and passwords still work, because the drop-in was read first and its yes won.
Two settings do the work:
| Setting | Effect |
|---|---|
PasswordAuthentication no | The server will not accept account passwords over SSH |
PermitRootLogin no | Root cannot log in over SSH at all |
PermitRootLogin prohibit-password | Root can log in, but only with a key |
You can check what sshd really applies with sshd -T, which prints the effective configuration after all includes are read.
Warning: do this only after key login works for a sudo user (see Log in with SSH keys). A mistake here can lock you out. Keep one SSH session open for the whole process.
Step 1: Check for overriding files#
sudo grep -ri "PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/What you should see: one line per match, with the file name in front. Lines starting with # are comments and do nothing. If a file such as /etc/ssh/sshd_config.d/50-cloud-init.conf has PasswordAuthentication yes, change that line to no there too.
Step 2: Edit the settings#
sudo nano /etc/ssh/sshd_configSet these lines (remove any leading #):
PasswordAuthentication no
PermitRootLogin noUse PermitRootLogin prohibit-password instead if you still need root with a key. Save with Ctrl+O, exit with Ctrl+X.
Step 3: Test the config#
sudo sshd -tWhat you should see: no output at all. Silence means the syntax is valid. If it prints an error with a line number, fix that line before going on. Reloading a broken config can leave the server unable to accept new logins.
Step 4: Reload SSH#
On Ubuntu the service is called ssh, not sshd:
sudo systemctl reload sshWhat you should see: no output. Do not close your existing session. A reload keeps current sessions alive and applies the new settings to new ones.
Check it worked#
Open a second terminal and log in with your key. It should work. Then try a password-only login:
ssh -o PubkeyAuthentication=no deploy@203.0.113.10You should see Permission denied (publickey) and no password prompt. The flag turns off key use for this one attempt, so it proves the server would not take a password.
On the server, check the effective value:
sudo sshd -T | grep -iE 'passwordauthentication|permitrootlogin'What you should see: passwordauthentication no and the root setting you chose, in lower case.
Edge cases and gotchas#
- Drop-in files win over the main file. Remember that the first value read is used. If the effective value from
sshd -Tis not what you set, searchsshd_config.dagain and also check that the file ends in.conf, since other names are not included. - A new user created later can make you think it failed. If you test with a user that has no key installed, you get
Permission denied (publickey)for the right reason but cannot tell it from a broken setup. Test with a user you know has a key first. - Keyboard-interactive can still allow password-like prompts. If
KbdInteractiveAuthenticationisyesand PAM is on, a password prompt may still appear through that path. The Ubuntu default isno.sshd -Tshows the current value. - A passphrase on your key is not a server password. Being asked for a passphrase locally is normal and happens on your computer, not on the server.
- Match blocks can re-enable passwords for some users or addresses. Look for
Matchlines in the config files. Settings under aMatchblock apply only when the condition fits. - Socket activation and ports. On Ubuntu 22.10 and later, sshd may be started through
ssh.socket. If you also change thePort, the socket may needsudo systemctl daemon-reloadand a restart ofssh.socket. A plainreload sshis enough for the settings in this lesson. - Automation breaks. Scripts, CI jobs and backups that used a password stop working. Give each its own key first.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Unit sshd.service not found | Ubuntu names the service ssh | Use sudo systemctl reload ssh |
| Still asked for a password | A file in sshd_config.d sets yes, or you did not reload | Run the grep from step 1, fix it, reload, check sshd -T |
Permission denied (publickey) for your own key | Key not in authorized_keys, wrong user, or wrong permissions | Check the user name and that ~/.ssh is mode 700 and authorized_keys is 600 |
sshd -t prints Bad configuration option | Typo in an option name | Fix the line it names, then run sshd -t again |
Missing privilege separation directory | The runtime directory is missing, often when running sshd by hand | Reload through systemd instead of starting sshd manually |
| Locked out completely | Password login off and no working key | Log in through the provider web console and revert the change or add your key |
Permission denied (publickey,password) in the verbose output | The server still lists password as a method | Check sshd -T and drop-in files, then reload |
Connection closed by ... port 22 right after the change | sshd rejected the new config or you were banned | Check sudo journalctl -u ssh and your fail2ban status |
Real-world scenario#
You have a server where you log in as deploy with a key. A scanner has been guessing passwords for days, so you decide to turn passwords off.
You run the grep from step 1 and find PasswordAuthentication yes in /etc/ssh/sshd_config.d/50-cloud-init.conf. You change it to no, set the same in the main file, run sudo sshd -t (no output) and sudo systemctl reload ssh.
In a second terminal, ssh -o PubkeyAuthentication=no deploy@203.0.113.10 now ends with Permission denied (publickey). Your normal key login still works. Only then do you close the first session.
Quick reference#
| Task | Command |
|---|---|
| Find overrides | sudo grep -ri PasswordAuthentication /etc/ssh/ |
| Test syntax | sudo sshd -t |
| Apply | sudo systemctl reload ssh |
| Show effective values | sudo sshd -T |
| Prove passwords are refused | ssh -o PubkeyAuthentication=no user@host |
Security and safety notes#
- Do not close your working session until a second login with your key succeeds.
- Keep a backup of your private key. Without it, only the provider console gets you back in.
- Run
sudo sshd -tbefore every reload.
Related lessons#
- Log in with SSH keys: set up the key first
- Create a sudo user: you need a non-root account before closing root
- Protect SSH with fail2ban: ban the addresses that keep trying
- Initial server setup checklist: where this step fits
Next#
Reduce brute-force noise with Protect SSH with fail2ban.
Check what you learned
Frequently asked questions
Is it safe to disable root login too?
Yes, once you have a sudo user that logs in with a key. PermitRootLogin prohibit-password is a middle option that allows root only with a key.
Why did my change not take effect?
Ubuntu reads files in /etc/ssh/sshd_config.d/ first, and the first value found wins. A file such as 50-cloud-init.conf can set PasswordAuthentication yes and override your edit.
What if I lose my private key after disabling passwords?
You cannot log in over SSH. Use your provider's web console or rescue mode to get a shell and add a new public key. Keep a backup of your key in a safe place.
Will this stop me using sudo with a password?
No. This setting only controls how SSH accepts logins. Your user still types its password for sudo unless you configured otherwise.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.