Symptoms
When attempting to connect to a Linux/Unix server via SSH using public key authentication, you receive one of the following errors:
Permission denied (publickey)Permission denied (publickey,gssapi-keyex,gssapi-with-mic)Authentication failed. Permission denied.
The connection fails even though the correct key pair is in place. Debug output (ssh -vvv user@host) often shows the server rejecting the offered key.
Root Causes
The most common causes are:
- Incorrect file permissions on the
~/.sshdirectory or theauthorized_keysfile on the server. - Wrong ownership of the
.sshdirectory or its contents. - Misconfigured
sshd_configdisabling public key authentication or restricting key usage. - Incorrect key format or missing public key in
authorized_keys. - SELinux or AppArmor blocking SSH key access.
Step-by-Step Fix
1. Verify Key Pair
Ensure your private key (~/.ssh/id_rsa or similar) is present on the client and the corresponding public key is appended to ~/.ssh/authorized_keys on the server. Check the public key file on the server:
cat ~/.ssh/authorized_keysIf the file doesn't exist, create it and paste your public key:
echo 'ssh-rsa AAAAB3NzaC1yc2E...' >> ~/.ssh/authorized_keys2. Set Correct Permissions on Server
Log in to the server (via console or another method) and run:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts # if existsAlso ensure the home directory is not writable by group or others:
chmod go-w ~3. Set Correct Ownership
The .ssh directory and its contents must be owned by the user, not root:
chown -R $(whoami):$(whoami) ~/.ssh4. Check SSH Server Configuration
Edit /etc/ssh/sshd_config and verify these lines are present and uncommented:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no # optional, for securityThen restart SSH service:
sudo systemctl restart sshd5. Debug with Verbose Mode
On the client, run:
ssh -vvv user@serverLook for lines like:
debug1: Offering public key: /home/user/.ssh/id_rsa— client is offering the key.debug2: we sent a publickey packet, wait for replydebug1: Authentication succeeded (publickey)— success.debug1: Authentications that can continue: publickey— server still waiting for valid key.
If the server rejects the key, check the server logs (/var/log/auth.log or /var/log/secure) for details like Authentication refused: bad permissions.
Alternative Fixes
Check SELinux Context
If SELinux is enforcing, restore default contexts:
restorecon -Rv ~/.sshUse StrictModes
If you want to bypass permission checks temporarily (not recommended for production), set StrictModes no in sshd_config. But fix permissions instead.
Verify Key Fingerprint
On the server, check the fingerprint of the stored key:
ssh-keygen -lf ~/.ssh/authorized_keysCompare with your client's public key fingerprint:
ssh-keygen -lf ~/.ssh/id_rsa.pubPrevention
- Always use
ssh-copy-id user@serverto automatically set correct permissions. - Never set
StrictModes noin production. - Regularly audit
~/.sshpermissions withstatorls -la. - Use a configuration management tool (Ansible, Puppet) to enforce permissions.
- Keep SSH server updated and follow security best practices.
By following these steps, you should be able to resolve SSH permission denied errors and restore key-based authentication.