Sometimes, when you try to connect via SSH, you may encounter the “Permission Denied (Public Key)” error. This error can occur if you have changed permission settings on your SSH server such as creating new user accounts or installing new packages. So, this prevents you from successfully connecting to your SSH server.
In this guide, we will learn how to fix SSH Permission Denied (Public Key) error. Furthermore, we will also explore the causes and possible solutions to resolve this common SSH authentication error.
You can also learn how to generate ssh key on ubuntu using this guide.
Why does the SSH “Permission denied (public key)” error occur?
This error can occur due to various reasons such as incorrect username or host, missing public key on the server, invalid permissions to the SSH keys and folders, and the SSH service configuration. However, the two main causes of this error are listed in the following:
- This error may be caused by misconfiguration in the “/etc/ssh/sshd_config” file which contains the default SSH configuration.
- The error can also be caused by insufficient permissions on the “~/.ssh/authorized_key” file located on the remote system. This file holds the public keys of client systems that are allowed to establish an SSH connection with the server.
How to Fix “Permission Denied (Public Key)” Error
You can resolve the “Permission denied (public key)” error using two different methods:
Solution 1: Check and Enable Password Authentication
One of the main causes of this error is misconfiguration of the “/etc/ssh/sshd_config” file. Disabling the password authentication feature on your system can cause this error when someone only uses SSH key-based authentication, and the private key may not be present for any reason. So, if you want to access the SSH server using password authentication, you can fix the “Permission Denied Error” by enabling the password authentication feature in your SSH configuration file.
To do this, open a terminal using the “Ctrl+Alt+t” keyboard shortcut and perform the following steps on the server side:
step 1: Open the “/etc/ssh/sshd_config” configuration file in a text editor. Here, we use the default Nano text editor.
, sudo nano ,etc,ssh,sshd_config
step 2: Locate the “Password Authentication” line and enable it to “Yes”. If this line is commented, remove the “#” sign at the beginning to remove the comment on this line.
Save previous changes using “Ctrl+O” and exit the configuration file via “Ctrl+x”.
step 3: Reload or restart the ssh service on your system using the following mentioned commands to apply the changes to the file.
, sudo systemctl restart sshd
Now, if you try to connect to your ssh client again, you should be able to access the remote host machine.
Solution 2: Change Permissions on SSH Authorized_Keys
Using the SSH password-based authentication method is not recommended due to security issues. Therefore, this method is better as it fixes this error by using the Public Fundamental Authentication approach. In most cases, the error comes from setting incorrect ownership and permissions on the “~/.ssh/authorized_keys” file located on the remote server. To fix the error using this method, you need to follow the given steps:
step 1: Open the “/etc/SSH/sshd_config” file in a text editor.
, sudo nano ,etc,ssh,sshd_config
Now, edit the following options:
PermitRootLogin Number
public authentication Yes
If you want to use root login, set “permitrootlogin yes”.
step 2:Comment out the GSSAPI options by adding a “#” symbol at the beginning of these lines.
#GSSAPIAuthentication Yes
#GSSAPICleanupCredentials No
Also, set “UsePAM Yes” in the configuration file.
step 3: Save the previous configuration and restart the SSH services using this command:
, sudo systemctl restart sshd
Now, check the permissions on your home folder:
If you do not have read-write permissions (drwx——) on this folder, set the permissions using the “chmod” command:
, sudo chmod ,Home,our patience
Change permission on “.ssh” folder using the following command:
, chmod 0700 ,Home,your home,.ssh
There is “authorized_keys” file in “.ssh” folder. Check permissions on this file.
, Ras -LD .ssh,authorized_keys
The file owner must have read-write permission. To change file permissions, use the following command:
, chmod 0600 ,Home,[username],.ssh,authorized_keys
Now, try to connect to the SSH remote host. We hope you will not get any errors at the time of connection.
conclusion
In this troubleshooting guide, we figured out how to resolve the “Permission denied (public key)” error. We discussed the possible causes of this error and suggested two different solutions to resolve this error. We hope the previous instructions help you fix this error on your Linux system. If you want to get more information you can visit here document here, Thank you!