Amazon EC2 Permission denied (publickey)

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

I installed a new instance of Amazon EC2 using command line tools and connected via SSH, and did some setup work.

Initially, although I could not connect to the instance, I had to stop and restart the instance, after which I was able to connect. Before rebooting, I received a response.

Permission denied (publickey). 

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

 Permission denied (publickey). 

I tried to reload the instance without joy.

Can someone point me in the right direction? The same team that worked last night doesn't work anymore, I'm connecting to my Macbook Pro.

+72
ssh amazon-web-services amazon-ec2
Jan 28 2018-11-11T00:
source share
14 answers

I am going to answer my question if someone else sees the same ... Last night I did:

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

then connected to:

 ssh ec2-user@[ec2 instance ip] 

This morning I tried the same thing and could not connect. But do

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

turns me on.

Using ssh-add in the key pair again makes me log in. I assume ssh-add only works inside the shell in which I released it. When I closed the terminal window and opened another, I no longer had this key pair available without an explicit one.

+75
Jan 28 '11 at 9:50 a.m.
source share

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

So, quick google reports that the username for AMI Bitnami is bitnami . The problem is solved.

+28
May 29 '13 at 1:28
source share

I had a similar problem, and it turned out that it resolves the home folder. Fortunately, I still had another existing ssh connection, so I was able to check the log on an ec2 instance:

$ sudo less / var / log / secure

which contained:

 Dec 9 05:58:20 ... sshd[29816]: Authentication refused: bad ownership or modes for directory /home/ec2-user 

This was fixed by issuing the command:

$ chmod og-rwx / home / ec2-user

I hope this helps someone else.

+14
Dec 09 '11 at 7:10
source share

Note that after restarting the instance, the dns name has changed. I fell for it several times. The key file is still valid, but the "server name" has changed.

+12
Jan 18 '12 at 22:10
source share

Thank!

I really appreciate @Trevor's answer here. I am going to add this little trick that I am now using to avoid this problem in the future.

Convenience

Since you need to create a different key pair for each availability zone, it becomes quite a challenge to manage all of them and the teams that use them. When configured correctly in ~/.ssh/config my ssh command is simple as:

 ssh ec2-52-10-20-30.us-west-2.compute.amazonaws.com 

This is the full public DNS server in the US West 2 access zone. Because of this, the correct username and key are selected:

 ## ~/.ssh/config Host *.us-west-2.compute.amazonaws.com User ec2-user IdentityFile ~/.ssh/bruno-bronosky-aws-us-west-2.pem 
+3
Mar 19 '15 at 15:55
source share

If an EC2 instance uses Ubuntu ami 14.04. Try adding "ubuntu @" before the EC2 ip instance.

 ssh -i [key name] ubuntu@[EC2 instance ip] 
+2
Apr 21 '17 at 4:45
source share

Make sure the path to your private key is correct.

If your ssh client cannot find the private key that you are trying to provide, oddly enough, it will not give you an error! he simply will not use this key. It will use what you ever had under the commands .ssh / id_dsa and .ssh / id_ecdsa, which, of course, will weaken public key authentication.

+1
Mar 12 '13 at 16:46
source share

I solved this by copying the contents of ~ / .ssh / id_rsa.pub to ~ / .ssh / authorized_keys in an EC2 instance.

This is stated in the documentation: http://docs.aws.amazon.com/opsworks/latest/userguide/security-ssh-access.html

Then I could ssh use this command:

 ssh ec2-user@[ip.address] 
0
Jan 21 '15 at 2:04
source share

I searched the Internet all day for an answer. My question is the same. I was busy with the issue of permission, changed back and forth, but no one solved my problem. After testing with a new key and starting / ending several instances, I found that it relates to the same key name in different regions.

Like this: "I was allowed (publication)":
1. Follow the instructions, select us-east-1 as the default zone
2. Create the key name "mykey"
3. Exploring the world of AWS by following the examples in this book.
4. Once you try to check the speed in the Sydney zone, switch to the default Sydney zone.
5. Create another key, calling it "mykey" without hesitation, but do not use it to connect via cli for a couple of days. 6. Try connecting to AWS using cli.
7. Received "Permission denied (publickey)".
8. I worked for many hours to debug the ssh problem until I noticed a problem with the key / zone.

Hope this helps beginners like me.

To avoid this problem, I believe that it is best practice to name a key by joining an area to it.

0
Oct 20 '16 at 6:32
source share

I also got: Permission denied.

I used:

 ssh -v -i ~/.ssh/pemfile ec2-user@xx.xx.xx.xx 

and the answer was:

 debug1: No more authentication methods to try. 

Enter the command:

 ssh-add -l 

But the answer was empty

So, I think there is something wrong with the format in the pen file. Then I found the pen file downloaded from the ec2 website and moved it. Before that, I created a new file and parsed the text from the downloaded pem file into the ".ssh" directory, and then:

 ssh-add filename 

It was a success.

0
Oct 21 '16 at 16:01
source share

I changed the permissions to 600, although the permissions for the pem file were already 644. And it worked: p hope this helps

0
Aug 29 '17 at 8:12
source share

There was the same problem, here is what you have to do. First of all, if you have Windows, use the Babun command line, which is similar to Linux. After receiving this command line, open it and enter ssh-i [key pair path] [username]@[EC2 public IP]. To find the path for a key pair, go to the file where your key is stored, hold Shift and right-click and click Copy Path, and then paste it to where this path goes in the above command. You will probably get "" marks on the outside of the path you inserted and \ backslash. Remove the "" marks and replace the \ backslash with regular slashes /. It worked in a situation like mine, good luck to you.

0
Apr 7 '18 at 22:59
source share

Connecting to EC2 from cli is a bit complicated, at least for the first time. If you go to "

Services -> Compute -> EC2 -> Launch Instances> and select the instance you want ssh -> connect

"Then you will see a dialog box describing how to connect to it. Part of this is shown below.

enter image description here

If you use number 4 without the ec2-user@ preceding it, you will get

 Permission denied (publickey). 

Just copy and paste the one below in "Example:.

0
Jan 16 '19 at 7:07
source share

In my case, the reason was because I changed the permissions of the root folder using chmod. On the AWS website, they describe a long way to change permissions using a different temporary instance. However, I just interrupted the old instance and started another, and this time did not make any changes to the permissions of the root directory, and everything is in order.

0
Jul 25 '19 at 20:38
source share



All Articles