53

我无法通过 ssh 连接到我的 EC2 服务器并收到错误消息:

ssh -i /Users/Skeniver/Keepass/skeniver.pem ubuntu@xx.xxx.xx.xxx

Identity file /Users/Skeniver/Keepass/skeniver.pem not accessible: No such file or directory

该文件肯定在目录中,我已将目录更改为 755,将文件更改为 600:

drwxr-xr-x  14 Skeniver  staff    476 28 Nov  2012 Keepass
-rw-------  1 Skeniver  staff    1696  5 Oct  2012 skeniverkey.pem

我很确定这是一个权限问题,但无法弄清楚为什么......

这是使用 -v 的 ssh 命令的输出,以防万一它有任何用处:

Warning: Identity file /Users/Skeniver/Keepass/skeniver.pem not accessible: No such file or directory.
OpenSSH_5.2p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /etc/ssh_config
debug1: Connecting to xx.xxx.xx.xxx [xx.xxx.xx.xxx] port 22.
debug1: Connection established.
debug1: identity file /Users/Skeniver/.ssh/identity type -1
debug1: identity file /Users/Skeniver/.ssh/id_rsa type 1
debug1: identity file /Users/Skeniver/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.2
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'xx.xxx.xx.xxx' is known and matches the RSA host key.
debug1: Found key in /Users/Skeniver/.ssh/known_hosts:8
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/Skeniver/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/Skeniver/.ssh/identity
debug1: Trying private key: /Users/Skeniver/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).
4

15 回答 15

31

You need the identity file to login to the box. Use the command:

ssh -i (identity_file) username@hostname"

This worked for me. Write just the filename (without any slashes), unlike Amazon EC2 tutorial which asks you to enter:

ssh -i /path/key_pair.pem ec2-user@public_dns_name

It worked for me after putting the identity file in the .ssh (hidden) folder on home. To view hidden folders on home, use ctrl+h

于 2013-09-26T23:47:05.583 回答
21

我有类似的问题,发现命令中有隐藏的字符,粘贴到纯文本编辑器并复制回来对我有帮助。

于 2017-06-18T05:20:14.970 回答
5

我遇到了同样的问题。将身份文件存储在我的~/.ssh目录中并在工作cd ~/.ssh之前ssh -i (identity_file) username@hostname运行。我只是在尝试从~/.ssh目录外部运行 ssh 命令时遇到了这个问题。希望这会有所帮助

于 2017-01-20T21:44:44.497 回答
5

对于你的情况

只需执行以下命令即可更改权限

sudo chmod 400 /Users/Skeniver/Keepass/skeniver.pem

然后执行将您连接到 EC2 的命令

ssh -i /Users/Skeniver/Keepass/skeniver.pem ubuntu@xx.xxx.xx.xxx

于 2018-05-16T03:54:23.527 回答
3

我有同样的问题,我发现文件路径的问题。右键单击您的 key_pair.pem (相同文件)转到属性并复制,现在您的评论就像

ssh -i /path/skeniver.pem user@your_public_ip

例如在我的情况下命令像

ssh -i /home/anil/.ssh/webKey.pem ec2-user@ec2-45-349-50-38.us-west-2.compute.amazonaws.com
于 2018-06-13T18:53:57.360 回答
1

这很奇怪,但在我的情况下,我是从一条松弛消息中复制命令,并且反复导致此错误,即使我满足了上述所有要求。键入整个命令而不从剪贴板复制任何内容对我有用。在这里发布这个,我希望有一天这对某人有所帮助。

于 2020-12-14T19:47:09.663 回答
1

看起来您输入了错误的文件名。您skeniver.pem在命令中指定了文件名,但看起来您的文件名实际上是skeniverkey.pem.

于 2019-11-26T03:21:40.050 回答
1

chmod 600 ~/.ssh/public_key_file为我解决了

于 2019-04-10T09:11:59.613 回答
1

从 Windows 环境连接时,对我来说,诀窍是将路径放在引号"中,如下所示:

ssh -i "C:\Users\XXXX\Desktop\XXXX\testing_amazon_linux.pem" ubuntu@XXXX.amazonaws.com

我替换了可识别的东西,XXXX但让其余的东西就位以提高示例的清晰度。

于 2021-11-16T19:20:19.517 回答
1

发生此错误的原因有多种。1 您尝试从任何不匹配的路径访问 2 您没有访问权限 3 如果您无权访问该特定文件,您可以尝试更改权限模式

像上面的解决方案

sudo chmod 400 /Users/Skeniver/Keepass/skeniver.pem

即使没有,您也可以与管理您的云服务器的网络或您的云团队进行协调

于 2020-06-25T02:51:58.450 回答
0

从 .ssh 目录 ie/Users//.ssh 运行命令,然后执行命令 ssh -v -i @

在多次尝试失败后,这对我有用。

注意:请确保 .pem 文件具有必要的权限 chmod 400 <.pem file> 在执行命令之前

于 2019-07-30T14:13:36.167 回答
0

我在解决相同或类似问题时来到这里。在尝试了一些事情之后,这最终对我有用。我在 MacBook Pro 上并尝试使用 VSCode Remote SSH 连接到 Ec2 Amazon Linux 2。

我必须更新这个文件

/Users/MrRobot/.ssh/config

文件中有一个配置,如下所示。

身份文件 xx.pem

无论您的 id_rsa 或 pem 文件在哪里,我都必须将其更新为完整路径。

身份文件 /Users/MrRobot/.ssh/xx.pem

于 2020-03-09T23:57:30.960 回答
0

我的解决方案是将其移至所有英文字符的路径

(我是中国人,使用中文窗口,当我从包含中文字符的路径运行命令时,它会返回“警告:无法访问身份文件 debao-visa.pem:没有这样的文件或目录。”)

于 2020-05-16T06:20:03.913 回答
-1

尝试使用

ssh -i Users/Skeniver/Keepass/skeniver.pem ubuntu@xx.xxx.xx.xxx

代替

ssh -i **/**Users/Skeniver/Keepass/skeniver.pem ubuntu@xx.xxx.xx.xxx
于 2016-02-04T02:55:18.350 回答
-2

您需要身份文件才能登录该框。使用命令

ssh -i (identity_file) username@hostname
于 2013-06-11T06:47:57.847 回答