When SSH rejects a public key, it’s tempting to change file permissions or restart sshd immediately. Both can waste time—and careless changes can lock you out. A better approach is to trace the authentication path: confirm the account and key file, inspect permissions, then verify which key the client offers.

Start with the account and file

authorized_keys is a server-side list of public keys allowed to authenticate as one particular user. A key listed for deploy does not automatically work for root or ubuntu. The private key stays on the client; never put it in authorized_keys.

On the server, check the account you intend to log in as:

whoami printf 'Home: %s\n' "$HOME" ls -ld "$HOME" "$HOME/.ssh" ls -l "$HOME/.ssh/authorized_keys" 

Run these commands as the target account, or otherwise verify that you’re inspecting that account’s actual home directory. Don’t assume the home is /home/username; cloud images and system accounts can use different names or paths.

A normal entry contains a key type, the complete public-key data, and optionally a comment, all on one physical line:

ssh-ed25519 AAAA... alice@laptop 

That abbreviated line is only an illustration, not a usable key. Copy the entire contents of the real .pub file. Each additional key goes on its own line. Comments are labels, not part of the security identity.

If the file doesn’t exist, that doesn’t necessarily mean the SSH service is broken. The account might not have file-based key access configured, or the server may use a different key source.

Check ownership and the whole permission path

A common starting point on Unix-like servers is mode 700 for ~/.ssh and 600 for authorized_keys, with both owned by the login user. Check the path, not just the final file:

stat -c '%a %U:%G %n' "$HOME/.ssh" "$HOME/.ssh/authorized_keys" namei -l "$HOME/.ssh/authorized_keys" 

With StrictModes enabled, OpenSSH checks for unsafe ownership and write permissions in the relevant path. 600 is a conservative setting, but it isn’t a universal requirement that the file be unreadable by everyone else: a correctly owned 644 file may be accepted if unauthorized users cannot write it. The home directory also must not be writable by unauthorized users.

If you’ve confirmed the path and own the account, typical fixes are:

chmod 700 "$HOME/.ssh" chmod 600 "$HOME/.ssh/authorized_keys" 

Avoid recursive chmod or chown commands across a home directory. They can alter unrelated files. If ownership is wrong, have an administrator correct the specific affected path.

For a refresher on what these modes mean, see how chmod 600 applies to SSH key files.

Confirm the server is reading that file

The daemon can be configured to use a different AuthorizedKeysFile, an AuthorizedKeysCommand, or account-specific Match rules. On Debian or Ubuntu, inspect the effective server settings:

sudo /usr/sbin/sshd -T | grep -E '^(authorizedkeysfile|authorizedkeyscommand|strictmodes|pubkeyauthentication) ' 

A generic sshd -T output may not include connection-specific Match conditions. To evaluate settings for a particular incoming connection, supply the relevant user, host, and source address with -C:

sudo /usr/sbin/sshd -T -C user=deploy,host=client.example,addr=192.0.2.25 | grep -E '^(authorizedkeysfile|strictmodes|pubkeyauthentication) ' 

The example address is reserved for documentation; use the actual connection details when investigating. Don’t change server configuration just to test a theory if you lack fallback access.

Verify what the client offers

A valid server file won’t help if the client offers a different private key. From the client, explicitly select the paired private key and enable verbose output:

ssh -vv -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 user@server 

Look for the expected identity being offered, then whether the server accepts it. IdentitiesOnly=yes helps make the test specific instead of letting other configured or agent-loaded identities confuse the result.

If you need to check which private key a host configuration selects, SSH IdentityFile examples can help. You can also compare fingerprints without printing full key contents:

ssh-keygen -lf ~/.ssh/id_ed25519.pub 

On the server, where supported, compare against the entries in authorized_keys:

ssh-keygen -lf ~/.ssh/authorized_keys 

A fingerprint mismatch means you’re comparing different keys. If the expected key is offered but rejected, check the target username, effective key-file path, file ownership and any restrictions on the key.

Use server logs to narrow the cause

Permission denied (publickey) tells you authentication failed; by itself, it does not tell you why. Check the server’s authentication logs around the time of a fresh attempt:

sudo journalctl -u ssh -u sshd --since '10 minutes ago' --no-pager 

On Debian or Ubuntu systems using a traditional authentication log, you can also check:

sudo tail -n 60 /var/log/auth.log 

Log locations vary. Look for evidence such as bad ownership, an unacceptable key, a denied account, or a failed public-key check. Avoid disabling StrictModes or other security protections as a shortcut.

Add or remove keys carefully

If you still have a working login, ssh-copy-id can append a public key from a Linux or macOS client:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server 

The -i argument points to the public key. Keep your existing session open and test a new connection before removing another access method. When editing authorized_keys manually, append the new line—don’t overwrite the file and erase keys other users or automation depend on.

To revoke a key, identify its fingerprint, back up the file, and remove only the matching entry. Changes to authorized_keys apply to new authentication attempts; adding or removing an entry does not require restarting sshd, and removing a key does not terminate sessions already logged in.

A reliable troubleshooting order is simple: verify the account and path, inspect ownership and permissions, check the server’s effective configuration, then confirm the client’s offered key and consult server logs. That sequence fixes the common causes without weakening SSH security or turning a recoverable login problem into a lockout.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.