Connecting to a remote Linux server or cloud instance using Secure Shell (SSH) only to be rejected with the error: ‘Permission denied (publickey)’ or ‘Permission denied (publickey,gssapi-keyex,gssapi-with-mic)’ prevents terminal administration. Public key authentication is the industry standard for securing remote Linux infrastructure. When an SSH client attempts to connect, the OpenSSH server evaluates the client’s cryptographic signature against keys listed in the user’s authorized_keys file. When this handshake fails, the server closes the connection immediately. In Linux, this failure is typically caused by incorrect directory permissions, unassigned SSH keys, or server-side configuration restrictions.
Why Does SSH Report ‘Permission denied (publickey)’?
The OpenSSH daemon (sshd) enforces strict security checks before accepting a public key. Common causes of rejection include:
- Overly Permissive File Permissions: OpenSSH strictly rejects keys if the remote
~/.sshdirectory orauthorized_keysfile is writable by group members or other users on the system. - Missing Private Key in SSH Agent: The local SSH client fails to offer the required private key because it is not loaded into
ssh-agentor lacks default file naming (e.g.id_ed25519orid_rsa). - Public Key Missing from Server authorized_keys: The public key corresponding to your private key has not been correctly appended to the target user’s
~/.ssh/authorized_keysfile. - Disabled Public Key Authentication on Server: The server’s
/etc/ssh/sshd_configfile hasPubkeyAuthenticationset tono. - SELinux or AppArmor Security Denials: Mandatory access control labels on the remote server preventing the
sshddaemon from reading the user’s authorized_keys file.
Step 1: Debug the Connection with Verbose Output
Running SSH with verbose flags exposes the exact authentication methods negotiated and which keys your client offers.
- In your local terminal, run the SSH command with the
-vvvflag:ssh -vvv username@remote_host - Inspect the debug log lines starting with
debug1: Authentications that can continue:. - Look for:
debug1: Offering public key: /path/to/keyfollowed bydebug1: Authentications that can continue: publickey(indicating the server rejected the offered key).debug1: Trying private key: ...(checking whether the client offered the correct key file).
Step 2: Explicitly Specify the Private Key
If you have multiple SSH keys, the client may exhaust its allowed authentication attempts before presenting the correct identity key.
- Connect specifying the exact private key using the
-iflag:ssh -i ~/.ssh/id_ed25519 username@remote_host - Add the private key to your local SSH authentication agent:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 - Verify that the key is loaded in memory:
ssh-add -l
Step 3: Fix Remote ~/.ssh and authorized_keys Permissions
OpenSSH will refuse to read keys if permissions are too loose. This is the single most common cause of publickey rejections.
- Access the remote server (via web console, out-of-band management, or another authenticated session).
- Verify and apply the strict POSIX permissions required by OpenSSH:
# Set user home directory permissions (must not be group-writable): chmod 755 ~ # Set .ssh directory permissions: chmod 700 ~/.ssh # Set authorized_keys file permissions: chmod 600 ~/.ssh/authorized_keys # Ensure correct ownership: chown -R $USER:$USER ~/.ssh
Step 4: Verify the Public Key in authorized_keys
Ensure that your public key was appended cleanly without missing characters or unwanted newlines.
- On your local machine, print your public key:
cat ~/.ssh/id_ed25519.pub - On the remote server, inspect
~/.ssh/authorized_keys:cat ~/.ssh/authorized_keys - Ensure the public key on the server exactly matches your local public key. The key must sit entirely on a single continuous line starting with
ssh-ed25519orssh-rsa.
Step 5: Verify Server sshd_config Settings
Check that the remote OpenSSH daemon is configured to allow public key authentication.
- On the remote server, open the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Confirm that the following settings are present and not commented out with a hash (
#):PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys - Test the configuration syntax:
sudo sshd -t - If no errors are returned, restart the SSH service:
sudo systemctl restart ssh || sudo systemctl restart sshd
Step 6: Restore SELinux Contexts (RHEL / Rocky / Fedora)
On enterprise Linux distributions, an incorrect SELinux file context prevents sshd from reading user keyrings.
- On the remote server, restore default SELinux security contexts:
restorecon -R -v ~/.ssh
Configuring strict 700/600 file permissions, adding keys to ssh-agent, and verifying remote authorized_keys formatting resolves ‘Permission denied (publickey)’ SSH errors in Linux.