Chapter 1 · Lesson 13 of 19
Server basics

Disable SSH password login on Ubuntu

Disabling SSH password login makes the server accept only key-based logins, which blocks password guessing attacks.

By the end you will

Turn off SSH password and root login on Ubuntu and confirm only keys are accepted.

Time
15 min
Level
Intermediate
Check
4 questions
You need
  • 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:

SettingEffect
PasswordAuthentication noThe server will not accept account passwords over SSH
PermitRootLogin noRoot cannot log in over SSH at all
PermitRootLogin prohibit-passwordRoot 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#

bash
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#

bash
sudo nano /etc/ssh/sshd_config

Set these lines (remove any leading #):

output
PasswordAuthentication no
PermitRootLogin no

Use 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#

bash
sudo sshd -t

What 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:

bash
sudo systemctl reload ssh

What 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:

bash
ssh -o PubkeyAuthentication=no deploy@203.0.113.10

You 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:

bash
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 -T is not what you set, search sshd_config.d again 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 KbdInteractiveAuthentication is yes and PAM is on, a password prompt may still appear through that path. The Ubuntu default is no. sshd -T shows 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 Match lines in the config files. Settings under a Match block 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 the Port, the socket may need sudo systemctl daemon-reload and a restart of ssh.socket. A plain reload ssh is 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#

SymptomLikely causeFix
Unit sshd.service not foundUbuntu names the service sshUse sudo systemctl reload ssh
Still asked for a passwordA file in sshd_config.d sets yes, or you did not reloadRun the grep from step 1, fix it, reload, check sshd -T
Permission denied (publickey) for your own keyKey not in authorized_keys, wrong user, or wrong permissionsCheck the user name and that ~/.ssh is mode 700 and authorized_keys is 600
sshd -t prints Bad configuration optionTypo in an option nameFix the line it names, then run sshd -t again
Missing privilege separation directoryThe runtime directory is missing, often when running sshd by handReload through systemd instead of starting sshd manually
Locked out completelyPassword login off and no working keyLog in through the provider web console and revert the change or add your key
Permission denied (publickey,password) in the verbose outputThe server still lists password as a methodCheck sshd -T and drop-in files, then reload
Connection closed by ... port 22 right after the changesshd rejected the new config or you were bannedCheck 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#

TaskCommand
Find overridessudo grep -ri PasswordAuthentication /etc/ssh/
Test syntaxsudo sshd -t
Applysudo systemctl reload ssh
Show effective valuessudo sshd -T
Prove passwords are refusedssh -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 -t before every reload.

Next#

Reduce brute-force noise with Protect SSH with fail2ban.

Check what you learned4 questions
1You set PasswordAuthentication no but passwords still work. What is the likely cause?
2What does sudo sshd -t do?
3Why keep your current session open while you reload SSH?
4Which command shows the value sshd actually uses, after all config files are read?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios