简体   繁体   English

“未受保护的私钥文件!” 将 SSH 用于 Amazon EC2 实例 (AWS) 时出错

[英]"UNPROTECTED PRIVATE KEY FILE!" Error using SSH into Amazon EC2 Instance (AWS)

I've created a new linux instance on Amazon EC2, and as part of that downloaded the .pem file to allow me to SSH in.我在 Amazon EC2 上创建了一个新的 linux 实例,并且作为其中的一部分下载了.pem文件以允许我进入 SSH。

When I tried to ssh with:当我尝试ssh时:

ssh -i myfile.pem <public dns>

I got:我有:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         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).

Following this post I tried to chmod +600 the .pem file, but now when I ssh I just get这篇文章之后,我尝试对.pem文件进行chmod +600 ,但是现在当我输入ssh时,我就得到了

Permission denied (publickey).

What school-boy error am I making here?我在这里犯了什么小学生错误? The .pem file is in my home folder (in macOS). .pem文件在我的主文件夹中(在 macOS 中)。 Its permissions look like this:它的权限如下所示:

-rw-------@   1 mattroberts  staff    1696 19 Nov 11:20 amazonec2.pem

The problem is having wrong mod on the file.问题是文件上有错误的mod。

Easily solved by executing -通过执行轻松解决 -

chmod 400 mykey.pem

Taken from AWS instructions -取自 AWS 说明 -

Your key file must not be publicly viewable for SSH to work.您的密钥文件不得公开查看,SSH 才能正常工作。 Use this command if needed: chmod 400 mykey.pem如果需要,请使用此命令:chmod 400 mykey.pem

400 protects it by making it read only and only for the owner. 400 通过使其只读且仅供所有者使用来保护它。

You are likely using the wrong username to login, because—您可能使用了错误的用户名登录,因为——

  • Most Ubuntu images have a user ubuntu大多数 Ubuntu 映像都有一个用户ubuntu
  • Amazon's AMI is ec2-user亚马逊的 AMI 是ec2-user
  • Most Debian images have either root or admin大多数 Debian 映像都有rootadmin

To login, you need to adjust your ssh command:要登录,您需要调整ssh命令:

ssh -l USERNAME_HERE -i .ssh/yourkey.pem public-ec2-host

I know this is very late to the game ... but this always works for me:我知道这对游戏来说已经很晚了......但这总是对我有用:

step 1步骤1

ssh-add ~/.ssh/KEY_PAIR_NAME.pem

step 2, simply ssh in :)第 2 步,只需 ssh 进入 :)

ssh user_name@<instance public dns/ip>

eg例如

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

hope this helps someone.希望这可以帮助某人。

Ok man, the only thing that worked for me was:好吧,唯一对我有用的是:

  1. Change permissions of the key更改密钥的权限

    chmod 400 mykey.pem chmod 400 mykey.pem

  2. Make sure to log in using ec2-user , and the correct ec2-99... address.确保使用ec2-user和正确的 ec2-99... 地址登录。 The ec2-99 address is at the bottom of the aws console when you're logged in and seeing your instance listed当您登录并看到您的实例列出时,ec2-99 地址位于 aws 控制台的底部

    ssh -i mykey.pem ec2-user@ec2-99-99-99-99.compute-1.amazonaws.com ssh -i mykey.pem ec2-user@ec2-99-99-99-99.compute-1.amazonaws.com

Take a look atthis article .看看这篇文章 You do not use the public DNS but rather the form您不使用公共 DNS,而是使用表单

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

where the name is visible on your AMI panel名称在您的 AMI 面板上可见

In windows you can go to the properties of the pem file, and go to the security tab, then to advance button.在 Windows中,您可以转到 pem 文件的属性,然后转到安全选项卡,然后转到高级按钮。

remove inheritance and all the permissions.删除继承和所有权限。 then grant yourself the full control.然后授予自己完全控制权。 after all SSL will not give you the same error again.毕竟 SSL 不会再给你同样的错误。

Change permission for the key file with :使用以下命令更改密钥文件的权限:

chmod 400 key-file-name.pem

See AWS documentation for connecting to the instance:请参阅 AWS 文档以连接到实例:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html#EC2_ConnectToInstance_Linux http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html#EC2_ConnectToInstance_Linux

I know this question has been answered already but for those that have tried them all and you are still getting the annoying "Permission denied (publickey)".我知道这个问题已经得到了回答,但是对于那些已经尝试过所有这些问题的人来说,你仍然会得到恼人的“权限被拒绝(公钥)”。 Try running your command with SUDO.尝试使用 SUDO 运行您的命令。 Of course this is a temporary solution and you should set permissions correctly but at least that will let you identify that your current user is not running with the privileges you need (as you assumed)当然这是一个临时解决方案,您应该正确设置权限,但至少这可以让您确定您当前的用户没有以您需要的权限运行(如您所假设的)

sudo ssh -i amazonec2.pem ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com

Once you do this you'll get a message like this:执行此操作后,您将收到如下消息:

Please login as the user "ec2-user" rather than the user "root"

Which is also sparsely documented.这也很少记录。 In that case just do this:在这种情况下,只需这样做:

sudo ssh -i amazonec2.pem ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com -l ec2-user

And you'll get the glorious:你会得到光荣的:

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

Feb, 2022 Update: 2022 年 2 月更新:

See the description to ssh to EC2 instance on AWS :请参阅SSH 到 AWS 上的 EC2 实例的描述:

在此处输入图像描述

Then, you can find "No.3" saying this below:然后,您可以在下面找到“No.3”

在此处输入图像描述

So, run the command below as "No.3" says above:因此,按照上面的“No.3”运行以下命令:

chmod 400 myKey.pem

In Mac terminal, doing "chmod 400 xyz.pem" did not help me, it kept saying permission denied.在 Mac 终端中,执行“chmod 400 xyz.pem”对我没有帮助,它一直说权限被拒绝。 For ubuntu users I would suggest对于 ubuntu 用户,我建议

  1. ssh-add xyz.pem
  2. ssh -i xyz.pem ubuntu@ec2-54-69-172-118.us-west-2.compute.amazonaws.com (notice the user is ubuntu) ssh -i xyz.pem ubuntu@ec2-54-69-172-118.us-west-2.compute.amazonaws.com (注意用户是ubuntu)

ssh -i /.pem user@host-machine-IP ssh -i /.pem user@host-machine-IP

I think it's because either you have entered wrong credentials or, you are using a public key rather than private key or, your port permissions are open for ALL to ssh.我认为这是因为您输入了错误的凭据,或者您使用的是公钥而不是私钥,或者您的端口权限对所有 ssh 都是开放的。 This is bad for Amazon.这对亚马逊不利。

There can be three reasons behind this error.此错误背后可能有三个原因。

  1. Your are using a wrong key.您使用了错误的密钥。
  2. Your key doesn't have the correct permissions.您的密钥没有正确的权限。 You need to chmod it to 400.您需要将其更改为 400。
  3. You are using the wrong user.您使用了错误的用户。 Ubuntu images have a user ubuntu , Amazon's AMI is ec2-user and debian images have either root or admin Ubuntu 镜像有一个用户ubuntu ,亚马逊的 AMI 是ec2-user和 debian 镜像有rootadmin

SSH keys and file permission best practices: SSH 密钥和文件权限最佳实践:

  • .ssh directory - 0700 (only by owner) .ssh 目录 - 0700(仅限所有者)
  • private key/.pem file - 0400 (read only by owner)私钥/.pem 文件 - 0400(所有者只读)
  • public key/.pub file - 0600 (read & write only by owner)公钥/.pub 文件 - 0600(只能由所有者读写)

    chmod XXXX file/directory

Alternative log-in using PuTTY.使用 PuTTY 进行替代登录。 Its good but needs a few steps.它很好,但需要几个步骤。

  1. Get your .pem that was generated when you first made the EC2 instance.获取您第一次创建 EC2 实例时生成的 .pem。
  2. Convert the .pem file .ppk using PuttyGen since PuTTY does not read .pem.使用PuttyGen将 .pem 文件转换为 .ppk,因为 PuTTY 不读取 .pem。
  3. Open PuTTY and enter your Host Name which is your instance username + Public DNS (Ex. ubuntu@ec2-xxx-xxx-xxx-xxx.region.compute.amazonaws.com).打开 PuTTY 并输入您的主机名,即您的实例用户名 + 公共 DNS(例如 ubuntu@ec2-xxx-xxx-xxx-xxx.region.compute.amazonaws.com)。 Not your AWS account username.不是您的 AWS 账户用户名。
  4. Then navigate to Connection > SSH > Auth .然后导航到Connection > SSH > Auth Then add your .ppk file.然后添加您的.ppk文件。 Click on Browse where it says "Private key file for authentication" .单击“用于身份验证的私钥文件”浏览
  5. Click Open and you should be able to immediately establish connection.单击打开,您应该能够立即建立连接。

Im using PuTTY 0.66 in Windows.我在 Windows 中使用 PuTTY 0.66。

In addition to the other answers, here is what I did in order for this to work:除了其他答案之外,这是我为此所做的工作:

  • Copy the key to .ssh folder if you still hadn't:如果您还没有,请将密钥复制到 .ssh 文件夹:

cp key.pem ~/.ssh/key.pem

  • Give the proper permissions to the key授予密钥适当的权限

chmod 400 ~/.ssh/key.pem

eval `ssh-agent -s` ssh-add

  • Then, add the key然后,添加密钥

ssh-add ~/.ssh/key.pem

Now you should be able to ssh EC2 (:现在您应该可以 ssh EC2 (:

By default whenever you download the keyfile it come with 644 permissions.默认情况下,每当您下载密钥文件时,它都具有 644 权限。

So you need to change the permission each time you download new keys.因此,每次下载新密钥时都需要更改权限。

 chmod 400 my_file.pem

In Windows go to the .pem file, right click and select Properties .在 Windows 中,转到.pem文件,右键单击并选择Properties

  • Go to Advanced in Security tab转到安全选项卡中的高级

  • Disable and remove inheritance.禁用和删除继承。

  • Then press Add and select a principal.然后按添加并选择一个主体。

  • Add account username as object name and press ok.添加帐户用户名作为对象名称,然后按确定。

  • Give all permission.给予所有许可。

  • Apply and save changes.应用并保存更改。

Now check the above command现在检查上面的命令

In windows,在窗户里,

  • Right click on the pem file.右键单击 pem 文件。 Then select properties.然后选择属性。
  • Select security tab --> Click on the Advanced button --> Disable inheritance --> Remove all inherited permissions from this object选择安全选项卡 --> 单击高级按钮 --> 禁用继承 --> 从此对象删除所有继承的权限图像1
  • Click on the Add button --> Select a principal --> Enter your username on the inputbox --> Click on the Check Names button --> Click on Ok --> Click on Ok --> Click on Ok --> Click on Ok点击 Add 按钮 --> Select a principal --> 在输入框中输入您的用户名 --> 点击 Check Names 按钮 --> 点击 Ok --> 点击 Ok --> 点击 Ok -->点击确定图2

You can find the answer from the ASW guide.您可以从 ASW 指南中找到答案。 400 protects it by making it read only and only for the owner. 400 通过使其只读且仅供所有者使用来保护它。

chmod 400 mykey.pem

执行 chmod 400 yourkeyfile.pem 如果您的实例是 Amazon linux,则使用 ssh -i yourkeyfile.pem ec2-user@ip for ubuntu ssh -i yourkeyfile.pem ubuntu@ip for centos ssh -i yourkeyfile.pem centos@ip

BY default permission are not allowing the pem key.默认情况下,权限不允许 pem 密钥。 You just have to change the permission:您只需要更改权限:

chmod 400 xyz.pem chmod 400 xyz.pem

and if ubuntu instance then connect using:如果是 ubuntu 实例,则使用以下方式连接:

ssh -i xyz.pem ubuntu@ec2-youraws.amazonaws.com ssh -i xyz.pem ubuntu@ec2-yourraws.amazonaws.com

The issue for me was that my .pem file was in one of my NTFS partitions.我的问题是我的 .pem 文件位于我的 NTFS 分区之一中。 I moved it to my linux partition (ext4).我把它移到了我的 linux 分区(ext4)。

Gave required permissions by running:通过运行提供所需的权限:

chmod 400 my_file.pem

And it worked.它奏效了。

I have seen two reasons behind this issue我看到了这个问题背后的两个原因

1) access key does not have the right permission. 1)访问密钥没有正确的权限。 pem keys with default permission are not allowed to make a secure connection.不允许具有默认权限的 pem 密钥建立安全连接。 You just have to change the permission:您只需要更改权限:

