Permissions error when connecting to EC2 via SSH o

2020-05-14 18:51发布

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?

15条回答
闹够了就滚
2楼-- · 2020-05-14 19:31

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:

Host my-ec2-server
  HostName 11.11.11.11
  User ec2-user
  IdentityFile /path/to/generated-key.pem

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.

查看更多
Emotional °昔
3楼-- · 2020-05-14 19:32

The key for me to be able to connect was to use the "ec2-user" user rather than root. I.e.:

ssh -i [full path to keypair file] ec2-user@[EC2 instance hostname or IP address]
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-05-14 19:36

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:

server$ chmod go-w ~/
server$ chmod 700 ~/.ssh
server$ chmod 600 ~/.ssh/authorized_keys

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:

PasswordAuthentication yes

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.

查看更多
聊天终结者
5楼-- · 2020-05-14 19:41

+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.

查看更多
家丑人穷心不美
6楼-- · 2020-05-14 19:41

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:

  1. 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.

  2. 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.

查看更多
孤傲高冷的网名
7楼-- · 2020-05-14 19:42

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:

    # To disable tunneled clear text passwords, change to no here!
    PasswordAuthentication yes
    #PermitEmptyPasswords no
    # EC2 uses keys for remote access
    #PasswordAuthentication no

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:

ssh ec2-user@11.11.11.11

and be prompted for password no longer needing the key.

查看更多
登录 后发表回答