Linux Commands

How to Resolve SSH Permission Denied (publickey) Error

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list