"UNPROTECTED PRIVATE KEY FILE!" Error using SSH into Amazon EC2 Instance (AWS)

594,157

Solution 1

The problem is having wrong mod on the file.

Easily solved by executing -

chmod 400 mykey.pem

Taken from Amazon's instructions -

Your key file must not be publicly viewable for SSH to work. Use this command if needed: chmod 400 mykey.pem

400 protects it by making it read only and only for the owner.

Solution 2

You are likely using the wrong username to login, because—

  • Most Ubuntu images have a user ubuntu
  • Amazon's AMI is ec2-user
  • Most Debian images have either root or admin

To login, you need to adjust your ssh command:

ssh -l USERNAME_HERE -i .ssh/yourkey.pem public-ec2-host

Solution 3

I know this is very late to the game ... but this always works for me:

step 1

ssh-add ~/.ssh/KEY_PAIR_NAME.pem

step 2, simply ssh in :)

ssh user_name@<instance public dns/ip>

e.g.

ssh [email protected]

hope this helps someone.

Solution 4

Ok man, the only thing that worked for me was:

  1. Change permissions of the key

    chmod 400 mykey.pem

  2. Make sure to log in using ec2-user, and the correct ec2-99... address. The ec2-99 address is at the bottom of the aws console when you're logged in and seeing your instance listed

    ssh -i mykey.pem [email protected]

Solution 5

Take a look at this article. You do not use the public DNS but rather the form

ssh -i your.pem [email protected]

where the name is visible on your AMI panel

Share:
594,157
Matt Roberts
Author by

Matt Roberts

Updated on February 15, 2022

Comments

  • Matt Roberts
    Matt Roberts over 2 years

    This is probably a stupidly simple question to some :)

    I've created a new linux instance on Amazon EC2, and as part of that downloaded the .pem file to allow me to SSH in.

    When I tried to ssh with:

    ssh -i myfile.pem <public dns>
    

    I got:

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Permissions 0644 for 'amazonec2.pem' are too open.
    It is recommended that your private key files are NOT accessible by others.
    This private key will be ignored.
    bad permissions: ignore key: amazonec2.pem
    Permission denied (publickey).
    

    Following this post I tried to chmod +600 the pem file, but now when I ssh I just get:

    Permission denied (publickey).
    

    What school-boy error am I making here? The .pem file is in my home folder (in osx). It's permissions look like this:

    -rw-------@   1 mattroberts  staff    1696 19 Nov 11:20 amazonec2.pem