即使使用新的 EC2 实例,AWS SSH 登录仍会继续失败 [权限被拒绝 (PublicKey)]

Posted

技术标签:

【中文标题】即使使用新的 EC2 实例,AWS SSH 登录仍会继续失败 [权限被拒绝 (PublicKey)]【英文标题】:AWS SSH Login continues to fail even with new EC2 instance [Permission denied (PublicKey)] 【发布时间】:2021-03-08 18:00:24 【问题描述】:

我刚开始使用 AWS 在 EC2 实例上创建一个 wordpress 站点(使用https://www.youtube.com/watch?v=4WR7EFFLF04 上的教程)。一切正常,直到我尝试使用另一个教程添加 SSL 证书(我现在似乎找不到)。

在执行这些步骤时,由于重定向过多,网站出现了问题(假定 DNS 错误)无法加载。当我回到教程试图修复我所做的事情时,我到达了它让我通过 SSH 登录的地方,我以前没有任何问题,我开始收到 “权限被拒绝(公钥)” 错误。

从那时起,我就无法登录到该实例或任何其他实例,而不会出现相同的错误。我什至尝试使用全新的密钥创建一个新实例,但我仍然遇到同样的错误。知道会发生什么吗?

更新 我做了 Flos 推荐的调试命令,这是输出。 (这足以使其“成为主题”并重新提出问题吗?)

输出:

OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to ec2-13-59-207-225.us-east-2.compute.amazonaws.com port 22.
debug1: Connection established.
debug1: identity file /Users/bradley/Desktop/John and Rhonda Wordpress Files/keypair-2.pem type -1
debug1: identity file /Users/bradley/Desktop/John and Rhonda Wordpress Files/keypair-2.pem-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.9p1 Debian-10+deb10u2
debug1: match: OpenSSH_7.9p1 Debian-10+deb10u2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to ec2-13-59-207-225.us-east-2.compute.amazonaws.com:22 as 'admin'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:OiIQ5QiVxQvN9lAkjLm90ynddjDDs9L75PqDAL/cTv4
debug1: Host 'ec2-13-59-207-225.us-east-2.compute.amazonaws.com' is known and matches the ECDSA host key.
debug1: Found key in /Users/bradley/.ssh/known_hosts:6
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /Users/bradley/Desktop/John and Rhonda Wordpress Files/keypair-2.pem  explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/bradley/Desktop/John and Rhonda Wordpress Files/keypair-2.pem
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
admin@ec2-13-59-207-225.us-east-2.compute.amazonaws.com: Permission denied (publickey).

【问题讨论】:

【参考方案1】:

您确定您使用的身份/密钥、用户名和 ip/dns 正确吗?

一些发行版使用 ec2-userubuntuadmin 作为用户名。

它是哪个操作系统?

也看看here

你是怎么连接的?

我大多只是在我的 ssh 配置文件中添加一个条目 ~/.ssh/config

Host router
   HostName 192.168.1.1
   Port 22
   User ec2-user
   IdentityFile ~/.ssh/router.pem

然后我就输入

ssh router

【讨论】:

当我转到实例时,单击连接并选择 SSH 客户端选项,它说用户名是 admin。我还尝试了您提供的链接中列出的所有其他名称。我使用的服务器模板是Linux/Unix,Ubuntu 10 | 64 位 (x86) 亚马逊系统映像 (AMI) |更新日期:20 年 11 月 17 日 必须是主机名/IP、用户名或密钥文件。你如何连接?我主要为我的 ~/.ssh/config 主机路由器 HostName 192.168.1.1 Port 22 User ec2-user IdentityFile ~/.ssh/router.pem 创建一个条目,然后我只需输入 ssh router 我对 SSH 不是很熟悉,所以请原谅我缺乏对术语的了解,哈哈。我之前连接并继续尝试连接的方式是从 Mac 终端应用程序使用命令:ssh -i "keypair-2.pem" admin@ec2-13-59-207-225.us-east-2。 compute.amazonaws.com - 这是来自实例连接屏幕的直接副本 我想知道这是否是亚马逊生成的密钥对的问题,所以我还在我的 Mac 上生成了一个新的密钥对,将公钥上传到 AWS 并使用它创建了一个新的 EC2 实例.我遇到了同样的错误。不确定该信息是否有用 如果您以详细模式“-v”运行 ssh 并将结果添加到您的问题中,这可能是最好的。 ssh -v -i "keypair-2.pem" admin@ec2-13-59-207-225.us-east-2.compute.amazonaws.com 如果你没有让它运行,并且详细输出看起来没问题并且一切都正确,用户名/IP/身份尝试不同的 ubuntu 图像。

以上是关于即使使用新的 EC2 实例,AWS SSH 登录仍会继续失败 [权限被拒绝 (PublicKey)]的主要内容,如果未能解决你的问题,请参考以下文章

在将ssh尝试到实例时关闭AWS EC2连接

AWS EC2 ssh连接在10秒后关闭

在 Terraform 管理的 AWS EC2 SSH 堡垒实例上处理多个用户的 SSH 密钥

同一子网内的 AWS EC2 SSH <private_ip_address>

使用 SSH 连接到AWS EC2 Linux 实例(Windows可用SSH)

SSH到位于VPC中的私有子网中的AWS EC2实例