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

当前回答

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

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

 chmod 400 my_file.pem

其他回答

看看这篇文章。您不使用公共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实例。

我知道这个问题已经被回答了,但是对于那些已经尝试过所有这些方法的人来说,你仍然会得到恼人的“拒绝许可(公钥)”。尝试使用SUDO运行您的命令。当然,这是一个临时的解决方案,您应该正确地设置权限,但至少可以让您确定当前用户没有运行所需的权限(正如您所假设的那样)。

Sudo SSH -i amazonec2。pem ec2 - xxx - xxx - xxx xxx.us -西方- 2. compute.amazonaws.com

一旦你这样做了,你会得到这样的消息:

请以“ec2-user”用户而不是“root”用户登录。

这也鲜有文献记载。在这种情况下,只需这样做:

Sudo SSH -i amazonec2。Pem ec2-xxx-xxx-xxx-xxx -us -west-2.compute.amazonaws.com -l ec2-user

你会得到光荣的:

   __|  __|_  )
   _|  (     /   Amazon Linux AMI
  ___|\___|___|

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

openssl rsa -check -in test.pem -noout

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

密钥文件不应该公开可见,所以使用权限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