How to Fix ‘Permission Denied (publickey)’ SSH Error in Linux

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, or authorized_keys file 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_keys file.
  • Disabled Publickey Authentication on Server: The remote /etc/ssh/sshd_config configuration file has PubkeyAuthentication no specified.
  • 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: publickey without 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.

  1. Log into the target remote machine and navigate to the user home directory.
  2. Set strict ownership and permissions on the home, .ssh, and authorized_keys structures:
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    chmod 755 ~
  3. Ensure that your user owns the files, replacing username with your actual Linux user:
    chown -R username:username ~/.ssh
  4. 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.

  1. Start the local SSH agent in your shell:
    eval "$(ssh-agent -s)"
  2. 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_rsa instead.)

  3. Confirm that the identity is loaded:
    ssh-add -l
  4. 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.

  1. On the remote server, open the SSH configuration file with elevated privileges:
    sudo nano /etc/ssh/sshd_config
  2. Verify the following directive is set:
    PubkeyAuthentication yes
  3. If you are connecting with an older RSA key on modern OpenSSH, temporarily append:
    PubkeyAcceptedAlgorithms +ssh-rsa
  4. Save changes (Ctrl + O, then Enter) and exit (Ctrl + X).
  5. Test configuration syntax and restart the daemon:
    sudo sshd -t
    sudo systemctl restart sshd

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.