Troubleshooting & Comparisons

How to Fix Permission Denied Publickey Error in SSH Connections

Troubleshoot and resolve the 'Permission denied (publickey)' error encountered during SSH connections using detailed steps.

4 min read

Troubleshooting the "Permission denied (publickey)" error can be challenging, especially when SSH keys fail to authenticate. This guide will help you systematically identify and fix the root cause of the problem.

Understanding the Issue

When connecting to a remote server via SSH, you may encounter the error:

Permission denied (publickey).

This occurs when the server cannot match your private key with a corresponding public key. SSH relies on a pair of keys for secure communication:

  • Private key: Stored locally on your device.
  • Public key: Stored in the server's ~/.ssh/authorized_keys.

For authentication to succeed:

  • Your private key must match the server's public key.
  • Both keys must be correctly configured on their respective systems.

Prepare for Troubleshooting

prerequisites

  • Access to the SSH client machine's .ssh directory.
  • Control over the remote server, either remotely or through a hosting provider.
  • Familiarity with terminal commands.

Validate Key Files Locally

Ensure the SSH client can locate and use the correct key pair.

steps

  1. Open a terminal and navigate to your SSH directory:

    bash
    cd ~/.ssh
    ls

    Confirm the presence of key files like id_rsa (private key) and id_rsa.pub (public key).

  2. Verify the key pair matches:

    bash
    ssh-keygen -y -f id_rsa
    • Compare the output against id_rsa.pub using:
      bash
      cat id_rsa.pub
    • Ensure the keys are identical.

Check SSH Configuration

Review the local SSH configuration file to ensure it references the correct key identity.

steps

  1. Open the configuration file:
    bash
    nano ~/.ssh/config
  2. Verify or add the IdentityFile directive:
    Host *
        IdentityFile ~/.ssh/id_rsa
    Replace id_rsa with your private key name if customized.
  3. Save changes and exit the editor (Ctrl + X, then Y, then Enter).

Diagnose Using SSH Verbose Mode

Verbose mode provides detailed output on why the SSH connection fails.

Run SSH in verbose mode

ssh -v username@server_ip
# Look for lines like:
# debug1: Offering public key: /home/user/.ssh/id_rsa
# debug1: Server accepts key: no
  • Focus on lines indicating whether the suggested private key was used or rejected.

Inspect and Update Server-Side Setup

If the local keys are correct, verify the server configuration.

steps

  1. Access the server via an alternative method (e.g., another authorized user, web console).
  2. Navigate to the server's SSH directory:
    bash
    cd ~/.ssh
    nano authorized_keys
  3. If the file is empty or missing your public key, add your local id_rsa.pub contents.
  4. Save changes and ensure correct file permissions:
    bash
    chmod 600 ~/.ssh/authorized_keys
    chmod 700 ~/.ssh

Common Provider Solutions

Verification and Retesting

Once adjustments are made, retry the SSH connection.

Recap and FAQ

FAQ

Why does "Permission denied (publickey)" appear during SSH?

This error means the server cannot verify your private key against its authorized public keys. The problem may stem from missing keys, incorrect file permissions, or misconfigured SSH settings.

How do I check which key my SSH client offers?

Run your SSH command in verbose mode with ssh -v. Look for lines about Offering public key to verify which key the client attempts to use.

What should file permissions be for SSH keys?

On the client:

  • ~/.ssh directory: 700
  • Private key: 600

On the server:

  • ~/.ssh directory: 700
  • authorized_keys file: 600
Can I recover access without SSH keys?

If password-based login is allowed, use your username and password. Alternatively, use your hosting provider's emergency access tools like a web console or reset the SSH keys through their dashboard.


Official reference: OpenSSH manual pages.