chmod 400 xyz.pem chmod 400 xyz.pem

2) Also check whether you have logged-in with proper user credentials. 2) 还要检查您是否使用正确的用户凭据登录。 Otherwise, use sudo while connecting否则,连接时使用 sudo

sudo ssh -i {keyfile} ec2-user@{ip address of remote host} sudo ssh -i {keyfile} ec2-user@{远程主机的ip地址}

Well, looking at your post description I feel there were 2 mistakes done by you:-好吧,看看你的帖子描述,我觉得你犯了两个错误:-

  1. Set correct permissions for the private key .为私钥设置正确的权限 Below command should help you to set correct file permision.下面的命令应该可以帮助您设置正确的文件权限。

    chmod 0600 mykey.pem

  2. Wrong ec2 user you are trying to login .您尝试登录的 ec2 用户错误

    Looking at your debug log I think you have spawned an Amazon linux instance.查看您的调试日志,我认为您已经生成了一个 Amazon linux 实例。 The default user for that instance type is ec2-user .该实例类型的默认用户是ec2-user If the instance would have been ubuntu then your default user would have been ubuntu .如果实例是 ubuntu ,那么您的默认用户将是ubuntu

    ssh -i privatekey.pem default_ssh_user@server_ip

 Note: For an Amazon Linux AMI, the default user name is ec2-user. For a Centos AMI, the default user name is centos. For a Debian AMI, the default user name is admin or root. For a Fedora AMI, the default user name is ec2-user or fedora. For a RHEL AMI, the default user name is ec2-user or root. For a SUSE AMI, the default user name is ec2-user or root. For an Ubuntu AMI, the default user name is ubuntu. Otherwise, if ec2-user and root don't work, check with the AMI provider.

source: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html来源: https ://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Key file should not be publicly viewable so use permission 400密钥文件不应公开查看,因此请使用权限 400

chmod 400 keyfile.pem

If above command shows permission error use如果上面的命令显示权限错误使用

sudo chmod 400 keyfile.pem

Now ssh into the ec2 machine, if you still face the issue, use ec2-user现在 ssh 进入 ec2 机器,如果仍然遇到问题,请使用ec2-user

ssh -i keyfile.pem ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com ssh -i keyfile.pem ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com

You're not in root then run this command你不是 root 然后运行这个命令

sudo chmod 400 -R myfile.pem

Not is root then run this command不是 root 然后运行此命令

chmod 400 -R myfile.pem

If you are connecting from Windows, perform the following steps on your local computer.如果您从 Windows 连接,请在本地计算机上执行以下步骤。

  1. Navigate to your .pem file.导航到您的 .pem 文件。

  2. Right-click on the .pem file and select Properties.右键单击 .pem 文件并选择属性。

  3. Choose the Security tab.选择安全选项卡。

  4. Select Advanced.选择高级。

  5. Verify that you are the owner of the file.确认您是该文件的所有者。 If not, change the owner to your username.如果没有,请将所有者更改为您的用户名。

  6. Select Disable inheritance and Remove all inherited permissions from this object.选择禁用继承并从此对象删除所有继承的权限。

  7. Select Add, Select a principal, enter your username, and select OK.选择添加,选择主体,输入您的用户名,然后选择确定。

  8. From the Permission Entry window, grant Read permissions and select OK.在“权限条目”窗口中,授予读取权限并选择“确定”。

  9. Click Apply to ensure all settings are saved.单击应用以确保保存所有设置。

  10. Select OK to close the Advanced Security Settings window.选择确定关闭高级安全设置窗口。

  11. Select OK to close the Properties window.选择确定关闭属性窗口。

  12. You should be able to connect to your Linux instance from Windows via SSH.您应该能够通过 SSH 从 Windows 连接到您的 Linux 实例。

