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.
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
- Client Initiation: The SSH client begins the session, trying to create a secure channel.
- Key Verification: The server checks if the incoming connection's private key matches any of its registered public keys.
- 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:
- Incorrect Key Usage: The private key file used in the SSH command is incorrect.
- Permissions Issue: Incorrect file permissions on the private key file.
- Public Key Not Registered: The user's public key hasn't been correctly deployed to the remote instance.
- Misconfigured SSH Client: Mistakes in the SSH command or configuration file.
Example SSH Command
In this command:
-i my-key-pair.pemspecifies the private key file.ec2-useris 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):
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:
This command provides detailed logs that can help identify where the authentication process is failing.
Table of Common Solutions
| Issue | Cause | Solution |
| Incorrect Key File | Wrong private key specified | Correct the SSH command option |
| Incorrect Key Permissions | Private key has too liberal permissions | Adjust permissions |
| Wrong Username | Default username not used | Check AMI documentation for the correct user |
| Public Key Missing | Key not copied to authorized_keys | Manually add the public key to ~/.ssh/authorized_keys |
| SSH Configuration | Errors in SSH config file | Review and correct 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
- Persist local dynamoDB data in volumes lack permission - unable to open database file
- PersistentVolumeClaim is stuck ''waiting for a volume to be created, either by external provisioner ebs.csi.aws.com'' on new AWS EKS cluster
- Pipe a stream to s3.upload
- Pipe Events from Azure Event Hub to Azure Service Bus
- permission denied to set parameter client_min_messages to notice
- Permission Denied trying to run Python on Windows 10
- Placing Files In A Kubernetes Persistent Volume Store On GKE
- Pod CPU Throttling

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.