簡體   English   中英

EC2密鑰對在一個實例中起作用,但在另一個實例上失敗-權限被拒絕(公鑰)

[英]EC2 keypair works in one instance but fails on other - Permission denied (publickey)

我已經閱讀了許多有關此主題的帖子,但沒有一個可以幫助我解決問題。

我有一台使用以下SSH命令連接的Amazon EC2機器:

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

到目前為止,我從未遇到過此命令的問題。 它剛剛停止工作,將顯示以下消息: 權限被拒絕(公鑰)。 從哪兒冒出來!

我真的不明白為什么幾乎每天都使用的同一命令突然無法正常工作。 也許我已經做了一些我不應該做的事情,但是我很難弄清楚是什么。

第一次發生時,我正在為Web應用程序(Atlassian Bamboo)創建服務,但是我不確定這是否與錯誤有關。

我已經重啟了幾次機器,一次又一次地嘗試,但沒有成功。

下面顯示帶有-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實例,所以也許是第一台機器上發生了什么

聽起來〜/ .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. 啟動原始實例。

您應該可以立即登錄。

根據您的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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM