权限被拒绝(公钥)亚马逊Linux AWS

4
我在 AWS 上运行了两个实例。这两个实例使用相同的密钥对。长期以来我一直使用这两个实例,但是今天当我尝试通过终端登录其中一个实例时,出现了错误。如果我运行这个命令。
ssh -i mypem.pem ec2-user@52.xx.xxx.xxx
Permission denied (publickey).

但是,如果我尝试使用相同的pem文件登录到其他实例,则可以成功登录。

我尝试了这里提出的所有解决方案:

SSH访问Amazon EC2实例时出现“权限被拒绝(publickey)”

尝试SSH连接Amazon Ec2实例-权限错误

但是对我来说都没有用。

如果我这样做:

ssh -i mypem.pem ec2-user@52.xx.xxx.xxx -vvv

结果是这样的。
    OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "52.xx.xxx.xxx" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 52.xx.xxx.xxx [52.xx.xxx.xxx] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file mypem.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file mypem.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 52.xx.xxx.xxx:22 as 'ec2-user'
debug3: hostkeys_foreach: reading file "/Users/myusername/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/myusername/.ssh/known_hosts:6
debug3: load_hostkeys: loaded 1 keys from 52.xx.xxx.xxx
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
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
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: mykeyhere
debug3: hostkeys_foreach: reading file "/Users/myusername/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/myusername/.ssh/known_hosts:6
debug3: load_hostkeys: loaded 1 keys from 52.70.181.239
debug1: Host '52.xx.xxx.xxx' is known and matches the ECDSA host key.
debug1: Found key in /Users/irfansheikh/.ssh/known_hosts:6
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey after 134217728 blocks
debug2: key: mypem.pem (0x7f9053c03750), agent
debug2: key: mypem.pem (0x0), explicit
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: bringthings.pem
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug1: Trying private key: mypem.pem
debug3: sign_and_send_pubkey: RSA 
SHA256:keyhere**********
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

我尝试过的内容
chmod 400 mypem.pem

我也尝试过这个。
sudo ssh -i mypem.pem ec2-xxx-xxx-xxx-xxx.us-west 2.compute.amazonaws.com

收到这个

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

然后我做了这个。
sudo ssh -i mypem.pem ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com -l ec2-user

我得到了。
Permission denied (publickey)

如果我尝试使用相同的密钥登录其他实例,我可以成功登录。请帮忙找出问题所在。
我的实例上的安全组如下:

enter image description here


它们都是AWS Linux实例吗?如果不是,用户名将会不同(例如,对于Ubuntu实例,用户名是"ubuntu"而不是"ec2-user")。 - kdgregory
你有没有曾经能够登录第二个实例?如果是的话,有可能是你做了一些改变了 .ssh/authorized_keys 的事情吗? - kdgregory
另一个实例的用户名是ubuntu,但这个实例的用户名是ec2-user。是的,我可以成功登录到另一个实例。 - user1hjgjhgjhggjhg
一切都正常工作,直到晚上。当我醒来时,它停止工作了。所以,我没有做任何事情。 - user1hjgjhgjhggjhg
5个回答

5
问题是由于使用了不同的公钥,为解决这个问题:-
只需使用您的私钥创建一个公钥,例如mypem.pem文件,如下所示:
ssh-keygen -y -f mypem.pem
它将创建并显示私钥的公钥。 您只需将此密钥复制到aws实例中,如下所示:
如果您运行的是ubuntu实例,请打开authorized_keys文件并将生成的公钥粘贴到该文件中(删除现有内容):
vi ~/.ssh/authorized_keys
然后尝试使用以下命令连接:
ssh -i mypem.pem ec2-user@52.xx.xxx.xxx
或者
ssh -i mypem.pem ubuntu@52.xx.xxx.xxx

这应该是被点赞的答案。 - Nelson Katale
如何连接实例以添加发布密钥?因为目前我无法连接到实例,权限被拒绝。 - Four
如何连接实例以添加发布密钥?因为目前我无法连接到实例,权限被拒绝。 - undefined

1

1
使用实例ID而不是ubuntu作为用户名。 - Daniel

0

你可以尝试已经建议的-vvv选项,看看是否能获取更多关于SSH连接失败原因的信息。

如果仍然不清楚,唯一的选择就是将卷从当前实例中分离,并附加到另一个EC2实例上,挂载该卷并检查以下日志文件是否有任何错误信息:

/var/log/messages
/var/log/secure

您可以使用以下链接参考如何将卷从一个EC2实例分离并附加到另一个实例上:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-detaching-volume.html 
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-attaching-volume.html

在分离和连接时,请记下卷的设备名称,例如 /dev/xvda/dev/sda1。如果第一次错过了,那么你必须重复整个分离和连接过程,这是非常烦人的。

此处可能会出现多种错误原因。例如,如果上次服务器关闭时 /home/ec2-user/.ssh 文件夹或其中任何文件的权限设置不正确,则该命令将无法读取文件,因此失败。通常,在日志文件中记录的错误有助于提供无法访问原因。

我想指出的一件事是,你正在尝试使用两个密钥对名称:amazonec2.pemmypem.pem。在使用 mypem.pem 时,你在 SSH 命令中提到了用户名:ec2-user 和 IP 地址。然而,在使用 amazonec2.pem 时,它在命令中缺失了!

请注意,如果用户名不存在,命令将无法执行,出现相同的“Permission denied (publickey)”错误。我曾尝试在未指定用户名的情况下SSH登录到我的EC2实例,但是失败了。
希望这能帮到你。

抱歉,“amazonec2.pem和mypem.pem”只是一个打字错误。我在所有地方都使用相同的mypem.pem。 - user1hjgjhgjhggjhg
然后,您需要分析日志文件,ssh的-vvv输出是什么意思,您可以在这里更新输出,这样每个人都可以看到并提供帮助,如果他们有提示! - Crazy Psychild

0
在我的情况下,我多次输入了错误的密码短语。之后我一直不断地收到“权限被拒绝”的提示。 我重新启动了我的AWS实例并尝试了相同的凭据,结果成功了。

-1

这与问题中的问题无关。 - gshpychka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接