2014-04-30 180 views
21

我試圖用SSH連接到我的EC2實例,我變得瘋狂。我已閱讀這篇文章,嘗試了所有用戶的組合:AWS SSH連接錯誤:權限被拒絕(公鑰)

AWS ssh access 'Permission denied (publickey)' issue

它仍然沒有爲我工作。任何想法我錯過了什麼?

[email protected]:~/keys$ ssh -v -i ec2-key-pair.pem [email protected] 
OpenSSH_6.6, OpenSSL 1.0.1f 6 Jan 2014 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: /etc/ssh/ssh_config line 19: Applying options for * 
debug1: Connecting to ec2-54-72-242-0.eu-west-1.compute.amazonaws.com [54.72.242.0] port 22. 
debug1: Connection established. 
debug1: identity file ec2-key-pair.pem type -1 
debug1: identity file ec2-key-pair.pem-cert type -1 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_6.6p1 Ubuntu-2ubuntu1 
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2 
debug1: match: OpenSSH_6.2 pat OpenSSH* compat 0x04000000 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr [email protected] none 
debug1: kex: client->server aes128-ctr [email protected] none 
debug1: sending SSH2_MSG_KEX_ECDH_INIT 
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY 
debug1: Server host key: ECDSA e4:06:ee:a5:a5:d2:97:5f:0f:b7:06:5e:f2:b3:da:26 
debug1: Host 'ec2-54-72-242-0.eu-west-1.compute.amazonaws.com' is known and matches the ECDSA host key. 
debug1: Found key in /home/roberto/.ssh/known_hosts:3 
debug1: ssh_ecdsa_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: ec2-key-pair.pem 
debug1: key_parse_private2: missing begin marker 
debug1: read PEM private key done: type RSA 
debug1: Authentications that can continue: publickey 
debug1: No more authentication methods to try. 
Permission denied (publickey). 

UPDATE: 

根據@aldanux建議:

[email protected]:~/keys$ ssh-keygen -R 54.72.242.0 
# Host 54.72.242.0 found: line 4 type ECDSA 
/home/roberto/.ssh/known_hosts updated. 
Original contents retained as /home/roberto/.ssh/known_hosts.old 
[email protected]:~/keys$ ssh -i ec2-key-pair.pem [email protected] 
Warning: Permanently added the ECDSA host key for IP address '54.72.242.0' to the list of known hosts. 
Permission denied (publickey). 
+0

它看起來像'EC2琴鍵pair.pem'無效/損壞,' 「key_parse_private2:缺少開始標記」'。 – PlasmaPower

+0

@PlasmaPower查看我的評論如下。謝謝。 – Rober

回答

3

試試這個步驟:

ssh-keygen -R 54.72.242.0 

sudo chmod 600 ec2-key-pair.pem 

然後:

ssh -i ec2-key-pair.pem [email protected] 
+1

從你建議的執行中查看我的更新評論。仍然一樣:( – Rober

+1

我更新我的答案...嘗試sudo chmod到你的密鑰對...並告訴我發生了什麼...我現在要走了...我後來顯示您的評論 – aldanux

+0

得到了同樣的錯誤:( – Rober

33

你可能登錄的用戶錯誤。如果它是一個Ubuntu的實例的命令是:

ssh -v -i ec2-key-pair.pem [email protected] 
+3

就我而言,我們一直在使用Ubuntu,因此我習慣於使用ubuntu用戶:ubuntu @ host。測試Amazon Linux AMI時出現此錯誤。對於亞馬遜Linux,你必須連接爲ec2-user @主機 –

+0

使我的一天,多麼愚蠢 – n3rd

+0

這是我正在尋找的答案.. Tnx :) –

5

我有一個類似的問題,「key_parse_private2:缺少開始標記」,同時使用用戶名「EC2用戶」,但是當我改變到Ubuntu的用戶它得到了固定。

0

是的,的確是相當具有誤導性的信息。在我的例子中,我使用了錯誤的鍵。

我們需要刪除密鑰對並創建新密鑰對,除了我們的實例使用舊密鑰對(因爲您不能那麼容易)。

錯誤消息是相同的,所以它是值得檢查您的實例aws面板中的鍵名稱匹配您在密鑰巴黎使用的密鑰對。

1

另一件要檢查的是PermitRootLoginAllowUsers/etc/ssh/sshd_config

如果您的用戶訪問權限受限,即使成功授權後也會出現此debug1: key_parse_private2: missing begin marker

15

雖然沒有具體到AWS,這無益的錯誤信息

debug1: key_parse_private2: missing begin marker

的晦澀的場景,比如當SSH用戶的主目錄的所有權(或權限)是不正確的一小撮下會發生在遠程機器上。

解決此類和類似隱晦消息的最佳方法是檢查遠程計算機上的授權日誌,前提是您有權訪問,因爲它通常會查明問題所在。在Debian和Ubuntu系統中,這是最容易tail(使用sudo的如適用)完成:

tail -f -n 80 /var/log/auth.log

在我的具體情況,我發現

Authentication refused: bad ownership or modes for directory /var/www

完全準確,簡明:所有者:組被設​​置爲daemon:daemon,當它應該是www-data:www-data(這是在一臺Ubuntu機器上,過去必須安裝其他一些網絡服務器)。

+0

非常感謝。 .ssh目錄的權限是775,它授予對組成員的寫入權限。因此連接被拒絕。把它改成755的確有竅門。 – egelev

+0

謝謝,在遠程計算機上的'auth.log'中解決了我的問題,這是'/home/backup/.ssh/authorized_keys文件中的錯誤選項...'(''中沒有引號(''')' from =「ip1,ip2」')。但實際上'debug1:key_parse_private2:missing begin marker'仍然顯示在'ssh -v'裏,即使它工作。 – dentarg

+1

@dentarg感謝分享這個有用的觀察:'debug1:key_parse_private2 :missing missing marker'仍然顯示在'ssh -v'輸出中,即使它工作。嗯...所以也許這個消息是一個「紅鯡魚」在我的情況。我想這個答案仍然有用,我想知道這條消息的真正根源是什麼 –

0

以「admin」身份登錄爲我工作。根據您的實例類型,登錄用戶會更改。 ec2用戶或Ubuntu的或在我的情況下管理員。 SSH -v -i ./my_key_file.pem [email protected]

同時確保爲PEM文件的權限爲600 搭配chmod 600 ./my_key_file .pem

0

發生此錯誤的一個簡單方法是損壞的.pem文件。

例如,如果最近的行缺失,您將得到「缺少開始標記」。

確保的.pem結尾:

-----END RSA PRIVATE KEY-----