Fix SSH Permission Denied with Public Key Authentication
SSH public key authentication fails with 'Permission denied (publickey)' due to incorrect file permissions, wrong key placement, or SSH config issues. This guide covers diagnosis and resolution.
Symptoms
When attempting to connect to a remote Linux/Unix server via SSH using public key authentication, the connection fails with the error message: Permission denied (publickey). The client may also see: Authentication failed or No supported authentication methods available. This occurs even though the public key has been copied to the server's ~/.ssh/authorized_keys file.
Root Causes
The most common causes are:
- Incorrect file permissions on the
~/.sshdirectory,authorized_keysfile, or the user's home directory. - Wrong key location – the public key is not appended to the correct
authorized_keysfile for the target user. - SSH daemon configuration –
sshd_configmay havePubkeyAuthentication noor other restrictive settings. - SELinux or AppArmor blocking SSH key access.
- Key format issues – the public key may be corrupted or in an unsupported format.
Step-by-Step Fix
1. Verify SSH Key Pair
Ensure you have a valid key pair on the client machine. Check for id_rsa (private) and id_rsa.pub (public) in ~/.ssh/. If missing, generate a new pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"2. Copy Public Key Correctly
Use ssh-copy-id to safely copy the public key to the server:
ssh-copy-id user@remote_serverIf not available, manually append the key:
cat ~/.ssh/id_rsa.pub | ssh user@remote_server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"3. Fix Permissions on Server
Log into the server (using password or console) and set correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 755 ~Ensure the home directory is not writable by group or others.
4. Check SSH Daemon Configuration
Edit /etc/ssh/sshd_config on the server. Verify these settings:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no (optional, for security)
ChallengeResponseAuthentication noRestart SSH service:
sudo systemctl restart sshd5. Check SELinux/AppArmor
If SELinux is enforcing, restore contexts:
restorecon -Rv ~/.sshFor AppArmor, check logs with journalctl and adjust profiles if needed.
6. Enable Verbose Logging
Run SSH with -vvv to see detailed debug output:
ssh -vvv user@remote_serverLook for lines like debug1: Authentication refused: bad permissions or debug1: Offering public key.
Alternative Fixes
- Use different key type – try Ed25519 (
ssh-keygen -t ed25519). - Check home directory permissions – must not be group-writable.
- Verify key in authorized_keys – ensure no extra whitespace or line breaks.
- Disable SELinux temporarily (for testing):
sudo setenforce 0.
Prevention
- Always use
ssh-copy-idto transfer public keys. - Set proper permissions immediately after creating
.sshdirectory. - Regularly audit
sshd_configfor security best practices. - Use key passphrases and consider SSH agent for convenience.
- Monitor authentication logs with
journalctl -u sshdor/var/log/auth.log.
By following these steps, you can resolve SSH public key permission denied errors and ensure secure, passwordless authentication.
Was this solution helpful?