968

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 macOS). Its permissions look like this:

-rw-------@   1 mattroberts  staff    1696 19 Nov 11:20 amazonec2.pem
Community
  • 1
  • 1
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180
  • 2
    This error also comes when you are using wrong pem file. – Rahul Prasad Nov 23 '12 at 07:24
  • Also make sure you created your instance AFTER you created and selected the key pair as the designated key to use. I did this the other way around. – Gary Apr 15 '16 at 12:20
  • I'm in Windows with WinSCP. There is [nothing to do](http://stackoverflow.com/a/38416905/4058484) with `chmod 400 myfile.pem` as it uses `myfile.ppk` generated by PuTTYgen from the pem file. – eQ19 Jul 17 '16 at 00:44
  • Important: It also indicates that the file could be accessed by other users in the windows. SO you can change the file permissions by right clicking the key file in windows and limit permissions. (Full instructions with screenshots here: https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open) WORKED FOR ME – Hissaan Ali Apr 12 '22 at 11:03

43 Answers43

1895

The problem is wrong set of permissions on the file.

Easily solved by executing -

chmod 400 mykey.pem

Taken from AWS 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.

Kof
  • 23,893
  • 9
  • 56
  • 81
286

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
Till
  • 22,236
  • 4
  • 59
  • 89
77

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 ec2-user@ec2-198-51-100-1.compute-1.amazonaws.com
starball
  • 20,030
  • 7
  • 43
  • 238
user2838357
  • 901
  • 6
  • 6
  • is "ssh-add" the same as just copying *.pem file to ~/.ssh folder? – Incerteza Mar 13 '14 at 05:17
  • 1
    >> just copying *.pem file to ~/.ssh folder Is not same, you must add to folder and then run command ssh-add. – super_p Apr 30 '14 at 07:29
  • Very late to the game, but to add some clarification... 1. add the .pem file to the ~/.ssh directory (create it if necessary), 2. use the ssh-add command to add the identity to the authentication agent; this means never having to specify the .pem file when using ssh – Ian Atkin Nov 02 '14 at 08:00
  • 2
    After ssh-add ¬/.ssh/key.pem; Getting the error Could not open a connection to your authentication agent. eval `ssh-agent -s` reports SSH_AGENT_PID=3409; ssh-add gives the same error as above.........Any help here plz – Tariq Nov 28 '14 at 13:05
  • Wow that would be very convenient for all my future connections to my VPS. Thanks mate :) – Ahmad Mushtaq May 10 '16 at 03:57
42

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 ec2-user@ec2-99-99-99-99.compute-1.amazonaws.com

bobobobo
  • 64,917
  • 62
  • 258
  • 363
35

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

ssh -i your.pem root@ec2-XXX-XXX-XXX-XXX.z-2.compute-1.amazonaws.com

where the name is visible on your AMI panel

renick
  • 3,873
  • 2
  • 31
  • 40
  • minor improvement: when trying to log in as root aws prompts the following: "Please login as the ec2-user user rather than root user." – Andre Schweighofer Aug 08 '12 at 18:26
  • The 'root@' is what everyone else is missing in this answer. Yours helped! That, and chmod. – lordB8r Nov 05 '14 at 18:34
  • @lordB8r the username depends on which AMI is used. See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#TroubleshootingInstancesConnectingPuTTY – qwr Dec 07 '20 at 21:00
28

In windows you can go to the properties of the pem file, and go to the security tab, then to advance button.

remove inheritance and all the permissions. then grant yourself the full control. after all SSL will not give you the same error again.

18

Change permission for the key file with :

chmod 400 key-file-name.pem

See AWS documentation for connecting to the instance: Tutorial: Get started with Amazon EC2 Linux instances

Abdullah Khawer
  • 4,461
  • 4
  • 29
  • 66
Ayush Goyal
  • 201
  • 2
  • 3
12

I know this question has been answered already but for those that have tried them all and you are still getting the annoying "Permission denied (publickey)". Try running your command with SUDO. Of course this is a temporary solution and you should set permissions correctly but at least that will let you identify that your current user is not running with the privileges you need (as you assumed)

sudo ssh -i amazonec2.pem ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com

Once you do this you'll get a message like this:

Please login as the user "ec2-user" rather than the user "root"

Which is also sparsely documented. In that case just do this:

sudo ssh -i amazonec2.pem ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com -l ec2-user

