AWS
EC2
SSH
publickey
troubleshooting

Permission denied publickey when SSH Access to Amazon EC2 instance

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When accessing an Amazon EC2 instance via SSH, one common issue users may encounter is the error message: "Permission denied (publickey)." This error typically arises when SSH is unable to authenticate the connection due to issues with the provided SSH key pair. In this article, we'll delve into the technical details of why this might happen and how to resolve it.

Understanding SSH and Key Pairs

Secure Shell (SSH) is a protocol for securely accessing and managing network devices. When you launch an EC2 instance, you often need to use SSH to connect to it. Access is typically controlled via SSH key pairs: a public key that's placed on the EC2 instance and a private key held securely by the user.

SSH Key Pair

  • Public Key: Stored on the server, it grants access when verified against the private key.
  • Private Key: Held by the user, it should never be shared.

Connection Process

  1. Client Initiation: The SSH client begins the session, trying to create a secure channel.
  2. Key Verification: The server checks if the incoming connection's private key matches any of its registered public keys.
  3. Access Granted: If the match is successful, access is granted.

Common Reasons for "Permission Denied (publickey)" Error

Several factors may cause this error, including misconfigurations or missing files. Here are some common reasons:

  1. Incorrect Key Usage: The private key file used in the SSH command is incorrect.
  2. Permissions Issue: Incorrect file permissions on the private key file.
  3. Public Key Not Registered: The user's public key hasn't been correctly deployed to the remote instance.
  4. Misconfigured SSH Client: Mistakes in the SSH command or configuration file.

Example SSH Command

bash
ssh -i my-key-pair.pem [email protected]

In this command:

  • -i my-key-pair.pem specifies the private key file.
  • ec2-user is the typical username for an Amazon Linux AMI.
  • The hostname is represented by the instance's public DNS.

Troubleshooting and Fixing the Error

1. Verify Private Key File

Ensure the file path to your private key is correct. You should specify the full path if the key resides outside of your current working directory.

2. Check Permissions

One of the most common issues is permissions. Use the following command to ensure that your private key file has the correct permissions (read-only for the user):

bash
chmod 400 my-key-pair.pem

3. Confirm Instance Details

Make sure you are using the correct username. For Amazon Linux, it might be ec2-user; for Ubuntu, use ubuntu. Ensure the DNS or IP address is accurate.

4. Verify the Key on the EC2 Instance

Upon launching an instance, AWS automatically adds your public key to the ~/.ssh/authorized_keys file for the specified user. Ensure that the public key is correctly placed, and check for any alterations to this file.

5. Use Verbose SSH Mode

For more information about what is happening during the connection, use the -v (verbose) option in your SSH command:

bash
ssh -v -i my-key-pair.pem [email protected]

This command provides detailed logs that can help identify where the authentication process is failing.

Table of Common Solutions

IssueCauseSolution
Incorrect Key FileWrong private key specifiedCorrect the SSH command i-i option
Incorrect Key PermissionsPrivate key has too liberal permissionsAdjust permissions chmod400<file>chmod 400 <file>
Wrong UsernameDefault username not usedCheck AMI documentation for the correct user
Public Key MissingKey not copied to authorized_keysManually add the public key to &#126;/.ssh/authorized_keys
SSH ConfigurationErrors in SSH config fileReview and correct  /.ssh/config~/.ssh/config if used

By methodically addressing each potential cause, you should be able to resolve the "Permission denied (publickey)" error and gain SSH access to your EC2 instance.

Further Considerations

  • Security: Always ensure your private key is stored securely. Avoid using weak permissions or transferring it over unsecured channels.
  • Instance Metadata: AWS provides instance metadata for querying details without SSH access, which can help confirm details such as IAM roles or security groups.
  • Logs: Check EC2 instance logs if you suspect a configuration issue on the server side.

By understanding the SSH process and methodically addressing potential issues, you can effectively troubleshoot and resolve connectivity challenges, ensuring a smoother experience when managing your EC2 instances.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.