From a Windows command prompt, run the following commands.在 Windows 命令提示符下,运行以下命令。

  1. Run the following command to reset and remove explicit permissions: icacls.exe $path /reset运行以下命令以重置和删除显式权限:icacls.exe $path /reset
  2. Run the following command to grant Read permissions to the current user: icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"运行以下命令为当前用户授予读取权限:icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
  3. Run the following command to disable inheritance and remove inherited permissions : icacls.exe $path /inheritance:r运行以下命令以禁用继承并删除继承的权限:icacls.exe $path /inheritance:r

You should be able to connect to your Linux instance from Windows via SSH.您应该能够通过 SSH 从 Windows 连接到您的 Linux 实例。

It is just a permission issue with your aws pem key.这只是您的 aws pem 密钥的权限问题。

Just change the permission of pem key to 400 using below command.只需使用以下命令将 pem 密钥的权限更改为 400。

chmod 400 pemkeyname.pem

If you don't have permission to change the permission of a file you can use sudo like below command.如果您无权更改文件的权限,则可以使用 sudo 如下命令。

sudo chmod 400 pemkeyname.pem

Else if nothing works for you just follow this video to change the keys on your EC2 instance.否则,如果您没有任何效果,请按照此视频更改您的 EC2 实例上的密钥。 You can install now public / private key pair on your instance.您现在可以在您的实例上安装公钥/私钥对。

https://youtu.be/LvLlRCrS8B4 https://youtu.be/LvLlRCrS8B4