And you'll get the glorious:

   __|  __|_  )
   _|  (     /   Amazon Linux AMI
  ___|\___|___|
Ricardo Cid
  • 199
  • 1
  • 5
11

Feb, 2022 Update:

See the description to ssh to EC2 instance on AWS:

enter image description here

Then, you can find "No.3" saying this below:

enter image description here

So, run the command below as "No.3" says above:

chmod 400 myKey.pem
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
8

In Mac terminal, doing "chmod 400 xyz.pem" did not help me, it kept saying permission denied. For ubuntu users I would suggest

  1. ssh-add xyz.pem
  2. ssh -i xyz.pem ubuntu@ec2-54-69-172-118.us-west-2.compute.amazonaws.com (notice the user is ubuntu)
Xavi
  • 20,111
  • 14
  • 72
  • 63
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
5

ssh -i /.pem user@host-machine-IP

I think it's because either you have entered wrong credentials or, you are using a public key rather than private key or, your port permissions are open for ALL to ssh. This is bad for Amazon.

Syed Priom
  • 1,893
  • 1
  • 21
  • 22
5

There can be three reasons behind this error.

  1. Your are using a wrong key.
  2. Your key doesn't have the correct permissions. You need to chmod it to 400.
  3. You are using the wrong user. Ubuntu images have a user ubuntu, Amazon's AMI is ec2-user and debian images have either root or admin
Jagatveer Singh
  • 185
  • 2
  • 12
5

In addition to the other answers, here is what I did in order for this to work:

  • Copy the key to .ssh folder if you still hadn't:

cp key.pem ~/.ssh/key.pem

  • Give the proper permissions to the key

chmod 400 ~/.ssh/key.pem

eval `ssh-agent -s` ssh-add

  • Then, add the key

ssh-add ~/.ssh/key.pem

Now you should be able to ssh EC2 (:

Rodrigo Nantes
  • 77
  • 1
  • 10
5

In windows,

  • Right click on the pem file. Then select properties.
  • Select security tab --> Click on the Advanced button --> Disable inheritance --> Remove all inherited permissions from this object image1
  • Click on the Add button --> Select a principal --> Enter your username on the inputbox --> Click on the Check Names button --> Click on Ok --> Click on Ok --> Click on Ok --> Click on Ok image2
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
4

SSH keys and file permission best practices:

  • .ssh directory - 0700 (only by owner)
  • private key/.pem file - 0400 (read only by owner)
  • public key/.pub file - 0600 (read & write only by owner)

    chmod XXXX file/directory

Ryan
  • 3,085
  • 5
  • 27
  • 31
4

Alternative log-in using PuTTY. Its good but needs a few steps.

  1. Get your .pem that was generated when you first made the EC2 instance.
  2. Convert the .pem file .ppk using PuttyGen since PuTTY does not read .pem.
  3. Open PuTTY and enter your Host Name which is your instance username + Public DNS (Ex. ubuntu@ec2-xxx-xxx-xxx-xxx.region.compute.amazonaws.com). Not your AWS account username.
  4. Then navigate to Connection > SSH > Auth. Then add your .ppk file. Click on Browse where it says "Private key file for authentication".
  5. Click Open and you should be able to immediately establish connection.

Im using PuTTY 0.66 in Windows.

Community
  • 1
  • 1
jarvis
  • 297
  • 4
  • 13
  • This works, but is there a way to make the ssh connection work from the command line directly? – Ariel Mar 20 '18 at 12:45
4

By default whenever you download the keyfile it come with 644 permissions.

So you need to change the permission each time you download new keys.

 chmod 400 my_file.pem
4

In Windows go to the .pem file, right click and select Properties.

  • Go to Advanced in Security tab

  • Disable and remove inheritance.

  • Then press Add and select a principal.

  • Add account username as object name and press ok.

  • Give all permission.

  • Apply and save changes.

Now check the above command

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
User
  • 1,460
  • 14
  • 11
3

You can find the answer from the ASW guide. 400 protects it by making it read only and only for the owner.

chmod 400 mykey.pem
SeniorEngineer
  • 308
  • 1
  • 10
2

Do a chmod 400 yourkeyfile.pem If your instance is Amazon linux then use ssh -i yourkeyfile.pem ec2-user@ip for ubuntu ssh -i yourkeyfile.pem ubuntu@ip for centos ssh -i yourkeyfile.pem centos@ip

sathee005
  • 67
  • 3
2

Just change the permission of pem file to 0600 allowing only for the allowed user and it will work like charm.

sudo chmod 0600 myfile.pem

And then try to ssh it will work perfectly.

ssh -i myfile.pem <<ssh_user>>@<<server>>
Prash
  • 558
  • 5
  • 8
2

BY default permission are not allowing the pem key. You just have to change the permission:

chmod 400 xyz.pem

and if ubuntu instance then connect using:

ssh -i xyz.pem ubuntu@ec2-youraws.amazonaws.com

rahul kumar
  • 135
  • 6
2

The issue for me was that my .pem file was in one of my NTFS partitions. I moved it to my linux partition (ext4).

Gave required permissions by running:

chmod 400 my_file.pem

And it worked.

Rishabh Agrahari
  • 3,447
  • 2
  • 21
  • 22
2

I have seen two reasons behind this issue

1) access key does not have the right permission. pem keys with default permission are not allowed to make a secure connection. You just have to change the permission:

chmod 400 xyz.pem

2) Also check whether you have logged-in with proper user credentials. Otherwise, use sudo while connecting

