Wednesday, 10 June 2026

SSH Permission Denied (publickey) Fix in Linux

 

One of the most common SSH errors Linux administrators encounter is:
Permission denied (publickey)

This error occurs when SSH authentication using public keys fails. In this guide, we'll look at the common causes and how to fix them.

Error Message

ssh user@server
Permission denied (publickey)

Common Causes

  • SSH key not present on remote server

  • Incorrect file permissions

  • Wrong private key being used

  • Public key missing from authorized_keys

  • SSH server configuration issues

Step 1: Verify Your SSH Key Exists

Check your local SSH keys:

ls -la ~/.ssh

You should see files such as:

id_rsa
id_rsa.pub

or

id_ed25519
id_ed25519.pub

Step 2: Copy Public Key to Remote Server

Use:

ssh-copy-id user@server

If ssh-copy-id is unavailable:

cat ~/.ssh/id_rsa.pub

Copy the output and append it to:

~/.ssh/authorized_keys

on the remote server.

Step 3: Check Permissions

Incorrect permissions often cause SSH authentication failures.

Run:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Verify ownership:

chown -R user:user ~/.ssh

Step 4: Specify the Correct Private Key

If you have multiple keys:

ssh -i ~/.ssh/id_rsa user@server

You can also add this configuration:

Host myserver
    HostName server-ip
    User user
    IdentityFile ~/.ssh/id_rsa

inside:

~/.ssh/config

Step 5: Enable Debug Mode

Run SSH with verbose logging:

ssh -vvv user@server

Look for messages such as:

Offering public key
Authentication failed

These logs help identify the exact issue.

Step 6: Verify SSH Server Configuration

Check:

sudo vi /etc/ssh/sshd_config

Ensure:

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Restart SSH:

sudo systemctl restart sshd

or

sudo systemctl restart ssh

Conclusion

The "Permission denied (publickey)" error is usually caused by missing keys, incorrect permissions, or SSH configuration issues. By checking keys, permissions, and server settings, you can quickly restore access.

Have you encountered a different SSH authentication issue? Let us know in the comments.

No comments:

Post a Comment