

When connecting to a remote server via SSH, encountering the Permission denied (publickey)
error can be a frustrating roadblock, especially when you urgently need access. This error indicates that SSH is unable to authenticate using the public key on your local machine, and thus, cannot establish a connection to the server.
In this blog post, we will walk you through several steps to troubleshoot and resolve this issue.
1. Ensure SSH Key is Present and Configured Correctly
First, verify that the SSH key pair exists on your local machine. The SSH keys typically reside in the ~/.ssh
directory. To check:
ls ~/.ssh
You should see files like id_rsa
(your private key) and id_rsa.pub
(your public key). If these files are missing, you’ll need to generate a new key pair using ssh-keygen
.
2. Add Your Public Key to the Server
If you haven’t already added your public key to the server, you’ll need to do so. This allows the server to authenticate your connection.
On Your Local Machine:
Start by copying the contents of your public key file:
cat ~/.ssh/id_rsa.pub
On the Remote Server (If Accessible via Alternate User):
If you can access the server using another user with sudo privileges, follow these steps to manually add the key:
ssh other_user@13.235.5.215
sudo su - ubuntu
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
Paste your public key into the authorized_keys
file, save, and exit. Then, adjust the permissions:
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
3. Verify SSH Key Permissions
Incorrect permissions on your SSH key files can prevent SSH from using them. Ensure your key files have the appropriate permissions:
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
4. Specify the Correct Identity File
If your private key is not named id_rsa
, or if you have multiple keys, you may need to specify the correct identity file when connecting:
ssh -oHostKeyAlgorithms=+ssh-rsa -i ~/.ssh/your_key_name ubuntu@13.235.5.215
This ensures SSH uses the correct key for authentication.
5. Check SSH Configuration
If you have an SSH configuration file at ~/.ssh/config
, verify it is correctly configured. For example:
Host 13.235.5.215
User ubuntu
IdentityFile ~/.ssh/id_rsa
HostKeyAlgorithms +ssh-rsa
This configuration directs SSH to use the specified identity file and host key algorithms when connecting to the server.
6. Debugging the Connection
If the issue persists, use the -v
option for verbose output, which provides detailed information on the SSH connection process:
ssh -v -oHostKeyAlgorithms=+ssh-rsa ubuntu@13.235.5.215
The verbose output can help you pinpoint where the connection is failing and provide clues for further troubleshooting.
Summary
The Permission denied (publickey)
error is a common SSH issue that can usually be resolved by ensuring your SSH key is properly set up, the permissions are correct, and the public key is added to the remote server’s authorized_keys
file. Following the steps outlined in this guide should help you overcome this error and successfully connect to your remote server.
If you encounter specific error messages or additional issues during this process, please leave a comment, and I’d be happy to assist further.
By addressing this issue methodically, you can ensure smoother access to your servers and avoid disruptions in your workflow. Happy troubleshooting!
Tag Cloud: #SSH #PermissionDenied #PublicKeyAuthentication #SSHKeys #ServerAccess #Linux #DevOps #RemoteAccess #Troubleshooting #SysAdmin #Security