SSH “Error Permission Denied(publickey)” Error and Solution

During establishing an SSH connection you may get the “Error permission denied (publickey)” error. This error prevents you from connecting to the remote system via SSH. The remote system can be Linux, Ubuntu, Mint, Kali, Linode Server, Digital Ocean Server or AWS server, etc. This error is generated because of cryptographic keys.

“Error Permission Denied(publickey)” Solution

There are different reasons and causes for the “Error Permission Denied(publickey)” error and different solutions should be provided for these different causes.

Check If You Have a Valid SSH Key and If not Create SSH Key

The first solution is checking if the current user has an SSH key which is deployed to the remote SSH server. The SSH keys are located under the user’s home directory .ssh folder. List the contents below.

ls ~/.ssh/

If there is no file with a name like id_rsa or id_rsa.pub which generally contains the term id. Use the following post to create an SSH key and deploy to the remote SSH server.

Set Rigth File Permissions For Local Public and Private SSH Key

The public and private files are stored under the users home directory .ssh folder. Even the public and private keys are exist they need to have right file permission to read by the current user. Set this directory file permissions with the following chmod command.

chmod -R 740 ~/.ssh/

Check The Local Machine Key Is Authenticated On the Remote System

Login to the remote system you want to connect via GUI etc. Then navigate to the target user directory and look the authorized_keys file under the .ssh folder.

cat ~/.ssh/authorized_keys
SSH authorized_keys

Check if the given key is the same with your local system public key which can be listed like below.

ls ~/.ssh/

Select The Right SSH Key From Multiple Keys

The local system user may have multiple keys. While trying to connect remote SSH server one of the keys will be used but this may not the right key. So by using the -i parameter with the ssh command the right key should be selected. The private keys should be selected.

ssh -i ~/.ssh/id_rsa2

Enable Root Login For SSH Server

The root is a special account that has administrative rights. By default, the SSH server configuration disables the root login via SSH. If you try to login with root and key you may get this error. In order to fix this enable root login in the ssh configuration file /etc/ssh/sshd_config .

PermitRootLogin prohibit-password

Line to

PermitRootLogin yes 

Restart SSH Service To Apply Configuration and Related Changes

We have defined different solutions for the “Permission denied (publickey)” error. In order to apply this new configuration the SSH server should be restarted.

sudo systemctl restart sshd

Leave a Comment