Friday, 28 Jun 2024
Technology

How to Troubleshoot SSH Permission Denied Error

permission denied

Introduction

The SSH Permission Denied error often occurs when permission-related settings are modified on the SSH server. This error may occur after a package installation or when creating new users. In this tutorial, we will guide you through troubleshooting the SSH Permission Denied error and help you reconnect to your SSH server.

Prerequisites

Before we proceed, make sure you have the following:

  • SSH client on your local machine and SSH server on the remote system.
  • A user account to access the remote server (for password-based login).
  • A user account with sudo or root privileges.

What Causes SSH Permission Denied Error?

The SSH Permission Denied error occurs when trying to SSH into a server and can be displayed as “Permission denied (publickey,gssapi-keyex,gssapi-with-mic)”. This error message indicates that the attempted authentication methods failed during the connection initiation. While it suggests that the public key is the issue, it can be misleading.

There are two common reasons for this error:

  1. SSH server configuration file (sshd_config): This file contains the SSH server configuration. Changes to this file can lead to the Permission Denied error.
  2. Insufficient permissions in the authorized_keys file: The authorized_keys file contains a list of public keys for clients allowed to SSH into the server. If the system cannot read from this file, it results in the Permission Denied error.
Tham Khảo Thêm:  Hard Reset or Force Restart for iPhone 13 Models

How to Fix SSH Permission Denied

Here are two solutions that you can try to fix the SSH Permission Denied error. These solutions require performing steps on the server-side. Open the terminal on your server and follow the instructions below.

Solution 1: Enable Password Authentication

If you want to use a password to access the SSH server, you can fix the Permission Denied error by enabling password login in the sshd_config file. Follow these steps:

  1. Open the sshd_config file in a text editor (such as nano):
    sudo nano /etc/ssh/sshd_config
  2. Locate the PasswordAuthentication line and ensure it ends with “yes”.
  3. Find the ChallengeResponseAuthentication option and disable it by adding “no”.
  4. If any lines are commented out, remove the “#” to uncomment them.
  5. Save the file and exit.
  6. Restart the SSH service by running the following command:
    sudo systemctl restart sshd

Solution 2: Change File System Permissions

Using password-based login as the SSH authentication method is not recommended for security reasons. This solution troubleshoots the public key authentication method. Follow these steps:

  1. Open the sshd_config file in a text editor:
    sudo nano /etc/ssh/sshd_config
  2. Ensure the following options are set as specified:
    PermitRootLogin no
    PubkeyAuthentication yes
  3. Comment out the GSSAPI-related options by adding “#” at the beginning of the lines:
    #GSSAPIAuthentication yes
    #GSSAPICleanupCredentials no
  4. Make sure the UsePAM line is set to “yes”:
    UsePAM yes
  5. Save the file and restart the SSH service:
    systemctl restart sshd
  6. Check the permissions in your home folder:
    ls -ld
  7. If the owner’s permissions are not set to read, write, and execute (drwx-), use the chmod command to change them:
    chmod 0700 /home/[your-username]
  8. Go to the .ssh folder and check the permissions:
    ls -ld
  9. The .ssh folder should also have read, write, and execute permissions for the file owner. To enforce them, use chmod again:
    chmod 0700 /home/your_home/.ssh
  10. Check the permissions of the authorized_keys file in the .ssh folder:
    ls -ld authorized_keys
  11. The file owner should have read and write permissions. Set them using chmod:
    chmod 0600 /home/[username]/.ssh/authorized_keys
  12. Now try logging in with the key pair again. A successful login attempt should confirm that the issue is resolved.
Tham Khảo Thêm:  How to Change Your Payment Method on HBO Max

Frequently Asked Questions

Q: What is the SSH Permission Denied error?
A: The SSH Permission Denied error occurs when there are issues with authentication during an SSH connection attempt. It is often displayed as “Permission denied (publickey,gssapi-keyex,gssapi-with-mic)”.

Q: What causes the SSH Permission Denied error?
A: The error can be caused by misconfigurations in the SSH server (sshd_config) or insufficient permissions in the authorized_keys file.

Q: Can I use password authentication to fix the SSH Permission Denied error?
A: Yes, enabling password authentication in the sshd_config file can be a solution. However, using public key authentication is generally more secure.

Q: Are there any security risks associated with password-based login for SSH?
A: Yes, password-based login for SSH is less secure compared to public key authentication. It is recommended to use public key authentication whenever possible.

Conclusion

By following the steps provided in this tutorial, you should be able to troubleshoot and fix the SSH Permission Denied (publickey,gssapi-keyex,gssapi-with-mic) error. Once resolved, you will be able to successfully SSH into your server. For more helpful articles and tutorials, visit Eireview.