sudo ssh -i {keyfile} ec2-user@{ip address of remote host}

2

Well, looking at your post description I feel there were 2 mistakes done by you:-

  1. Set correct permissions for the private key. Below command should help you to set correct file permision.

    chmod 0600 mykey.pem

  2. Wrong ec2 user you are trying to login.

    Looking at your debug log I think you have spawned an Amazon linux instance. The default user for that instance type is ec2-user . If the instance would have been ubuntu then your default user would have been ubuntu .

    ssh -i privatekey.pem default_ssh_user@server_ip

Note:
   For an Amazon Linux AMI, the default user name is ec2-user.

   For a Centos AMI, the default user name is centos.

   For a Debian AMI, the default user name is admin or root.

   For a Fedora AMI, the default user name is ec2-user or fedora.

   For a RHEL AMI, the default user name is ec2-user or root.

   For a SUSE AMI, the default user name is ec2-user or root.

   For an Ubuntu AMI, the default user name is ubuntu.

   Otherwise, if ec2-user and root don't work, check with the AMI provider.

source: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

2

Key file should not be publicly viewable so use permission 400

chmod 400 keyfile.pem

If above command shows permission error use

sudo chmod 400 keyfile.pem

Now ssh into the ec2 machine, if you still face the issue, use ec2-user

ssh -i keyfile.pem ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com

singh30
  • 1,335
  • 17
  • 22
2

You're not in root then run this command

sudo chmod 400 -R myfile.pem

Not is root then run this command

chmod 400 -R myfile.pem
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
CHAVDA MEET
  • 777
  • 8
  • 14
2

If you are connecting from Windows, perform the following steps on your local computer.

  1. Navigate to your .pem file.

  2. Right-click on the .pem file and select Properties.

  3. Choose the Security tab.

  4. Select Advanced.

  5. Verify that you are the owner of the file. If not, change the owner to your username.

  6. Select Disable inheritance and Remove all inherited permissions from this object.

  7. Select Add, Select a principal, enter your username, and select OK.

  8. From the Permission Entry window, grant Read permissions and select OK.

  9. Click Apply to ensure all settings are saved.

  10. Select OK to close the Advanced Security Settings window.

  11. Select OK to close the Properties window.

  12. You should be able to connect to your Linux instance from Windows via SSH.

From a Windows command prompt, run the following commands.

  1. Run the following command to reset and remove explicit permissions: icacls.exe $path /reset
  2. Run the following command to grant Read permissions to the current user: icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
  3. Run the following command to disable inheritance and remove inherited permissions : icacls.exe $path /inheritance:r

You should be able to connect to your Linux instance from Windows via SSH.

Keith
  • 37
  • 1
2

It is just a permission issue with your aws pem key.

Just change the permission of pem key to 400 using below command.

chmod 400 pemkeyname.pem

If you don't have permission to change the permission of a file you can use sudo like below command.

sudo chmod 400 pemkeyname.pem

Else if nothing works for you just follow this video to change the keys on your EC2 instance. You can install now public / private key pair on your instance.

https://youtu.be/LvLlRCrS8B4

Ujwal Abhishek
  • 328
  • 1
  • 6
1

