Sub topic 11
Troubleshooting when the key is not accepted
This chapter assumes you can generate a key pair, install the public key in ~/.ssh/authorized_keys, and have tried a first key-based login. It also assumes you know Unix permission bits and can read a log file. You still have a working password login or another open session on the server, so you can fix the problem without locking yourself out.
The working model
When you run ssh, the client lists the keys it is willing to use. For each key, the client sends the public half to the server and asks whether it would be accepted. If the server finds that key in the account's authorized_keys file, it sends a challenge. The client signs the challenge with the private key, and the server checks the signature.
A key login can fail at four places:
- The client never offers the right key.
- The server cannot read or trust the
authorized_keysfile. - The server finds no match, or the server's configuration forbids the login.
- The key is offered and found, but the signature or algorithm is not accepted.
Troubleshooting means working out which of these happened. The client's verbose output covers the first and last. The server log covers the middle two.
Step 1: read the client's verbose output
Force public key authentication only, and turn on verbose output. This is the command from the plan:
ssh -v -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]Forcing key-only authentication stops the client from falling back to a password prompt, so the real failure stays visible. Add -vv or -vvv for more detail. A failing run might contain these lines:
debug1: Will attempt key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:q3x...
debug1: Offering public key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:q3x...
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).Read the lines in this order:
- Will attempt key and Offering public key show which keys the client really tried. If your key is missing, the problem is on the client.
- Server accepts key means the server found the key in
authorized_keys. If you see this line and the login still fails, the problem is in the signature step or the account (see Step 4). - Authentications that can continue lists the methods the server allows. If
publickeyis absent, the server has public key authentication turned off.
Client-side causes
The wrong key is offered. If the log shows a different key than you expected, name the key explicitly with -i ~/.ssh/id_ed25519, or add an IdentityFile entry in ~/.ssh/config. Check that the Host block actually matches the name you typed. A block for myserver does not apply when you type ssh server.example.com.
Too many keys are offered. An agent holding many keys can use up the server's MaxAuthTries limit (default 6) before the right key is tried. The symptom is Too many authentication failures. Add IdentitiesOnly yes next to your IdentityFile line so the client offers only the named key.
The private key file is too open. If the private key is readable by others, the client refuses to use it:
WARNING: UNPROTECTED PRIVATE KEY FILE!
Permissions 0644 for '/home/alice/.ssh/id_ed25519' are too open.Fix it with chmod 600 ~/.ssh/id_ed25519.
The public and private keys do not match. This happens after overwriting one file but not the other. Print the public key that belongs to the private key, then compare fingerprints:
ssh-keygen -y -f ~/.ssh/id_ed25519 | ssh-keygen -lf -
ssh-keygen -lf ~/.ssh/id_ed25519.pubThe two fingerprints should be identical. If they differ, the .pub file you installed on the server belongs to a different private key. You will be asked for the passphrase when you run the first command.
The agent holds a stale key. Run ssh-add -l to list what the agent has loaded. If it says The agent has no identities, run ssh-add ~/.ssh/id_ed25519.
Step 2: read the server log
If the client offers the right key and the server does not accept it, the server almost always says why. Open a second session (or use your password login) and read the log.
# systemd systems, Debian and Ubuntu (service is "ssh")
sudo journalctl -u ssh -n 50 --no-pager
# systemd systems, RHEL and Fedora (service is "sshd")
sudo journalctl -u sshd -n 50 --no-pager
# Classic log files
sudo tail -n 50 /var/log/auth.log # Debian, Ubuntu
sudo tail -n 50 /var/log/secure # RHEL, FedoraThe messages you are most likely to see are:
| Log message | Meaning | Fix |
|---|---|---|
| `Authentication refused: bad ownership or modes for directory /home/alice` | The home directory is writable by group or others, or owned by someone else. | `chmod go-w /home/alice` and check the owner. |
| `Authentication refused: bad ownership or modes for file /home/alice/.ssh/authorized_keys` | The file or `~/.ssh` has loose permissions or the wrong owner. | `chmod 700 ~/.ssh`, `chmod 600 ~/.ssh/authorized_keys`, `chown -R alice: ~/.ssh`. |
| `Failed publickey for alice from 203.0.113.5` with no other detail | The key was offered but is not in `authorized_keys`, or the file is unreadable. | Compare fingerprints and inspect the file. |
| `User alice not allowed because not listed in AllowUsers` | `sshd_config` restricts which accounts can log in. | Add the user to `AllowUsers` or `AllowGroups`. |
| `User alice not allowed because account is locked` | The account is locked. | Unlock it with your distribution's account tools. |
| `userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms` | The server rejects the old SHA-1 RSA signature. | Use an Ed25519 key, or an RSA key that signs with SHA-2. |
Step 3: fix permissions and ownership
Strict permission checking is on by default (StrictModes yes). The rule covers the whole path, not only the key file. The home directory, ~/.ssh, and authorized_keys must all be owned by the user (or root) and must not be writable by group or others.
The namei command shows owner and mode for every directory along a path:
namei -l /home/alice/.ssh/authorized_keysThen apply the standard settings:
chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519If you created authorized_keys while logged in as root, ownership is wrong even when the mode is right. Run sudo chown -R alice: /home/alice/.ssh.
On systems that use SELinux, files copied into place can carry the wrong security label, and sshd is then denied access even with correct modes. Check with ls -Z ~/.ssh and restore the default labels:
restorecon -R -v ~/.sshStep 4: check the file contents and the server configuration
Check authorized_keys itself. Each key must be on exactly one line. A common error is a key broken across two lines by a terminal or an email client. Another is pasting the private key, or pasting only part of the public key. A valid line looks like this:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... alice@laptopList the fingerprints of every key in the file and compare them with the one your client offered:
ssh-keygen -lf ~/.ssh/authorized_keysA line that starts with options, such as from="10.0.0.0/8" or command="...", restricts when and how that key works. A from= pattern that does not match your address causes a rejection that looks like a missing key.
Check the effective server configuration. The file on disk may differ from what the running daemon uses, and Match blocks can override global settings. Print the effective values for your user:
sudo sshd -T -C user=alice,host=server.example.com,addr=203.0.113.5 | grep -Ei 'pubkeyauthentication|authorizedkeysfile|allowusers|allowgroups|pubkeyacceptedalgorithms'Look for three things. pubkeyauthentication must be yes. authorizedkeysfile shows where the server looks. By default it is .ssh/authorized_keys, but some systems point it to a central directory such as /etc/ssh/authorized_keys/%u. If allowusers or allowgroups appear, your account must match them.
After you edit sshd_config, validate and reload it:
sudo sshd -t && sudo systemctl reload sshdUse ssh instead of sshd as the service name on Debian and Ubuntu.
Run a temporary debug daemon. If the log is still unclear, start a second sshd in the foreground on another port. It prints every step of the authentication to your terminal, and your existing sessions are not affected:
sudo mkdir -p /run/sshd
sudo /usr/sbin/sshd -d -p 2222From the client, connect with ssh -v -p 2222 [email protected]. The daemon exits after one connection. Make sure any firewall allows port 2222 for this test, and remove the rule afterward.
A quick symptom table
| Symptom on the client | Most likely cause | First thing to check |
|---|---|---|
| Password prompt appears instead of key login | Key not offered or not accepted | `ssh -v` and look for `Offering public key` |
| `Permission denied (publickey)` | Key offered but rejected | Server log for `bad ownership or modes` |
| `Too many authentication failures` | Agent offered too many keys | Add `IdentitiesOnly yes` and `IdentityFile` |
| `UNPROTECTED PRIVATE KEY FILE` | Private key mode is too open | `chmod 600` on the private key |
| `Server accepts key` but login ends | Account, shell, or SELinux problem | `getent passwd alice` and `ls -Z ~/.ssh` |
| Prompts for a passphrase each time | Key not in the agent | `ssh-add -l`, then `ssh-add` |
Check that the account has a valid login shell with getent passwd alice. A shell such as /sbin/nologin accepts the key and then closes the session at once.
A repeatable routine
- Run
ssh -vwith key-only options. Confirm your key is offered. - If it is offered but not accepted, read the server log for the exact refusal.
- Check the path with
namei -land fix modes and ownership. - Compare fingerprints between the client's key and
authorized_keys. - Check the effective configuration with
sshd -T. - Change one thing at a time, and test after each change.
Exercise
Your ssh -v output shows Offering public key: /home/alice/.ssh/id_ed25519, then Authentications that can continue: publickey,password, and finally falls back to a password prompt. The server log says Authentication refused: bad ownership or modes for directory /home/alice. What is wrong, and what commands fix it?
Show answer
The key reached the server, but sshd refused to read authorized_keys because StrictModes found the home directory writable by group or others. The client never sees Server accepts key. Check the path with namei -l /home/alice/.ssh/authorized_keys, then run chmod go-w /home/alice. Also confirm that chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys are set, and that alice owns all three.
Exercise
A user has eight keys loaded in ssh-agent. Logging in to one server fails with Too many authentication failures, although the correct key is installed there. Why does this happen, and what two lines in ~/.ssh/config prevent it?
Show answer
The client offers agent keys one at a time, and each rejected key counts against the server's MaxAuthTries limit (default 6). The server disconnects before the correct key is tried. In the Host block for that server, set IdentityFile ~/.ssh/id_ed25519 (the correct key) and IdentitiesOnly yes, so the client offers only that key.