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

当前回答

我知道这个问题已经被回答了,但是对于那些已经尝试过所有这些方法的人来说,你仍然会得到恼人的“拒绝许可(公钥)”。尝试使用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
  ___|\___|___|

其他回答

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

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

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

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

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

Chmod 400 my_file.pem

这招奏效了。

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

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

Chmod 400 xyz.pem

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

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

我知道现在已经很晚了……但这对我来说总是有效的:

# #第1步

ssh-add ~/.ssh/KEY_PAIR_NAME.pem

##第二步,简单地SSH在:)

ssh user_name@<instance public dns/ip>

e.g.

ssh ec2-user@ec2-198-51-100-1.compute-1.amazonaws.com

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