I am new to EC2. I created my security credentials from this site:
http://paulstamatiou.com/how-to-getting-started-with-amazon-ec2
It worked great, I rebooted and now when I try to connect I get a login/password prompt. (Which I never set up.) After several attempts I get this error:
Permission denied (publickey,gssapi-with-mic).
What am I doing wrong?
I recommend against setting a password as some other answers suggest. Using the key file is both safer (no one can guess your passwords) and more convenient (once you set up a config file). Here's a basic
~/.ssh/config
:Now you can just type
ssh my-ec2-server
and you're in! And as also mentioned in other answers, use -v to get extra info when your connection isn't working.The key for me to be able to connect was to use the "ec2-user" user rather than root. I.e.:
In my case it's because the permission for my home directory is 775, and SSH is not happy about it. It should work after executing:
I had very similar experience this afternoon. I was setting up django on EC2, and suddenly I cannot SSH into the box anymore. Glad I still had an active connection, so I modified
/etc/ssh/sshd_config
to set:and set password for
ec2-user
, then I can login by entering the password.However, after some googling I found this thread: http://ubuntuforums.org/showthread.php?t=577279. It turned out that during my setup of django I changed the permission for my home directory, and SSH is very strict about this. So the file permission must be set correctly.
+1
I noticed that for some AMIs like Amazon Linux, ec2-user@xxx.XX.XX.XXX would work. But for an ubuntu image, I had to use ubuntu@ instead. It was never a problem with the .pem, just with the user name.
If the issue is consistent and happened about 10-15 times in a row even after changing file permissions to 400 or 600, then it is most certainly something is wrong on the ec2 instance, so to make sure:
Check the logs when you try to ssh to the instance by adding -v at the end and see either it gives out anything specific.
Make sure you use the correct name for ssh, like Ubuntu. Perhaps that depends on Linux distribution and users you added and either you've given permission for "root user" ssh.
Then if nothing helps, follow the documentation here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#TroubleshootingInstancesConnectingMindTerm to fix that. It helped in my case, and it happened because of messed up directories/files permissions.
Tagging on to mecca831's answer:
ssh -v -i generated-key.pem ec2-user@11.11.11.11
[ec2-user@ip-11.11.11.11 ~]$ sudo passwd ec2-user newpassword newpassword
[ec2-user@ip-11.11.11.11 ~]$ sudo vi /etc/ssh/sshd_config Modify the file as follows:
Save
[ec2-user@ip-11.11.11.11 ~]$ sudo service sshd stop [ec2-user@ip-11.11.11.11 ~]$ sudo service sshd start
you should be able to exit and ssh in as follows:
and be prompted for password no longer needing the key.