我在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

当前回答

密钥文件不应该公开可见,所以使用权限400

chmod 400 keyfile.pem

如果以上命令显示权限错误使用

sudo chmod 400 keyfile.pem

现在ssh到ec2机器,如果您仍然面临这个问题,请使用ec2-user

SSH -i keyfile。pem ec2 - user@ec2 - 12 - 34 - 56 - 78. -计算- 1. - amazonaws.com

其他回答

我的问题是我的.pem文件在我的一个NTFS分区中。我把它移动到我的linux分区(ext4)。

通过运行给予所需的权限:

Chmod 400 my_file.pem

这招奏效了。

好吧,唯一对我有用的是:

修改密钥的权限 Chmod 400 mykey.pem 请确保使用ec2-user和正确的ec2-99登录…地址。当您登录并看到您的实例被列出时,ec2-99地址在aws控制台的底部 SSH -i mykey。pem ec2 - user@ec2 - 99 - 99 - 99 - 99. -计算- 1. amazonaws.com

你可以从ASW指南中找到答案。 400通过使其仅为所有者只读来保护它。

chmod 400 mykey.pem

除了其他答案之外,以下是我为了让它起作用所做的事情:

复制密钥到.ssh文件夹,如果你还没有:

cp的关键。pem ~ / . ssh / key.pem

给密钥适当的权限

Chmod 400 ~/.ssh/key.pem

启动ssh-agent(感谢https://stackoverflow.com/a/17848593)

Eval ssh-agent -s ssh-add

然后,添加密钥

ssh-add ~ / . ssh / key.pem

现在你应该能够ssh EC2 (:

如果您是从Windows连接,请在本地计算机上执行以下步骤。

Navigate to your .pem file. Right-click on the .pem file and select Properties. Choose the Security tab. Select Advanced. Verify that you are the owner of the file. If not, change the owner to your username. Select Disable inheritance and Remove all inherited permissions from this object. Select Add, Select a principal, enter your username, and select OK. From the Permission Entry window, grant Read permissions and select OK. Click Apply to ensure all settings are saved. Select OK to close the Advanced Security Settings window. Select OK to close the Properties window. You should be able to connect to your Linux instance from Windows via SSH.

在Windows命令提示符中运行以下命令。

使用实例重置和移除显式权限。 Icacls.exe $path /reset icacls.exe $path / grant:R "$($env:USERNAME):(R)" icacls.exe $path /inheritance:r .执行以下命令,禁用继承并移除继承权限

您应该能够通过SSH从Windows连接到您的Linux实例。