Sub topic 11
Troubleshooting When the SSH Key Is Not Accepted
Assume you can use a Linux or macOS command line, have logged in with an SSH password before, understand basic Unix permissions, and can use a text editor. You also need administrative access to the server or another way to inspect its logs.
A failed key login usually has one of four causes:
- The client is offering the wrong private key.
- The server has the wrong public key.
- SSH refuses the key file because of ownership or permissions.
- The server configuration does not allow public key authentication.
Troubleshoot from the client to the server. Change one thing at a time.
Confirm the account and key
First, make sure the connection uses the intended account and host:
ssh -i ~/.ssh/id_ed25519 [email protected]The public key must be in the home directory of alice, not in the directory of another user. On the server, check the installed key:
grep -n 'ssh-ed25519' ~/.ssh/authorized_keysThe public key is one long line. It normally begins with ssh-ed25519 or ssh-rsa and ends with a comment. Do not wrap it across multiple lines or add extra characters.
Compare the public key derived from the private key with the key in authorized_keys:
ssh-keygen -y -f ~/.ssh/id_ed25519This prints the public key material. Compare it with the key stored on the server. Do not copy the private key to the server for comparison.
Read the client's verbose output
Use verbose mode to see which identities SSH loads and offers:
ssh -vvv -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 [email protected]Look for messages such as:
Offering public key: the client is offering a key.Server accepts key: the server accepted the key and should request the private-key signature.Permission denied (publickey): authentication failed after the available methods were tried.identity file ... type -1: the specified key file does not exist or cannot be read.
If the client offers many keys, an SSH agent may be supplying unexpected identities. IdentitiesOnly=yes limits this connection to the key named with -i.
Check permissions and ownership
The SSH server, sshd, may reject keys when the home directory or SSH files can be modified by another user. Log in with a password or use the server console, then check:
ls -ld "$HOME" "$HOME/.ssh"
ls -l "$HOME/.ssh/authorized_keys"Typical safe permissions are:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R "$USER:$USER" ~/.sshThe home directory itself must also belong to the account. Avoid running these commands with sudo unless you preserve the correct owner. A root-owned authorized_keys file can prevent the account from using it.
Keep an existing administrative session open while troubleshooting. Do not disable password authentication until a separate terminal successfully logs in with the key.Inspect server logs
The server log usually gives a more specific reason than the client. On systems using systemd, try:
sudo journalctl -u sshd -b --no-pager -n 100On Debian and Ubuntu, the service may be named ssh:
sudo journalctl -u ssh -b --no-pager -n 100Common messages identify bad ownership, an unreadable authorized_keys file, a disabled authentication method, or a rejected key algorithm. If the server uses SELinux, incorrect security labels can also block access. An administrator can inspect them with:
ls -Zd ~/.ssh ~/.ssh/authorized_keysCheck effective SSH configuration
The server may have settings that override the lines you edited. Test the configuration before reloading it:
sudo sshd -tReview settings related to keys and the account:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
AllowUsers aliceThe AllowUsers, DenyUsers, group rules, or a Match block may prevent the login even when the key is correct. On the client, inspect the effective configuration for a host alias:
ssh -G myserver | grep -E '^(user|hostname|identityfile|identitiesonly) 'This reveals whether ~/.ssh/config selects the expected user, hostname, and identity file.
Exercise
Verbose output shows the client offers ~/.ssh/id_ed25519, but the server rejects it immediately. Name two server-side checks to perform.
Show answer
Check that the matching public key is one complete line in the correct user's ~/.ssh/authorized_keys, then check ownership and permissions on the home directory, .ssh, and authorized_keys.