CommandsLinux

How to Fix SSH Permission Denied (Public key) Error in Linux

Introduction

Encountering the “Permission Denied (Public Key)” error while trying to establish an SSH connection in Linux can be frustrating. In this article, we will guide you through the process of troubleshooting and resolving this issue. You will learn about the commands involved, their purpose, and how to use them effectively.

Step 1: Verify SSH Key Permissions

To ensure the correct permissions are set for your SSH private key, use the following command:

ls -l ~/.ssh/id_rsa

Make sure the permissions are restrictive (600 or 400) for better security. The output should resemble something like this:

-rw------- 1 username username 1766 Jun 1 09:42 /home/username/.ssh/id_rsa
Step 2: Check SSH Agent

Start the SSH agent and set the necessary environment variables by running the following command:

eval $(ssh-agent)

This will enable the agent to manage your private keys.

Step 3: Add Private Key

Add your SSH private key to the agent with the command:

ssh-add ~/.ssh/id_rsa

If your private key has a passphrase, you will be prompted to enter it.

Step 4: Verify SSH Connection

Test the SSH connection to the target host by executing the following command:

ssh -T user@hostname

This command will verify if the SSH connection is successful. You should see a welcome message or a prompt indicating a successful connection, similar to this:

Welcome to XYZ Server!
Step 5: Update SSH Server Configuration

Open the SSH server configuration file for editing:

sudo nano /etc/ssh/sshd_config

Make sure the following options are enabled in the configuration file: PubkeyAuthentication, AuthorizedKeysFile, and PasswordAuthentication. Edit the file as needed, save, and exit the text editor.

Step 6: Restart SSH Service

To apply the changes made in the SSH server configuration, restart the SSH service by running:

sudo service ssh restart

This will ensure that the SSH server is using the updated configuration.
By following these steps, you can troubleshoot and fix the SSH Permission Denied (Public Key) error in Linux, allowing you to establish successful SSH connections.

Also, check out the next article How to Setup SSH Passwordless Login in Linux – A Comprehensive Guide.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button