权限被拒绝(公钥) Amazon Linux aws

Posted

技术标签:

【中文标题】权限被拒绝(公钥) Amazon Linux aws【英文标题】:Permission denied (publickey) Amazon Linux aws 【发布时间】:2018-10-01 20:58:23 【问题描述】:

我有两个实例在我的 aws 上运行。我对他们两个都有相同的密钥对。我长时间使用这两个实例。但是今天如果我运行此命令,我在尝试通过终端登录时在一个实例上遇到错误。

ssh -i mypem.pem ec2-user@52.xx.xxx.xxx
Permission denied (publickey).

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

我已经尝试了这里介绍的所有解决方案

Permission denied (publickey) when SSH Access to Amazon EC2 instance

Trying to SSH into an Amazon Ec2 instance - permission error

但对我没有任何作用

如果我这样做

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)

如果我尝试使用相同的密钥登录其他实例。我可以成功登录。请帮助这里实际出了什么问题

我的实例上的安全组是这个

【问题讨论】:

它们都是 AWS Linux 实例吗?如果不是,用户名将不同(例如,对于 Ubuntu 实例,它是“ubuntu”而不是“ec2-user”)。 曾经能够登录到第二个实例吗?如果是,您是否可能做了一些改变.ssh/authorized_keys的事情? 另一个实例的用户名是 ubuntu 但这个实例有 ec2-user.. 是的,我可以成功登录到其他实例 直到晚上一切正常。当我醒来时它停止工作。所以不,我什么都没做 【参考方案1】:

与 ec2 的连接故障排除: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html

您也可以尝试三重详细选项 -vvv

我以前经常遇到同样的错误,通常我使用的是 ec2-user 而不是 ubuntu 或错误的密钥

【讨论】:

使用实例 ID 而不是 ubuntu 作为用户名。【参考方案2】:

问题是因为使用了不同的公钥并解决了这个问题:-

只需使用您的私钥创建一个公钥,即 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

【讨论】:

【参考方案3】:

您可以按照建议尝试-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 时,您提到了用户名:ec2-user 以及 SSH 命令中的 IP 地址。但是,当您使用amazonec2.pem 时,命令中缺少它!

请注意,如果用户名不存在,该命令实际上会失败,并给出相同的Permission denied (publickey). 错误。我尝试在不提及用户名的情况下通过 SSH 连接到我的 EC2 实例,但失败了。

希望这会有所帮助。

【讨论】:

抱歉,“amazonec2.pem 和 mypem.pem”这些只是拼写错误。我在所有地方都使用相同的 mypem.pem 那你要分析一下日志文件,ssh的-vvv输出是说什么的,你可以在这里更新输出,让大家看到,有提示的帮忙!跨度> 【参考方案4】:

就我而言,我多次输入了错误的密码。之后我不断获得 Permission denied 。 我重新启动了我的 AWS 实例并尝试了相同的凭据。

【讨论】:

以上是关于权限被拒绝(公钥) Amazon Linux aws的主要内容,如果未能解决你的问题,请参考以下文章

Amazon ec2 显示“权限被拒绝(公钥)”

Amazon EC2 实例“权限被拒绝(公钥)”

ssh - Amazon EC2 上的权限被拒绝(公钥)

Amazon AWS Filezilla 传输权限被拒绝

连接到 Amazon AWS:权限被拒绝

AWS ssh 访问“权限被拒绝(公钥)”问题[关闭]