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

当前回答

我看到了这个问题背后的两个原因

1) access key没有正确的权限。具有默认权限的Pem密钥不允许进行安全连接。你只需要更改权限:

Chmod 400 xyz.pem

2)还要检查您是否使用正确的用户凭证登录。否则,在连接时使用sudo

Sudo SSH -i {keyfile} ec2-user@{远程主机ip地址}

其他回答

看看这篇文章。您不使用公共DNS,而是使用表单

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

在AMI面板上可以看到名字的地方

如果您是从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实例。

检查表:

您是否使用正确的私钥.pem文件? 它的权限设置正确吗?(我的亚马逊品牌ami使用644个,但红帽必须至少600或400个。不了解Ubuntu。) 您在ssh行中使用的用户名是否正确?Amazon-branded = "ec2-user", Red Hat = "root", Ubuntu = " Ubuntu "。用户可以指定为“ssh -i pem usename@hostname”或“ssh -l username -i pem hostname”

默认权限不允许pem键。 你只需要更改权限:

Chmod 400 xyz.pem

如果是ubuntu实例,则使用:

SSH -i xyz。pem ubuntu@ec2-youraws.amazonaws.com

解决这个问题的方法是将.pem文件移到apps目录中。fooapp是我的应用程序的名字。我把它直接放在那里。