Checklist:

  1. Are you using the right private key .pem file?

  2. Are its permissions set correctly? (My Amazon-brand AMIs work with 644, but Red hat must be at least 600 or 400. Don't know about Ubuntu.)

  3. Are you using the right username in your ssh line? Amazon-branded = "ec2-user", Red Hat = "root", Ubuntu = "ubuntu". User can be specified as "ssh -i pem usename@hostname" OR "ssh -l username -i pem hostname"

fivedogit
  • 8,374
  • 7
  • 34
  • 43
1

Following are the simple steps for Linux user to connect with the server using .pem file:

Step1: To to the location of pem file and copy it to home .ssh location.

cp example.pem ~/.ssh/example.pem

Step2: Change the permission

chmod 400 ~/.ssh/example.pem

Step3: Run the following command

ssh -i ~/.ssh/example.pem ec2-user@host.com

As this command is too long so you sould create the alias of this using following commands:

 vim ~/.bashrc

Write the same command in the following manner at the last.

alias sshConnect='ssh -i ~/.ssh/example.pem ec2-user@host.com'

Now restart your system and use sshConnect to connect with your server.

Ajai
  • 2,492
  • 1
  • 14
  • 23
1

It is just a permission issue with your aws pem key.

Just change the permission of pem key to 400 using below command.

chmod 400 pemkeyname.pem

If you don't have permission to change the permission of a file you can use sudo like below command.

sudo chmod 400 pemkeyname.pem

I hope this should work fine.

Deepak N
  • 1,408
  • 3
  • 15
  • 37
1

.400 protects it by making it read only and only for the owner.
You can find the answer from the ASW guide.

chmod 400 yourPrivateKey.pem

enter image description here

Zgpeace
  • 3,927
  • 33
  • 31
1

What did it for me is editing the default security group to allow for inbound TCP traffic at port 22:

enter image description here

Jason
  • 2,495
  • 4
  • 26
  • 37
1

If you are on windows 10 using the ubuntu subsystem, and if you sudo chmod to change the key to 400, then it may still error with "Load key pem: Permission denied"

ls -al and you will see root now owns the file! chown it to your logged in user and then it will work.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

You would need to put some conservative permissions on the key file (myfile.pem). Try changing it to r-------- OR 400

Sunil
  • 707
  • 1
  • 7
  • 13
1

for windows 10.

. Right click file . properties->security-> disable inheritance .now add -> your user(window) with only "read" . Click ok

now its working for me

1

Windows 10 - PowerShell

icacls.exe .\Desktop\xxxx.pem /reset
icacls.exe .\Desktop\xxxx.pem /grant:r "$($env:USERNAME):(r)"
icacls.exe .\Desktop\xxxx.pem /inheritance:r

ssh -i .\Desktop\xxxx.pem ec2-user@54.2xx.xxx.x

macos & linux

chmod 400 ~/Desktop/xxxx.pem

ssh -i ~/Desktop/xxxx.pem ec2-user@54.2xx.xxx.x
Fatih Ertuğral
  • 230
  • 2
  • 4
0

What fixed this for me was to move the .pem file within the apps directory. Soo say fooapp is the name of my app. I placed it directly in there.

Nick Res
  • 2,154
  • 5
  • 30
  • 48
0

You should also check if your .pem file is not corrupted. I spent about an hour scratching my head and decided to check using this line

openssl rsa -check -in test.pem -noout

If it returns "RSA key ok" then you are good. If not, make sure you have the right file and or copied it correctly for whatever reason.

Ega
  • 437
  • 4
  • 12
0

there's notes when you creating new EC2 instance that pormote you to change the file permtion

Easily solved by executing -

chmod 400 mykey.pem

0

One thing I like doing in this matter, is to use an alias and add it to the .bashrc file so that I don't have to write connect commands or get back to the key each time I need to SSh the EC2 instance.

Here is how I do it:

vim .bashrc

Add the following content to the end of the file

# Custom fields

###[ MY APP 1 NAME ]###

# APP 1 Dev env EKS cluster bastion host
alias app1_dev="ssh -i ~/.ssh/app1-dev-bastion.pem USER@IPv4_ADDRESS"

###[ MY APP 2 NAME ]###
# APP 2 Stg env CodeDeploy instance
alias app_stg_cd="ssh -i ~/.ssh/app2-stg-cd.pem USER@IPv4_ADDRESS"

And then apply changes:

source .bashrc

Sorry if anyone answered this before and I didn't notice, and just wanted to share my own work taste, not like the other fellows didn't answer very well.

Khadjiev
  • 25
  • 7
-3

if don't have permissions don't forget sudo it. sudo ssh -i myfile.pem <<ssh_user>>@<>

Joanale273
  • 1
  • 1
  • 3