How to Resolve SSH Permission Denied (publickey) Error (2024)

SSH keys are a great method to use to authenticate SSH sessions without the need for a password at each login. However, like all technologies, SSH keys are not perfect, and you may encounter errors when using them. One of the most common errors when working with SSH keys is the permission denied (publickey) error.

This article will discuss the various reasons for this error and show you the quick steps that you can take to fix the error.

Error 1: Authorized Keys and Directory Permissions

One of the causes of this error could be the permissions and ownership configured for the .ssh directory and authorized_keys file.

To resolve this issue, set the .ssh directory permissions to 700 and the authorized_keys permissions to 6oo.

sudo chmod 700 ~/.ssh sudo chmod 600 authorized_keys

Error 2: SSH Incorrect Configurations

Another common cause of the publickey error is an incorrect configuration in the sshd_config file. To resolve this issue, edit the /etc/ssh/sshd_config file and change the following entries.

#PermitRootLogin prohibit-password
#PasswordAuthentication yes

The above entries should be as shown below:

PermitRootLogin yes

PasswordAuthentication yes

Save the configuration and restart the service:

sudo systemctl restart sshd.service

Error 3: Missing Keys

Another possible cause of the publickey permission denied error could be missing keys from your local machine. If you have your keys added to the authorized_keys file of the server but are missing the corresponding private keys, this may cause an error.

To resolve this issue, edit the ssh configuration file, disable public key authentication, and enable password login, as follows:

PubkeyAuthentication yes

#PasswordAuthentication yes

Change To the following entries:

#PubkeyAuthentication yes
PasswordAuthentication yes

Once you edit the configuration, save the file, and restart the SSH service:

sudo systemctl restart sshd.service

If you are certain that your keys have been compromised, you can remove them from authorized_keys or add a specific key to the revoked list in SSH.

Conclusion

This quick guide showed you how to address various issues that may cause the permission denied (publickey) error in SSH. If this issue persists, consider other troubleshooting methods.

How to Resolve SSH Permission Denied (publickey) Error (2024)

FAQs

How to Resolve SSH Permission Denied (publickey) Error? ›

To address this error, edit the default SSH configuration file. Locate the PasswordAuthentication attribute and set it to yes as shown below. Save the changes and exit the configuration file. Then restart the SSH service for the changes to come into effect.

How do I fix SSH permission denied publickey? ›

To address this error, edit the default SSH configuration file. Locate the PasswordAuthentication attribute and set it to yes as shown below. Save the changes and exit the configuration file. Then restart the SSH service for the changes to come into effect.

Why does SSH keep saying access denied? ›

If you receive this error, check for the following issues: The password is incorrect. The SSH key is missing on your local computer or on the Droplet. You are trying to use a password, but PasswordAuthentication is disabled in sshd_config.

How to add permission denied in SSH Linux? ›

To fix the error, you must first ensure that your keys are configured correctly on both the client and server, reset the permissions on the “authorized_keys” file, and check the server's SSH configuration. You may need to generate new keys and update your “authorized_keys” file on the server to resolve the issue.

How do I fix SSH failed permission denied publickey gssapi keyex gssapi with mic? ›

Steps After Mismatching Keys Confirmed
  1. Regenerate the Key Pair. ...
  2. Deploy the New Public Key to the Server. ...
  3. Verify Key Deployment. ...
  4. Set Private Key Permissions (Client): ...
  5. Set authorized_keys File Permissions (Server): ...
  6. Set .ssh Directory Permissions (Both Client and Server): ...
  7. Verify Permissions:
Feb 8, 2024

How do I give permission to SSH? ›

Granting SSH access to a user
  1. Locate the following OpenSSH file: /etc/ssh/sshd_config.
  2. Make a backup of this file so you can revert if necessary. cp /etc/ssh/sshd_config{,.' date +%s'}
  3. Edit the file by using the OpenSSH keywords.

How do I fix SSH error? ›

6 Ways to Fix the SSH Connection Refused Error
  1. Change the SSH Port Number. ...
  2. Double Check the Credentials. ...
  3. Make Sure SSH Is Enabled. ...
  4. Make Sure SSH Client Is Installed. ...
  5. Ensure SSH Daemon Is Installed on the Server. ...
  6. Check Your Firewall Settings.

How do I fix permission denied in Linux? ›

For example, if you encounter “Permission Denied” while reading a file, you can use sudo cat filename to read the file with superuser permissions. This grants you the necessary access to bypass permission restrictions and read the file content.

How to check SSH permissions? ›

Procedure
  1. Check the SSH public key files by running the following command: #ls -al /etc/ssh/*pub.
  2. Verify that the files have the following permissions. ...
  3. Check the SSH private key files by running the following command: ls -al /etc/ssh/*key.
  4. Verify that the files have the following permissions.
Feb 7, 2023

How do I change permissions in Linux SSH? ›

To change the permissions of all of the files within that directory, type “chmod -R 755 directoryname” . (Replace “directoryname” with the actual name of the directory.) If you wish to only change permissions for one file, type “chmod 755 filename“. Type “exit” to log out of the SSH session when you are done.

Why is my Linux server not accepting SSH connections? ›

Therefore, if you receive the SSH "connection refused" error, it may be because you are attempting to connect to the SSH server on the default port 22 while it is running on some different port. After identifying the correct SSH port, try connecting to your remote system using that specific port.

Why is my SSH host not connecting? ›

Verify that the host IP address is correct for the Droplet. Verify that your network supports connectivity over the SSH port being used. Some public networks may block port 22 or custom SSH ports. You can do this by, for example, testing other hosts using the same port with a known working SSH server.

Why is my SSH connection refused? ›

Incorrect SSH credentials or port configuration can also result in the “Connection Refused” error. Typos or inaccuracies in the SSH username, password, hostname, or port can cause the server to reject the connection request, triggering the error message.

How do I authorize a SSH key? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

Why is my SSH proxy refusing connections? ›

Typos or incorrect credentials are common reasons for a refused SSH connection. This includes errors in the specified username or password, or wrong IP address. Any discrepancies lead to authentication failure and result in the connection being refused.

How to check SSH permissions in Linux? ›

Procedure
  1. Check the SSH public key files by running the following command: #ls -al /etc/ssh/*pub.
  2. Verify that the files have the following permissions. ...
  3. Check the SSH private key files by running the following command: ls -al /etc/ssh/*key.
  4. Verify that the files have the following permissions.
Feb 7, 2023

Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 5811

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.