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

当前回答

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

复制密钥到.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 (:

其他回答

2022年2月更新:

参见在AWS上ssh到EC2实例的描述:

然后,你可以在下面看到“No.3”说:

所以,运行下面的命令,如“No.3”所示:

chmod 400 myKey.pem

这只是您的aws pem密钥的权限问题。

使用以下命令将pem密钥的权限更改为400即可。

chmod 400 pemkeyname.pem

如果您没有权限更改文件的权限,您可以使用sudo像下面的命令。

sudo chmod 400 pemkeyname.pem

否则,如果没有任何工作,请按照本视频更改EC2实例上的密钥。现在可以在实例上安装公共/私有密钥对。

https://youtu.be/LvLlRCrS8B4

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

# #第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实例。

在windows中,

右键单击pem文件。然后选择属性。 选择安全选项卡——>单击“高级”按钮——>禁用继承——>删除该对象的所有继承权限 点击“添加”按钮——>选择主体——>在输入框中输入用户名——>点击“检查名称”按钮——>点击“确定”——>点击“确定”——>点击“确定”——>点击“确定”