Checklist:清单:

  1. Are you using the right private key .pem file?您是否使用了正确的私钥 .pem 文件?

  2. Are its permissions set correctly?它的权限设置是否正确? (My Amazon-brand AMIs work with 644, but Red hat must be at least 600 or 400. Don't know about Ubuntu.) (我的亚马逊品牌 AMI 使用 644,但 Red hat 必须至少为 600 或 400。不了解 Ubuntu。)

  3. Are you using the right username in your ssh line?您在 ssh 行中使用了正确的用户名吗? Amazon-branded = "ec2-user", Red Hat = "root", Ubuntu = "ubuntu".亚马逊品牌 = “ec2-user”,红帽 = “root”,Ubuntu = “ubuntu”。 User can be specified as "ssh -i pem usename@hostname" OR "ssh -l username -i pem hostname"用户可以指定为“ssh -i pem usename@hostname”或“ssh -l username -i pem hostname”

Following are the simple steps for Linux user to connect with the server using .pem file:以下是 Linux 用户使用 .pem 文件连接服务器的简单步骤:

Step1: To to the location of pem file and copy it to home .ssh location.步骤1:到pem文件的位置并将其复制到home .ssh位置。

cp example.pem ~/.ssh/example.pem

Step2: Change the permission第二步:更改权限

chmod 400 ~/.ssh/example.pem

Step3: Run the following command第三步:运行以下命令

ssh -i ~/.ssh/example.pem ec2-user@host.com

As this command is too long so you sould create the alias of this using following commands:由于此命令太长,因此您可以使用以下命令创建它的别名:

 vim ~/.bashrc

Write the same command in the following manner at the last.最后按以下方式编写相同的命令。

alias sshConnect='ssh -i ~/.ssh/example.pem ec2-user@host.com'

Now restart your system and use sshConnect to connect with your server.现在重新启动您的系统并使用sshConnect连接您的服务器。

Just change the permission of pem file to 0600 allowing only for the allowed user and it will work like charm.只需将 pem 文件的权限更改为 0600 只允许允许的用户,它就会像魅力一样工作。

sudo chmod 0600 myfile.pem

And then try to ssh it will work perfectly.然后尝试 ssh 它将完美地工作。

ssh -i myfile.pem <<ssh_user>>@<<server>>

It is just a permission issue with your aws pem key.这只是您的 aws pem 密钥的权限问题。

Just change the permission of pem key to 400 using below command.只需使用以下命令将 pem 密钥的权限更改为 400。

chmod 400 pemkeyname.pem

If you don't have permission to change the permission of a file you can use sudo like below command.如果您无权更改文件的权限,则可以使用 sudo 如下命令。

sudo chmod 400 pemkeyname.pem

I hope this should work fine.我希望这应该可以正常工作。

Please ignore this answer if it is irrelevant for you, but from my experience I've seen people having an issue with Permission denied (publickey) because they simply pasted their public key (on a target machine) without the first letter !如果此答案与您无关,请忽略此答案,但根据我的经验,我见过有人遇到Permission denied (publickey)问题,因为他们只是在没有第一个字母的情况下粘贴了他们的公钥(在目标机器上)!

This happens when using vim to edit (paste) the key.使用vim编辑(粘贴)密钥时会发生这种情况。 Since vim by default opens in command mode (not in an insert mode ), pasting the key without switching to an insert mode (ie i ) will result in skipping the first s letter, eg instead of由于vim默认在命令模式下打开(不是在插入模式下),粘贴键而不切换到插入模式(即i )将导致跳过第一个s字母,例如而不是

ssh-rsa <key>

you end up pasting你最终粘贴

sh-rsa <key>

So before trying other solutions, see if you've pasted your key correctly !因此,在尝试其他解决方案之前,请查看您是否正确粘贴了密钥 ie IE

cat ~/.ssh/id_rsa.pub

Only if you're certain, perform the next steps;只有当您确定时,才执行下一步; trying to ssh in a verbose mode (ie flag -v ) might point you to the actual issue:尝试以详细模式(即标志-v )进行 ssh 可能会指向实际问题:

ssh -v -i <private_key> <name>@<ip> -p <port>

As a side note, as it has been already mentioned here by others, in majority of cases starting an empty ssh agent (program that keeps your keys in memory) and adding your key should resolve the issue:作为旁注,正如其他人在这里已经提到的那样,在大多数情况下,启动一个空的ssh 代理(将您的密钥保存在内存中的程序)并添加您的密钥应该可以解决问题:

ssh-agent bash
ssh-add <private_key>

.400 protects it by making it read only and only for the owner. .400 通过使其只读且仅供所有者使用来保护它。
You can find the answer from the ASW guide.您可以从 ASW 指南中找到答案。

chmod 400 yourPrivateKey.pem

在此处输入图像描述

What did it for me is editing the default security group to allow for inbound TCP traffic at port 22:对我来说是编辑默认安全组以允许端口 22 的入站 TCP 流量:

在此处输入图像描述

You would need to put some conservative permissions on the key file (myfile.pem).您需要对密钥文件 (myfile.pem) 设置一些保守的权限。 Try changing it to r-------- OR 400尝试将其更改为r-------- OR 400

for windows 10.对于 Windows 10。

. . Right click file .右键单击文件。 properties->security-> disable inheritance .now add -> your user(window) with only "read" .属性->安全->禁用继承。现在添加->你的用户(窗口)只有“读取”。 Click ok点击确定

now its working for me现在它为我工作

What fixed this for me was to move the .pem file within the apps directory.为我解决这个问题的方法是将 .pem 文件移动到应用程序目录中。 Soo say fooapp is the name of my app. Soo 说 fooapp 是我的应用程序的名称。 I placed it directly in there.我直接把它放在那里。

Your key must not be publicly viewable for SSH to work. 您的密钥不得公开可见,SSH才能正常工作。 Use this command if needed: 如果需要,请使用以下命令:

chmod 400 Interview-apps.pem

Connect to your instance using its Public DNS:

ec2-**-***-***-***.us-west-2.compute.amazonaws.com

Example: 例:

ssh -i "Interview-apps.pem" ec2-user@ec2-**-***-***-***.us-west-2.compute.amazonaws.com

You should also check if your .pem file is not corrupted.您还应该检查您的 .pem 文件是否未损坏。 I spent about an hour scratching my head and decided to check using this line我花了大约一个小时挠头,决定用这条线检查一下

openssl rsa -check -in test.pem -noout

If it returns "RSA key ok" then you are good.如果它返回“RSA key ok”,那么你很好。 If not, make sure you have the right file and or copied it correctly for whatever reason.如果没有,请确保您拥有正确的文件,并且无论出于何种原因正确复制它。

If you are on windows 10 using the ubuntu subsystem, and if you sudo chmod to change the key to 400, then it may still error with "Load key pem: Permission denied"如果您在使用 ubuntu 子系统的 Windows 10 上,并且如果您 sudo chmod 将密钥更改为 400,那么它可能仍然会出现“加载密钥 pem:权限被拒绝”错误

ls -al and you will see root now owns the file! ls -al 你会看到 root 现在拥有该文件! chown it to your logged in user and then it will work. chown 到您的登录用户,然后它会工作。

there's notes when you creating new EC2 instance that pormote you to change the file permtion当您创建新的 EC2 实例时,有一些注释会提示您更改文件权限

Easily solved by executing -通过执行轻松解决 -

chmod 400 mykey.pem chmod 400 mykey.pem

Windows 10 Windows 10

icacls.exe .\Desktop\xxxx.pem /reset
icacls.exe .\Desktop\xxxx.pem /grant:r "$($env:USERNAME):(r)"
icacls.exe .\Desktop\xxxx.pem /inheritance:r

ssh -i .\Desktop\xxxx.pem ec2-user@54.2xx.xxx.x

macos & linux macOS & linux

chmod 400 ~/Desktop/xxxx.pem

ssh -i ~/Desktop/xxxx.pem ec2-user@54.2xx.xxx.x

One thing I like doing in this matter, is to use an alias and add it to the .bashrc file so that I don't have to write connect commands or get back to the key each time I need to SSh the EC2 instance.在这件事上我喜欢做的一件事是使用别名并将其添加到.bashrc文件中,这样我就不必在每次需要 SSh EC2 实例时都编写连接命令或返回密钥。

Here is how I do it:这是我的操作方法:

vim .bashrc

Add the following content to the end of the file在文件末尾添加以下内容

# Custom fields

###[ MY APP 1 NAME ]###

# APP 1 Dev env EKS cluster bastion host
alias app1_dev="ssh -i ~/.ssh/app1-dev-bastion.pem USER@IPv4_ADDRESS"

###[ MY APP 2 NAME ]###
# APP 2 Stg env CodeDeploy instance
alias app_stg_cd="ssh -i ~/.ssh/app2-stg-cd.pem USER@IPv4_ADDRESS"

And then apply changes:然后应用更改:

source .bashrc

Sorry if anyone answered this before and I didn't notice, and just wanted to share my own work taste, not like the other fellows didn't answer very well.对不起,如果之前有人回答过我没有注意到,只是想分享我自己的工作品味,而不是像其他人一样没有回答得很好。

This error is only because of permission. 此错误仅是由于许可。

Just give the permission 400 只需授予权限400

#chmod 400 pemfilepath #chmod 400 pemfilepath

Sometimes there may be a error in the folder. 有时文件夹中可能有错误。 I do not know why... 我不知道为什么...

You can change the folder and try again. 您可以更改文件夹,然后重试。 For example, you can experiment in the usual (Desktop, Downloads and etc) folders. 例如,您可以尝试使用常规(桌面,下载等)文件夹。

I tried this method and worked 我尝试了这种方法并工作

if don't have permissions don't forget sudo it.如果没有权限不要忘记sudo它。 sudo ssh -i myfile.pem <<ssh_user>>@<>须藤 ssh -i myfile.pem <<ssh_user>>@<>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM