7
  1. The pem file is generated by AWS console
  2. I was able to connect using the EC2 remote web interface, using the same pem file
  3. The permission of pem is 600 already

Error..

ssh -i ~/test.pem -v [email protected]

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/john/test.pem
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/home/john/test.pem':

Anyone can help?

6
  • Presuming that you know the passphrase, you can remove it with: openssl rsa -in test.pem -out test-nopass.pem (which will prompt you for the passphrase and save the unencrypted key for you). If this is not the case, your key may have been inadvertently modified at some point, in which case, you will need a backup of the original key to get back into those instances using that key.
    – cyberx86
    Apr 16, 2012 at 17:28
  • There is no passphrease. I can use the same pem file in AWS web interface for remote login without any passphrase.
    – Howard
    Apr 16, 2012 at 17:51
  • 1
    Depending on how you saved your key, the problem could be line-endings (the Java client is likely more lax than openSSH on this front). This is more likely if you copied and pasted the output (as opposed to redirecting the output). Check the key with openssl rsa -in test.pem -check and (even though you don't have a passphrase) try the command suggested earlier (and then change your ssh command to use the new key file) as it will write the key to a new file (possibly fixing the problem if it is just a line-ending issue, if it can read the file).
    – cyberx86
    Apr 16, 2012 at 18:22
  • 1
    in my case, I miss one dash "-" at the end of pem file. After I add it back. it won't ask passphrase anymore. Ming
    – user73360
    Mar 10, 2017 at 10:45
  • I had the same issue when trying to connect to ec2 instance with public key insteed of private key.
    – burtsevyg
    Apr 5, 2019 at 14:06

12 Answers 12

-4

If sharing a private key between 2 or more ec2 instances and if you try to establish a ssh connection from a ssh connection on ec2, use notepad to open .pem file on your local machine and copy the contents to the new .pem file you're creating in the ssh terminal. It will work 100% and won't ask you for any passphrase.

If you open your local .pem file with other text editors i.e. VSCode you will be asked for the passphrase when trying to use your new .pem file.

3
  • 6
    This answer makes no sense. What specific course of action are you trying to suggest?
    – voretaq7
    Jun 11, 2013 at 0:34
  • I had the same issue when trying to connect to ec2 instance with public key insteed of private key.
    – burtsevyg
    Apr 5, 2019 at 14:08
  • if you're copying the content of a shared private key - open it with notepad and copy the contents - using other text editors such as VSCode will somehow require a passphrase.
    – MattBH
    Dec 20, 2021 at 13:12
7

Most likely your test.pem is corrupt.

To check if it is fine run openssl rsa -check -in test.pem -noout

It should say "RSA key ok"

3

I have also faced this problem a few times and found a solution that works for me and hope will work for you too. Most of the time when we ssh into an ec2 instance(say instance 1) from another ec2 instance(say instance 2) that is connect via ssh from our local machine we need a private key file i.e a ".pem" file to authenticate the identity of instance-2 to connect to instance-1. When we copy the content of the .pem file from our local machine to instance-2, sometimes what happens is, we open the .pem file in a text editor and copy the content and when we paste the file content in the file created in instance-2, we copy it using some other text editor. Here the problem arises.

The solution is -> Always use the same text editor in both the machines to copy and paste the content of .pem files otherwise the format will be changed and you find yourself in trouble.

1
  • 1
    I used 'cat' to copy the key of the .pem file and then pasted it using 'nano'. I was able to connect when I copied and pasted using 'nano' for both.
    – Larney
    Jul 8, 2020 at 11:40
1

I had this issue when I was trying to login from a NAT instance that I set up. The NAT instance is not something I can specify (to the best of knowledge) what OS to use, and I presume that it is using AWS CentOS. Anyways, I was always being asked for a passphrase when trying to connect to an instance on my private network. Here was how i resolved it, although it wasn't entirely apparent to me at the time.

[ec2-user@ip-10-2-3-192 ~]$ sudo bash
[root@ip-10-2-3-192 ~]$ ssh -i /home/test.pem [email protected]

Hope this helps

1
1

I had the same issue, I was trying to ssh to a private ec2 instance from a public ec2 instance, and I kept getting the "Enter passphrase for key <>" "Permission denied (publickey)." error.

What I did was create a .ppk from the .pem file with PuttyGen, then copy-pasted the contents of that .ppk file into a new file created with vi editor. And then I was trying to ssh with this file. I realized the error from the contents of the file when I re-opened it again in vi editor, it was not the same as the .pem file originally downloaded. So, I removed the erroneous file, copy-pasted the contents from the .pem file to a new file created with vi editor, then was successful to ssh to my private instance from my public instance.

0

I had the same problem. I got a request to enter a passphrase when I connected through linux, even though I connected using puTTY without a passphrase. Here is how I solved it: When I needed to copy-paste the key, I opened it with notepad and copied from there, instead of using the puTTY key generator. Then when I did the 0600 and ssh everything worked without requesting a passphrase.

0

I had exactly the same, problem was in not valid pem file (i have created it by vim and paste there key value) but in someway format of pem file was not right. I have created the same pem file in windows using notepad and moved it to my linux machine. And than everything worked for me.

0

I faced the same issue in AWS.

I am trying to access the instance from a private subnet through public. Whenever i run the command ssh -i key.pem ec2-user@**private ip** (from a linux terminal), I get the following prompt:

Enter passphrase of key.pem:

So what I did was to run the following command on the file:

$vi key.pem

I saw whatever .pem (private key) I copied from the notepad it has not copied fully.

It was missing part of the header:

-----BEGIN.

So i write again correctly and resolved the issue. I hope it would be helpful for some people.

Thanks

0

Yes I also faced the same issue. I just fixed mine now by going into the text editor, vi KeyPair.pem and making sure that ---BeGin and --End were both added along with the private key. Once I added those 2 lines, boom! Ubuntu was up and running! Thanks everyone. You are all amazing people!

1
  • 1
    It is unlikely that was the OP's problem.
    – RalfFriedl
    Oct 26, 2019 at 6:28
0

facing issues connecting from your public instance to your private instances

my problem was my fudged up key file. When copying and pasting. I just used scp to copy the original to the public instance.

scp -i <yourkey>.pem <yourkey>.pem ec2-user@<publicip>:~/

this passed openssl checks

-1

I faced the same issue. The best solution is to upload the private key to a private S3 bucket and then copy the file from the S3 bucket to your ssh client.

3
  • 2
    You don't upload the private key anywhere! Then it is no longer private. Feb 10, 2018 at 18:45
  • I disagree, the private key might be shared by a team that needs to access a server. If the key is uploaded to a repository whose access is restricted to specific users, then that's fine. Public and private when referring to keys refer to their capacity to be shared between unsecured networks, such as the internet. If the repository is secured, it's still private.
    – TZubiri
    Apr 24, 2019 at 19:37
  • What you are essentially doing is delegating the security aspect to the access control of the s3 bucket. The total security will be as strong as the weakest link, so unless you secure the s3 bucket with another private key, you are weakening the security of the system.
    – TZubiri
    Apr 24, 2019 at 19:49
-1

I faced same issue, while doing connecting my public instance to private instance, My issue is resolved.

You can to the below things.

  1. While copying key use editor and copy it properly.

  2. Or copy .pem key to s3 bucket and from their download it to public instance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .