AWS SSH连接错误:Permission denied (publickey)

19 浏览
0 Comments

AWS SSH连接错误:Permission denied (publickey)

我正在尝试使用SSH连接到我的EC2实例,但我变得很疯狂。我阅读了这篇文章并尝试了所有的用户组合:

AWS ssh访问“Permission denied (publickey)”问题

对我来说仍然不起作用。有什么想法我错过了什么吗?

  roberto@ubuntu:~/keys$ ssh -v -i ec2-key-pair.pem ec2-user@ec2-54-72-242-0.eu-west-1.compute.amazonaws.com
OpenSSH_6.6, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to ec2-54-72-242-0.eu-west-1.compute.amazonaws.com [54.72.242.0] port 22.
debug1: Connection established.
debug1: identity file ec2-key-pair.pem type -1
debug1: identity file ec2-key-pair.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6p1 Ubuntu-2ubuntu1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA e4:06:ee:a5:a5:d2:97:5f:0f:b7:06:5e:f2:b3:da:26
debug1: Host 'ec2-54-72-242-0.eu-west-1.compute.amazonaws.com' is known and matches the ECDSA host key.
debug1: Found key in /home/roberto/.ssh/known_hosts:3
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: ec2-key-pair.pem
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).
UPDATE: 

根据@aldanux的建议:

    roberto@ubuntu:~/keys$ ssh-keygen -R 54.72.242.0
# Host 54.72.242.0 found: line 4 type ECDSA
/home/roberto/.ssh/known_hosts updated.
Original contents retained as /home/roberto/.ssh/known_hosts.old
roberto@ubuntu:~/keys$ ssh -i ec2-key-pair.pem ec2-user@ec2-54-72-242-0.eu-west-1.compute.amazonaws.com
Warning: Permanently added the ECDSA host key for IP address '54.72.242.0' to the list of known hosts.
Permission denied (publickey).

admin 更改状态以发布 2023年5月24日
0
0 Comments

虽然与AWS无关,但此错误信息并没有提供有用的信息:

debug1: key_parse_private2: missing begin marker

只有在一些偏僻的场景中才会发生,例如在远程计算机上SSH用户的主目录的所有权(或权限)不正确时。

最好的排查方式是检查远程计算机上的授权日志(如果有权限),因为它通常可以确定问题的所在。在Debian和Ubuntu系统上,最方便的方法是使用tail(视情况使用sudo):

tail -f -n 80 /var/log/auth.log

在我的特定情况下,我找到了:

Authentication refused: bad ownership or modes for directory /var/www

完全准确而简洁:所有者:组的设置为daemon:daemon,但应该是www-data:www-data(这是在Ubuntu机器上,可能在过去安装了其他的Web服务器)。

0
0 Comments

你可能正在使用错误的用户登录。如果是 Ubuntu 实例,命令应该是:

ssh -v -i ec2-key-pair.pem ubuntu@ec2-54-72-242-0.eu-west-1.compute.amazonaws.com

0