How to Resolve SSH Permission Denied (publickey) Error?

Secure Shell (SSH) keys are a combination of public-private key-value pairs for making a secure connection between remote devices. The public key has significant importance in automating the authentication process. While dealing with the SSH keys, an error “ssh permission denied (publickey)” occurs in the terminal.

This article will explain multiple solutions to resolve the error mentioned above. The supported content of this article is given below:

Let’s start with the first reason for this error.

Reason: Incorrect Configuration of the sshd_config File

One of the common reasons causing errors is the sshd_config file is not properly configured. Therefore, the system does not locate the required public keys:

Solution 1: Enable the Password Authentication in the sshd_config File

To encounter the above error, disable the password authentication services. To do so, access the “sshd_config” file and perform some modifications. By default, the value of “PasswordAuthentication” is “no”. Change the value from “no” to “yes” and save the file by disabling the public authentication:

$ sudo nano /etc/ssh/sshd_config

After closing the file, restart the “sshd” services to apply changes:

$ sudo systemctl restart sshd

Solution 2: Install the Openssh Package

Another solution that can resolve the error is possible by installing the “openssh-server” package. To install the required package, execute the below script:

For Ubuntu/Debian/Linux Mint:

$ sudo apt install openssh-server

For RHEL/Centos/Fedora:

$ yum -y  install openssh-server

The output shows that the “openssh-server” package is installed in the system.

Solution 3: Generate and Add an SSH Key to the Server

Another solution to resolve the ssh permission denied error, generate the new ssh keys and add them to the server. To generate the public-private key pair, follow the below script:

$ ssh-keygen

The output shows that the public-private key has been successfully generated.

To add the generated key to the server, specify the username with the host. In this case, the “itslinuxfoss” represents the username, and “ubuntu” refers to the hostname:

$ ssh-copy-id [email protected]

The output shows that the ssh key has been added successfully to the server.

Verify SSH Services

To verify the ssh services, write the current username with the host that will display the detail of the last login with the IP address:

The output shows that ssh services are running perfectly.

These are all the reasons and solutions to fix the permission denied (publickey) error.

Conclusion

The error “ssh permission denied (publickey)” occurs due to the incorrect configuration of the sshd_config file. It can be solved by disabling the password authentication in the sshd_config file, reinstalling the Openssh package, and adding the SSH Key to the Server. This article has provided all possible methods to encounter the above error.