DevOps

AWS EC2 ssh access Permission denied (Public Key)


Introduction

If, while attempting to SSH into an EC2 instance, you receive the ‘Permission denied (public key)’ error then this means that an ssh directory with the key is missing on the EC2 instance. Many users trying to connect to their EC2 instances for the first time are having a separate problem. This article will explore the reasons this error happens, and provide some ways to solve the problem.

Why did this error occured?

This error usually shows when the SSH client cannot authenticate with an EC2 instance using the provided key pair. Due to secure access, AWS EC2 instances use public private key pair authentication. The client is denied access if the instance fails to correctly authenticate the client.

Solution

That just means authentication failed. Most common reasons:

  1. Wrong Key Pair: Sure that your key pair is good for this instance?
  2. Wrong Username: If you’re using Ubuntu based distributions, the username is `ubuntu`. It might be ‘ec2-user’, ‘admin’, ‘root’, ‘fedora’ or anything else. See the documentation for your specific instance.
  3. Wrong Host: Make sure you’re connecting to the correct EC2 instance.

In addition, if you have accidentally edited the `/home/<username>/.ssh/authorized_keys` on the EC2 instance, authentication might also fail.

 

It is often unclear from the AMI image description as to what the username (point 2) is, but on the EC2 side, the AWS EC2 documentation will normally help.

 

Pro Tip: Use the `-v` option with your SSH command to enable verbose mode and get more detailed information on why the authentication is failing.

 

Conclusion

Finally, if we try to access an AWS EC2 instance, we need to take care of the ‘Permission denied (public key)’ error and here we need to validate key pair, username and instance details. Once you start using verbose mode, it allows you to quickly find and resolve any authentication related issue that follows the above steps.

 

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise. 


Devops

Related Center Of Excellence