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.
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.
The above entries should be as shown below:
Save the configuration and restart the 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:
Change To the following entries:
Once you edit the configuration, save the file, and restart the SSH 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.
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.