Attempting to establish a remote connection to a Linux server only to receive the terminal error: “Permission denied (publickey)” or “Permission denied (publickey,gssapi-keyex,gssapi-with-mic)” prevents remote command-line administration. Secure Shell (SSH) relies on cryptographic key pairs to authenticate sessions securely. When the OpenSSH client cannot locate a matching private key, or when the remote SSH daemon (sshd) rejects the incoming public key due to overly permissive directory permissions, SSH terminates the handshake immediately. This guide explains how to identify authentication failures using verbose client logs, adjust POSIX permissions, and configure OpenSSH correctly.
Why Does the SSH Publickey Error Occur?
OpenSSH enforces strict security requirements to prevent privilege escalation. Common causes of publickey rejection include:
- Overly Permissive File Permissions: OpenSSH rejects keys if the remote user’s home directory,
~/.ssh/folder, orauthorized_keysfile are writable by group or other users. - Missing Private Key in SSH Agent: The local client has not loaded the private identity key into
ssh-agent, or the key path was omitted from~/.ssh/config. - Mismatched Public Key: The public counterpart of the private key used for authentication does not exist inside the server’s
~/.ssh/authorized_keysfile. - Disabled Publickey Authentication on Server: The remote
/etc/ssh/sshd_configconfiguration file hasPubkeyAuthentication nospecified. - Deprecated Key Algorithms: Modern OpenSSH releases reject older SHA-1 based RSA keys by default unless explicitly permitted.
Method 1: Identify Failures Using Verbose Logging
Running the SSH command with verbose output details the exact stage where key negotiation fails.
ssh -vvv username@remote_host_ip
Observe the terminal output carefully:
- Look for
Offering public key: /home/user/.ssh/id_ed25519. If the client offers no keys, your local agent has not loaded your credentials. - If the client offers a key and the server responds with
Authentications that can continue: publickeywithout accepting it, the server rejected the key or file permissions.
Method 2: Correct POSIX Permissions on the Remote Server
If you have console access via a cloud provider’s web terminal or physical machine, verifying remote permissions is essential.
- Log into the target remote machine and navigate to the user home directory.
- Set strict ownership and permissions on the home,
.ssh, andauthorized_keysstructures:chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys chmod 755 ~ - Ensure that your user owns the files, replacing
usernamewith your actual Linux user:chown -R username:username ~/.ssh - Attempt to reconnect from your local terminal.
Method 3: Add Private Key to the Local SSH Agent
If your local machine has generated a custom private key, load it into the local memory cache.
- Start the local SSH agent in your shell:
eval "$(ssh-agent -s)" - Add your private key (such as an Ed25519 or modern RSA key):
ssh-add ~/.ssh/id_ed25519(If you are using a standard RSA key, use
ssh-add ~/.ssh/id_rsainstead.) - Confirm that the identity is loaded:
ssh-add -l - Connect again using the loaded credentials.
Method 4: Copy the Public Key to the Remote Host
If you have temporary password authentication enabled on the server, use ssh-copy-id to properly append your public key.
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@remote_host_ip
This utility automatically verifies that the remote directory exists and applies the correct file permissions to authorized_keys.
Method 5: Check Remote SSH Daemon Configuration
Verify that public key authentication is explicitly enabled in the server’s daemon configuration.
- On the remote server, open the SSH configuration file with elevated privileges:
sudo nano /etc/ssh/sshd_config - Verify the following directive is set:
PubkeyAuthentication yes - If you are connecting with an older RSA key on modern OpenSSH, temporarily append:
PubkeyAcceptedAlgorithms +ssh-rsa - Save changes (Ctrl + O, then Enter) and exit (Ctrl + X).
- Test configuration syntax and restart the daemon:
sudo sshd -t sudo systemctl restart sshd