Home » How to Fix Permission Denied (publickey) Error in SSH Connections
Systems & Servers

How to Fix Permission Denied (publickey) Error in SSH Connections

✨ Quick Answer

The Permission denied (publickey) error when trying to connect to a remote server via SSH occurs because the SSH daemon (sshd) on the remote server rejects the private key presented by your client. This is usually due to file permissions being too permissive, which invalidates the security of the connection and forces the server to discard the attempt for protection.

Quick Diagnostics

Cause
Overly permissive file permissions on private key (~/.ssh/id_rsa)
Solution
Set strict permissions on private key: chmod 600 ~/.ssh/id_rsa
Cause
Public key missing from remote server's authorized_keys
Solution
Copy public key to server: ssh-copy-id user@server

The Permission denied (publickey) error when trying to connect to a remote server via SSH occurs because the SSH daemon (sshd) on the remote server rejects the private key presented by your client. This is usually due to file permissions being too permissive, which invalidates the security of the connection and forces the server to discard the attempt for protection.

Step-by-Step Solution

  1. 1

    Step 1: Corregir los permisos de tu clave privada en el cliente

    The private key must never be readable by other users of the local machine. Adjust the permissions of the .pem or id_rsa file:

    BASH
    # Cambiar permisos para que solo el propietario pueda leer el archivo
    chmod 600 ~/.ssh/id_rsa
    
  2. 2

    Step 2: Ajustar los permisos del directorio .ssh y autorizaciones en el servidor

    Access the server console (through TTY or the hosting provider's console) and run the following fixes on your home directory:

    BASH
    # Asegurar el directorio de configuración de SSH
    chmod 700 ~/.ssh
    
    # Asegurar el archivo que almacena las claves autorizadas
    chmod 600 ~/.ssh/authorized_keys
    
    # Restablecer la propiedad de los archivos a tu usuario actual
    chown -R $USER:$USER ~/.ssh
    
  3. 3

    Step 3: Comprobar la directiva de autenticación en el servidor

    If the error persists, edit the SSH service configuration file on the server /etc/ssh/sshd_config and verify that public key authentication is enabled:

    PLAINTEXT
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
    

    Restart the service to apply the changes:

    BASH
    sudo systemctl restart sshd
    

Prevention Advice

Recommended security practices:

  • Avoid sharing your private key through insecure means or using lax permissions on the local host. Always maintain the principle of least privilege in your SSH configuration. Setting the StrictModes yes directive on the server forces SSHD to proactively reject connections if the home directory or authorized file permissions are insecure, preventing accidental intrusions due to filesystem oversights.
Author • Web Designer & App Creator

Rodolfo Castro

Web designer, app developer, and founder of SoporteCero. Specializing in UI/UX architecture, digital products, and modern web environments. Every tutorial and guide on SoporteCero is thoroughly tested and verified in our technical lab to ensure reliable, up-to-date solutions.