【问题标题】:Unable to ssh to Amazon EC2 account. (public key error)无法 ssh 到 Amazon EC2 帐户。 (公钥错误)
【发布时间】:2012-05-14 12:30:52
【问题描述】:

我刚刚启动了一个 EC2 实例,但在启动 ssh 连接时遇到了困难。请注意,我有一个以前的 EC2 实例,它使用相同的密钥对 ssh 运行良好。我很困惑,因为我使用相同的密钥对启动了这个新的 EC2 实例。

以下是我尝试过的。关于这里可能发生的事情的任何专家建议?以及如何解决?

me@ubuntu:~/keys$ ssh -i mykey.pem ubuntu@1.2.3.4
The authenticity of host '1.2.3.4 (1.2.3.4)' can't be established.
RSA key fingerprint is aa:bb:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '1.2.3.4' (RSA) to the list of known hosts.
Permission denied (publickey).

me@ubuntu:~/keys$ chmod 400 mykey.pem
me@ubuntu:~/keys$ ssh -i mykey.pem ubuntu@1.2.3.4
Permission denied (publickey).

me@ubuntu:~/keys$ ssh -v -i mykey.pem ubuntu@1.2.3.4
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 1.2.3.4 [1.2.3.4] port 22.
debug1: Connection established.
debug1: identity file mykey.pem type -1
debug1: identity file mykey.pem-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 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 aa:bb:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc:cc
debug1: Host '1.2.3.4' is known and matches the RSA host key.
debug1: Found key in /home/me/.ssh/known_hosts:10
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: Trying private key: mykey.pem
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

【问题讨论】:

  • 您的 ssh 目录中是否有“公钥”文件?这可能是错误的来源 - ssh 无法将公钥写入钥匙串。
  • @codingjoe 你找到解决方案了吗?我也遇到了这个问题。

标签: ubuntu amazon-ec2 amazon-web-services


【解决方案1】:

关键调试日志是这样的:

debug1: Roaming not allowed by server

您是从 3g/4g 热点连接的吗? Amazon EC2 公然忽略您的 pem 文件。

【讨论】:

  • 我无法通过 SSH 连接到热点上的 ec2 - 有没有简单的解释说明原因?
  • 谢谢 Mikhail,原来是 IP 问题 - Google 告诉我我的外部 IP 地址与我获得 SSH 访问所需的不同。亚马逊也认为它是不同的 - 很奇怪......一旦我找到了正确的,它就起作用了!
  • 您是如何找到正确的 IP 地址的?我想我也有同样的问题。
【解决方案2】:

您使用的密钥对一定是错误的/丢失了,您无法恢复私钥对,因为我曾经丢失了 .pem 文件并且必须重新创建和实例化。 就像密码一样,亚马逊出于安全原因不会保存私钥。

修复它。

转到 aws 管理控制台 1. 停止实例并创建相同的 AMI 映像。 2. 使用创建的 AMI 映像和附加的新密钥对启动一个新实例。 3.然后分配之前分配给旧实例的弹性IP。 4. 如果一切正常,删除旧实例。

然后将 XXXX.pem 文件保存在网上某处。

【讨论】:

    【解决方案3】:

    试试:

    chmod 600 [FULL_PATH_TO_KEYFILE_DIRECTORY]/mykey.pem
    

    改为。

    如果您的 AMI 是 Amazon AMI,请使用 ec2-user 作为您的用户名。

    如果仍然无法正常工作,请尝试将以下内容发送到您的 ~/.ssh/config

    IdentitiesOnly yes
    KeepAlive yes
    ServerAliveInterval 60
    Host ALAIS_FOR_YOUR_HOST
        User ubuntu
        HostName HOST_IP
        IdentityFile FULL_PATH_TO_KEY_FILE
    

    【讨论】:

    • 谢谢,但这仍然不起作用。我将以上内容添加到我的 ~/.ssh/config 文件中,但仍然有相同的错误消息。还有其他建议吗?
    • @codingJoe 您是否先尝试使用chmod 600 YOUR_PEM_KEYFILE?如果它和您的 AMI 不是 Amazon AMI,则在将上述内容添加到您的 ~/.ssh.config 文件后,使用 ssh ALAIS_FOR_YOUR_HOST 进行连接。
    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2021-06-16
    • 2017-04-19
    • 2013-09-20
    相关资源
    最近更新 更多