由於SSH配置文件的不匹配,導致的Permission denied (publickey)及其解決方法


讀者如要轉載,請標明出處和作者名,謝謝。 
地址01:http://space.itpub.net/25851087 
地址02:http://www.cnblogs.com/zjrodger/ 
作者名:zjrodger 

【問題發生環境和相關參數】

(1)OS:Win7 32Bit.

(2)Git:GitHub for Windows 2.0.

    下載地址:https://windows.github.com/

(3)Command Shell:Git Shell.

【問題重現描述】
 
    在Win7本地平台上安裝完Github for Windows 2.0后,要在本地生成密匙,向Github上上傳密匙並且進行網絡連通性測試。
    ①利用命令“ ssh-keygen -t rsa -C "zjrodger@163.com" 生成SSH密匙( id_rsaid_rsa.pub)后,將本地的“ id_rsa.pub”文件中的內容上傳到Github上的個人“SSH Keys”管理項中,從而生成一個新的SSH Keys。
    ②之后,進行本地與Remote Server(Github網站)的連接測試,命令和結果如下所示:
F:\Workspaces\Github_Workspace> ssh -T git@github.com 
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of know 
n hosts. 
Permission denied (publickey).
    在Windows的PowerShell中輸入“ ssh -T git@github.com”,結果出現“ Permission Denied(publickey)


【問題原因】
    在Github for Windows 2.0 默認的安裝配置中,  
SSH的配置文件 ssh_config中的 IdentityFile“ 
與實際情況不相符。
 
(1)原來默認情況下的IdentifyFile的值
    在Github for Windows 2.0上( 默認安裝情況下),SSH的配置文件 ssh_config中的 IdentityFile(其值為密匙的全路徑名) 這項信息的內容是“ ~/.ssh/github_rsa”,如下命令所示:
Host github.com
 StrictHostKeyChecking no
 UserKnownHostsFile=/dev/null
  IdentityFile= ~/.ssh/github_rsa
 
(2)實際的情形
實際上,通過命令“ ssh-keygen -t rsa -C "zjrodger@163.com”生成的 新的SSH密匙全路徑名為: ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ”。
 
注意 ~/.ssh/ github_rsa  不等於  ~/.ssh/id_rsa
 
(3)結論
①Git 默認安裝情況下,ssh_config配置文件中的“IdentityFile”項的值:IdentityFile= ~/.ssh/github_rsa
②實際的IdentityFile的值:IdentityFile= ~/.ssh/id_rsa
    如上所述,Github for Windows 2.0在 默認安裝情況下,SSH的的配置文件 ssh_config中的 IdentityFile 項的值與實際新創建的密匙全路徑名不相符,結果導致本地的SSH工具無法找到到正確的密匙,進而無法同已經上傳到Github密匙相匹配,結果就出現了“ Permission denied (publickey)”這樣的錯誤。

(4)補充
SSH配置文件ssh_config在自己本地的路徑:
    C:\Users\Administrator\AppData\Local\GitHub\PortableGit_6d98349f44ba975cf6c762a720f8259a267ea445\etc\ssh
密匙文件的存放路徑:
    C:\Users\Administrator\.ssh
ssh_config的原文件(有誤的版本):
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null


Host github.com
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
IdentityFile=~/.ssh/github_rsa
重裝Github for windows 2.0后的 新發現(重要)
    為了確保正確性,自己將本機的Github for Window 2.0卸載並且重裝了一遍,發現SSH的配置文件 ssh_config中的 IdentityFile 依然是“ ~/.ssh/github_rsa”。
而重裝后的密匙文件的存放路徑(C:\Users\Administrator\.ssh)下,有四個密匙文件,分別是 github_rsagithub_rsa.pubid_rsaid_rsa.pub
這樣,用戶就不用自己新建密匙文件了,只用將“ github_rsa.pub”中的內容上傳到Github網站的個人SSH管理中即可。
之后,在本地與Remote端進行網絡連通性測試,發現可以聯通。
這樣,考慮到修改軟件原有配置信息所帶來的隱患,筆者就不推薦自己手動修改SSH的配置文件 ssh_config中的 IdentityFile 字段這個方法了。


【解決方法】
方法一:
 
   在生成新的密匙文件后,若新生成的密匙文件名字為“ id_rsa”,則將ssh_config配置文件中的“IdentityFile”項的值改為“ ~/.ssh/id_rsa
方法二:
 
    將新生成的密匙文件名字改為“ github_rsa”,從而與ssh_config配置文件中的“IdentityFile”項的值相同。
方法三:
    重裝Github for Window 2.0,不用新建密匙文件,而是用Github自帶的“ github_rsa.pub”文件。
總之,不論方法一,放法二還是方法三,一定要保持新生成的密匙文件的名字同“ssh_config”中“IdentityFile”字段的值一致即可。

【參考文檔】

If it says "Permission denied (publickey)" you will have to put in a passphrase for your key. Do not be tempted to just press enter...this was what worked for me...it took me five hours to realize that pressing enter made OpenSSH feel that your key was too public so that is why it is denying you from going to the next step.

So as mentioned in prior answers, the  Permission denied error in Windows is because you are trying to use a key  other than  id_rsa.
Windows lacks the bells and whistles that Linux and Mac have to try out all your public keys when trying to connect to a server via SSH. If you're using the ssh command, you can tell it which key to use by passing the  -i flag followed by the path to the key to use:
F:\Workspaces\Github_Workspace>  ssh -T git@github.com 
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of know 
n hosts. 
Permission denied (publickey). 
F:\Workspaces\Github_Workspace>  ssh -i ~/.ssh/id_rsa git@github.com 
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of know 
n hosts. 
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa': 
Hi zjrodger! You've successfully authenticated, but GitHub does not provide shel 
l access. 
Connection to github.com closed.
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM