✔️ [Solved ]Permission denied (publickey) SSH to Amazon EC2 instance

Quick Fixes for Permission denied (publickey): Make sure you are using the right key. Make sure you are using the right username. Make sure you are connecting to the right IP. Read on in the article for more details.

Get the correct ssh command from the AWS console

Permission denied (publickey) select connect
Open up the connect page
Copy the ssh command out of the console

This command from AWS should be exactly what you need. You’ll see Permission denied (publickey) if any of the details in this command are wrong, so make sure your path to your key file is correct.

ssh -i <key> ec2-user@<ec2 ip>

The simple fixes to Permission denied (publickey)

Check you are using the right key in AWS

Verify that you are using the correct key by checking the name of the key in the AWS console.

Permission denied (publickey) check the key name in the console

Verify you are using the right username when connecting

You’ll get the Permission denied (publickey) error when connecting to an AWS EC2 instance if you’re using the wrong username. Check in the console what the username should be.

Permission denied (publickey) select connect
Open up the connect page
Permission denied (publickey)  get the username from aws console
See what the username should be

Verify that you are connecting to the right host

Validate that you are connecting to the right instance. Maybe you’ve got a typo in the IP?

Advanced Troubleshooting

More tips:

Add the -v flag to your SSH command to get more detailed troubleshooting information

More AWS docs on how to connect to your EC2 insance: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Other potential issues that cause Permission denied (publickey)

  • If the user you are trying to SSH in’s home directory is writable to the group then SSH login will not be possible.
  • /home/<user>/.ssh/authorized_keys file is messed up on the instance

AWS Guide

If none of this has helped then it is possible something has gone wrong on the EC2 instance itself. Check out this AWS guide for more advanced troubleshooting instructions.

https://aws.amazon.com/premiumsupport/knowledge-center/ec2-linux-fix-permission-denied-errors/

Leave a Comment

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