Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon EC2 Permission denied (publickey)

This seems to be a common problem but my specific case seems a little different.

I set up a new Amazon EC2 instance using the command line tools and connected via SSH and did some configuration work.

Initially, though, I couldn't ssh on to the instance, I had to stop and restart the instance, then i could connect. Before restarting I just got the response.

Permission denied (publickey). 

That was last night, this morning I go back to the same instance and now all I get is

Permission denied (publickey). 

I've tried rebooting the instance with no joy.

Can anyone point me in the right direction here? The same command that worked last night no longer works, I'm connecting from my Macbook Pro.

like image 342
Trevor Avatar asked Jan 28 '11 09:01

Trevor


People also ask

Can I connect to EC2 instance Permission denied Publickey?

To solve the "Permission denied (publickey)" error when trying to SSH into an EC2 instance: Open your terminal in the directory where your private key is located and change its permissions to only be readable by the current user.

How do I fix SSH permission denied Publickey?

If you want to use a password to access the SSH server, a solution for fixing the Permission denied error is to enable password login in the sshd_config file. In the file, find the PasswordAuthentication line and make sure it ends with yes . Find the ChallengeResponseAuthentication option and disable it by adding no .

Why can't I access my EC2 instance?

Verify that your instance is ready Check your instance to make sure it is running and has passed its status checks. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances, and then select your instance.


2 Answers

I'm going to answer my own question in case anyone else sees the same thing... Last night i had done:

ssh-add ~/.ssh/[keypair name] 

then been connecting with:

ssh ec2-user@[ec2 instance ip] 

This morning I tried the same and couldn't connect. But doing

ssh -i ~/.ssh/[keypair name] ec2-user@[ec2 instance ip] 

gets me in.

Using ssh-add on the key pair again gets me in. I'm guessing ssh-add only works within the shell I'd issued it in. When I closed the terminal window and opened another I no longer had that keypair available without being explicit.

like image 97
Trevor Avatar answered Oct 18 '22 01:10

Trevor


This was happening for me because I wasn't using the right username. I was able to log in when using an AMI used in a tutorial I was following, but when I tried to use a different AMI (ubuntu + LAMP from Bitnami) I would get the Permission denied (public key). error. I finally realized that if I changed the username for the tutorial ami from ubuntu to ec2-user I would get the same error.

So a quick google tells that the username for Bitnami AMIs is bitnami. Problem solved.

like image 43
RyanM Avatar answered Oct 18 '22 03:10

RyanM