Sub topic 12
Disabling Password Logins on the Server
Assume you can use the Linux command line, log in to the server with SSH, edit a configuration file, and read basic Unix permissions. Before disabling passwords, you must confirm that key-based login works in a separate terminal. Keep your current SSH session open until the new settings are tested.
“Passwordless SSH” usually means that the server accepts your private key instead of an account password. If your private key has a passphrase, you still enter that passphrase locally. The passphrase protects the key file and never travels to the server.
Why disable password authentication?
Password authentication lets an attacker repeatedly guess a password against an exposed SSH service. Strong passwords reduce this risk, but they can still be phished, reused, or attacked with automated guesses.
With public key authentication:
- The server stores only your public key.
- Your private key remains on your computer.
- The SSH client proves it has the private key without sending the key itself.
- A passphrase can protect the private key if the computer is lost.
Disabling passwords also prevents accidental fallback to password authentication when a client offers the wrong key.
Check key login before changing the server
Open a second terminal and connect using the key:
ssh -i ~/.ssh/id_ed25519 [email protected]If you use an SSH configuration alias, test that instead:
ssh myserverConfirm that the session opens without asking for the server account password. A prompt for your private-key passphrase is expected.
The following sequence is safe because the existing session remains available if the new login fails:
flowchart TD
A[Keep current SSH session open] --> B[Test key login in a second terminal]
B --> C[Validate sshd configuration]
C --> D[Reload SSH service]
D --> E[Test a new key login]
E --> F[Disable password authentication]
F --> G[Validate and reload again]
G --> H[Test another new login]Set the server policy
The SSH server reads its settings from /etc/ssh/sshd_config. Some systems also load files from /etc/ssh/sshd_config.d/.
Edit the main configuration file as root:
sudo nano /etc/ssh/sshd_configSet these options:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication noPasswordAuthentication no disables ordinary password authentication. KbdInteractiveAuthentication no disables interactive prompts that could otherwise provide another password-based route. On older OpenSSH versions, the related option may be named ChallengeResponseAuthentication.
Do not disable PubkeyAuthentication. Also check that the account's public key is in the correct user's file:
/home/alice/.ssh/authorized_keysA configuration file can contain an option more than once. OpenSSH uses the first value it finds for many settings, so remove or update conflicting entries rather than assuming the last line wins.
Validate before reloading
Check the syntax before asking the running server to reload:
sudo sshd -tNo output and an exit status of zero indicate that the syntax is valid. You can inspect the effective settings with:
sudo sshd -T | grep -E '^(pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication) 'Expected output includes:
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication noReload the service without disconnecting existing sessions:
sudo systemctl reload sshdOn Debian and Ubuntu, the service is commonly named ssh:
sudo systemctl reload sshIf the reload fails, existing connections usually remain active. Fix the configuration, run sshd -t again, and reload only after validation succeeds.
Test and recover
From the second terminal, force a key-only connection:
ssh -o PreferredAuthentications=publickey \
-o PasswordAuthentication=no \
[email protected]If it succeeds, the server is accepting your key without a password fallback. If it fails, inspect client details:
ssh -v -o PreferredAuthentications=publickey \
-o PasswordAuthentication=no \
[email protected]Look for the identity file offered by the client and the server's rejection message. On the server, review recent SSH messages:
sudo journalctl -u sshd -n 50 --no-pagerUse -u ssh on systems where the service is named ssh.
| Symptom | Likely cause |
|---|---|
| `Permission denied (publickey)` | Wrong key, missing `authorized_keys`, or incorrect ownership or permissions |
| The client never offers your key | Wrong `IdentityFile`, an inactive `ssh-agent`, or `IdentitiesOnly` configuration |
| Configuration test fails | A spelling error, invalid value, or unsupported option |
| Existing login works but new login fails | The reload applied a policy that blocks the new authentication method |
Exercise
Exercise
You have confirmed that ssh myserver works with your key. Which command should you run before reloading SSH after setting PasswordAuthentication no, and why?
Show answer
Run sudo sshd -t. It checks the SSH server configuration for syntax errors without changing the running service, so you can correct mistakes before applying the new policy.