【问题标题】:EC2 keypair works in one instance but fails on other - Permission denied (publickey)EC2 密钥对在一个实例中有效,但在另一个实例中失败 - 权限被拒绝(公钥)
【发布时间】:2014-04-25 14:10:38
【问题描述】:

我已经阅读了很多关于这个主题的帖子,但没有一个可以帮助我解决我的问题。

我有一台机器 amazon ec2,我使用这个 SSH 命令连接它:

ssh -i /Library/AWS/glrpopulis.pem ec2-user@54.225.154.23

到目前为止,我从未遇到过此命令的问题。它刚刚停止工作,显示以下消息:Permission denied (publickey). 不知从何而来!

我真的不明白为什么我几乎每天都使用的同一命令突然失效了。可能我改变了一些我不应该改变的东西,但我很难弄清楚是什么。

第一次发生这种情况时,我正在为 Web 应用程序(atlassian 竹子)创建服务,但我不确定这是否与错误有关。

我已经重新启动了几次机器,并反复尝试,但没有成功。

带有 -v 选项的完整输出如下所示:

mac-pipo:~ felipereis$ ssh -v -i /Library/AWS/glrpopulis.pem ec2-user@54.225.154.23
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to 54.225.154.23 [54.225.154.23] port 22.
debug1: Connection established.
debug1: identity file /Users/felipereis/.ssh/id_rsa type 1
debug1: identity file /Users/felipereis/.ssh/id_rsa-cert type -1
debug1: identity file /Users/felipereis/.ssh/id_dsa type -1
debug1: identity file /Users/felipereis/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH*
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: 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: Server host key: RSA 19:ef:f1:2b:56:dd:86:ec:42:65:ff:1d:6b:64:0f:f3
debug1: Host '54.225.154.23' is known and matches the RSA host key.
debug1: Found key in /Users/felipereis/.ssh/known_hosts:12
debug1: ssh_rsa_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: Offering RSA public key: /Users/felipereis/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: /Library/AWS/glrpopulis.pem
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/felipereis/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

更新: * 我刚刚测试过,我可以使用相同的密钥 (glrpopulis.pem) 连接到不同的 ec2 实例,所以可能是第一台机器上发生了一些事情

【问题讨论】:

    标签: linux ssh amazon-ec2


    【解决方案1】:

    听起来 ~/.ssh/authorized_keys 下的密钥搞砸了,或者文件被删除了。

    尝试以下方法:

    1. 停止您的 EC2 实例
    2. 分离你的根卷(/dev/sda1)——假设这是Volume A
    3. 启动一个具有相同类型和相同凭证的新 EC2 实例。
    4. Volume A 附加到该新实例作为/dev/sdf
    5. ssh 连接到他的新实例。
    6. mkdir -p /mnt/xvdf
    7. mount /dev/xvdf /mnt/xvdf
    8. cp ~/.ssh to /mnt/xvdf/home/ec2-user/.
    9. chmod 700 /mnt/xvdf/home/ec2-user
    10. chmod 600 /mnt/xvdf/home/ec2-user/authorized_keys
    11. 关闭新实例
    12. 在新实例上分离 Volume A
    13. 在原始实例的 /dev/sda1 上重新附加 Volume A
    14. 启动原始实例。

    您现在应该可以登录了。

    【讨论】:

    • 感谢您的回答 Rico。第 8 步应该是 cp ~/.ssh /mnt/xvdf/home/ec2-user/ 第 10 步应该是 chmod 600 /mnt/xvdf/home/ec2-user/.ssh/authorized_keys。然而,这对我不起作用。我的第一个实例不知何故损坏了,所以我无法恢复它。我不得不使用新创建的转储它。谢谢!
    【解决方案2】:

    根据您的 AMI,可能会将公钥添加到与 ec2-user 不同的用户的 authorized_keys 文件中。

    要找出答案,您可以在 EC2 控制台中查看实例的启动日志,它应该输出 cloud-init 使用的用户名作为“默认用户”。我的有这样一行:

    ci-info: +++++++++++++++++++++Authorized keys from /home/ec2-user/.ssh/authorized_keys for user ec2-user++++++++++++++++++++++
    

    您也可以尝试以 root 身份登录,因为这有时会出现类似“请以用户“ec2-user”而不是用户“root”身份登录的错误。

    这发生在我身上,这是因为我更新了我的 cloud-init 版本,这就是将公钥添加到 authorized_keys 的原因。默认配置文件(/etc/cloud/cloud.cfg)被替换,导致默认用户从“ec2-user”更改为“cloud-user”。

    我通过将新 /etc/cloud/cloud.cfg 的 system_info 部分更改为以下内容来解决此问题:

    ...
    system_info:
        ...
        default_user:
            name: ec2-user
            sudo: ALL=(ALL) NOPASSWD:ALL
        ...
    

    然后您可以从该实例创建一个新的 AMI,它应该会再次正确设置 ec2-user。

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多