1. Check the current setting
This asks sshd what it is actually using, with defaults and included files resolved.no, you are done. If it says yes, continue.
A fresh Ubuntu install says
yes. Ubuntu ships the setting commented out, and
sshd enables password login when the setting is absent.2. Add your key and confirm it works
From your own computer. Replaceyouruser with your login name on the machine
and 1.2.3.4 with its IP address, here and in every command below.
3. Turn off password login
First save a copy of the config you have now.cp -n never overwrites, so it is
safe to run this more than once.
PasswordAuthentication no in the SSH config files that have it,
including the extra files Ubuntu keeps in /etc/ssh/sshd_config.d/.
Or edit the config yourself
Open/etc/ssh/sshd_config and set:
yes to no. Files in that folder are read first, so
they win over the main config.
4. Apply and verify
sshd -t checks the config for errors. The && means the restart only runs if
that check passes, so a broken config cannot take SSH down. Editing the config
changes nothing until you restart.
no, log in once more from your own computer to confirm, then
close your original session.
If it still says yes, most likely no config file had the setting. Add it,
then restart and check again:
Once the output is
passwordauthentication no, you are done. If your machine
was flagged for password login, the error clears within about two hours.If you are locked out
You need access that does not go through SSH. Use the machine’s IPMI, iDRAC, iLO, or other BMC console, or plug a monitor and keyboard into it. Then put back every copy you saved in step 3, and restart:/etc/ssh/sshd_config is not enough. The files in
/etc/ssh/sshd_config.d/ are read first, so they keep password login off until
they are restored too.
Log in with your password, fix your key, and start again at step 2. Password
login has to go back off before the machine will verify.