我在Amazon EC2上创建了一个新的linux实例,并下载了.pem文件以允许我SSH登录。

当我试图ssh与:

ssh -i myfile.pem <public dns>

我有:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         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).

在这篇文章之后,我尝试chmod +600 .pem文件,但现在当我ssh时,我只是得到

Permission denied (publickey).

我犯了什么小学生的错误? pem文件在我的主文件夹中(在macOS中)。它的权限是这样的:

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

当前回答

使用PuTTY登录。它很好,但需要几个步骤。

Get your .pem that was generated when you first made the EC2 instance. Convert the .pem file .ppk using PuttyGen since PuTTY does not read .pem. 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. Then navigate to Connection > SSH > Auth. Then add your .ppk file. Click on Browse where it says "Private key file for authentication". Click Open and you should be able to immediately establish connection.

我使用PuTTY 0.66在Windows。

其他回答

嗯,看了你的帖子描述,我觉得你犯了两个错误:-

设置正确的私钥权限。 下面的命令将帮助您设置正确的文件权限。 Chmod 0600 mykey.pem 您试图登录的ec2用户错误。 查看您的调试日志,我认为您已经生成了一个Amazon linux实例。该实例类型的默认用户是ec2-user。如果实例是ubuntu,那么默认用户就是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.

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

Linux用户使用。pem文件连接服务器的简单步骤如下:

步骤1:拷贝到pem文件的位置,并将其复制到home .ssh位置。

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

步骤2:修改权限

chmod 400 ~/.ssh/example.pem

步骤3:执行以下命令

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

因为这个命令太长了,所以你应该使用以下命令创建这个的别名:

 vim ~/.bashrc

最后用下面的方式写同样的命令。

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

现在重新启动系统并使用sshConnect连接服务器。

默认情况下,无论何时下载密钥文件,它都带有644个权限。

因此,每次下载新密钥时,都需要更改权限。

 chmod 400 my_file.pem

您还应该检查.pem文件是否已损坏。我花了大约一个小时挠头,决定用这条线检查一下

openssl rsa -check -in test.pem -noout

如果它返回“RSA key ok”,那么就没问题了。如果没有,请确保您有正确的文件,或出于某种原因正确地复制了它。

修改密钥文件的权限:

chmod 400 key-file-name.pem

有关连接到实例的信息,请参阅AWS文档:教程:开始使用Amazon EC2 Linux实例