Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux EC2密钥对在一个实例中工作,但在另一个实例中失败-权限被拒绝(公钥)_Linux_Ssh_Amazon Ec2 - Fatal编程技术网

Linux EC2密钥对在一个实例中工作,但在另一个实例中失败-权限被拒绝(公钥)

Linux EC2密钥对在一个实例中工作,但在另一个实例中失败-权限被拒绝(公钥),linux,ssh,amazon-ec2,Linux,Ssh,Amazon Ec2,我读过很多关于这个问题的帖子,但没有一篇能帮我解决这个问题 我有一台机器amazon ec2,我使用以下SSH命令连接它: ssh -i /Library/AWS/glrpopulis.pem ec2-user@54.225.154.23 到目前为止,我从未遇到过这个命令的问题。它刚刚停止工作,将显示以下消息:权限被拒绝(公钥)。不知从何而来 我真的不明白为什么我几乎每天使用的同一个命令突然失效了。也许我已经改变了一些我不应该改变的事情,但我真的很难弄清楚是什么 第一次发生这种情况时,我正在为

我读过很多关于这个问题的帖子,但没有一篇能帮我解决这个问题

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

ssh -i /Library/AWS/glrpopulis.pem ec2-user@54.225.154.23
到目前为止,我从未遇到过这个命令的问题。它刚刚停止工作,将显示以下消息:权限被拒绝(公钥)。不知从何而来

我真的不明白为什么我几乎每天使用的同一个命令突然失效了。也许我已经改变了一些我不应该改变的事情,但我真的很难弄清楚是什么

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

我已经重新启动了机器几次,并尝试了一遍又一遍,没有成功

带有-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).
mac pipo:~felipereis$ssh-v-i/Library/AWS/glrpopulis.pem ec2-user@54.225.154.23
OpenSSH_6.2p2,OSSLShim 0.9.8r 2011年12月8日
debug1:读取配置数据/etc/ssh\u config
debug1:/etc/ssh\u配置第20行:应用*
debug1:连接到54.225.154.23[54.225.154.23]端口22。
debug1:已建立连接。
debug1:identity file/Users/felipereis/.ssh/id\u rsa类型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:启用协议2.0的兼容模式
debug1:本地版本字符串SSH-2.0-OpenSSH_6.2
debug1:远程协议版本2.0,远程软件版本OpenSSH_6.2
debug1:match:OpenSSH_6.2 pat OpenSSH*
debug1:SSH2\u MSG\u KEXINIT已发送
debug1:SSH2\u MSG\u KEXINIT已收到
debug1:kex:server->client aes128 ctr hmac-md5-etm@openssh.com没有一个
debug1:kex:客户端->服务器aes128 ctr hmac-md5-etm@openssh.com没有一个

debug1:SSH2_MSG_KEX_DH_GEX_请求(1024听起来像~/.ssh/authorized_keys下的密钥被弄乱了,或者文件被删除了

请尝试以下操作:

  • 停止EC2实例
  • 分离根卷(/dev/sda1)——假设这是
    卷A
  • 启动相同类型和相同凭据的新EC2实例
  • 卷A
    作为
    /dev/sdf
  • ssh连接到他的新实例
  • mkdir-p/mnt/xvdf
  • mount/dev/xvdf/mnt/xvdf
  • cp~/.ssh到/mnt/xvdf/home/ec2 user/
  • chmod 700/mnt/xvdf/home/ec2用户
  • chmod 600/mnt/xvdf/home/ec2用户/授权密钥
  • 关闭新实例
  • 在新实例上分离
    卷A
  • 在原始实例的/dev/sda1上重新连接
    卷A
  • 启动原始实例

  • 您现在应该可以登录。

    根据您的AMI,公钥可能会添加到ec2用户的其他用户的授权密钥文件中

    要找到答案,您可以在EC2控制台中查看该实例的引导日志,它应该输出cloud init用作“默认用户”的用户名。我的有一行如下所示:

    ci-info: +++++++++++++++++++++Authorized keys from /home/ec2-user/.ssh/authorized_keys for user ec2-user++++++++++++++++++++++
    
    您也可以尝试以root用户身份登录,因为这样有时会出现类似“请以用户“ec2 user”而不是用户“root”的身份登录”的错误

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

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

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

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

    谢谢您的回答。步骤8应该是
    cp~/.ssh/mnt/xvdf/home/ec2 user/
    步骤10应该是
    chmod 600/mnt/xvdf/home/ec2 user/.ssh/authorized_keys
    。但是这对我不起作用。我的第一个实例是破裂,所以我无法恢复。我不得不用新创建的一个将其转储。谢谢!