git之生成SSH key
SSH 為 Secure Shell 的縮寫,由 IETF 的網絡小組(Network Working Group)所制定。利用 SSH 協議可以有效防止遠程管理過程中的信息泄露問題。簡單說,SSH是一種網絡協議,用於計算機之間的加密登錄。
1.生成一個新的SSH key
1.1 生成命令:
$ ssh-keygen -t rsa -b 2048 -C “郵箱地址”
回車后輸出如下:
Generating public/private rsa key pair. Enter file in which to save the key (/home/xxx/.ssh/idrsa):
xxx為用戶名,回車后輸出如下:
Enter passphrase (empty for no passphrase): Enter same passphrase again:
這步是讓你輸入密碼,此密碼在上傳代碼時要使用,一般設為github密碼,方便記憶。
兩次輸入密碼后,回車生成成功。
生成成功后在~/.ssh/目錄會生成兩個文件,id_rsa和id_rsa.pub ,如下:

2.將SSH key添加到ssh-agent
先確認ssh-agent處於啟用狀態: eval “$(ssh-agent -s)”
輸出類似於:
Agent pid 32070
執行ssh-add時出現Could not open a connection to your authentication agent若執行ssh-add /path/to/xxx.pem是出現這個錯誤:Could not open a connection to your authentication agent,則先執行如下命令即可:ssh-agent bash
然后將SSH key添加到ssh-agent:
ssh-add ~/.ssh/id_rsa
這時又會要你輸入密碼:
Enter passphrase for /home/xxx/.ssh/id_rsa: 輸入剛才記起來的密碼,然后回車
3.將SSH key添加到Github賬戶中
這一步就是將本地和github連接起來的關鍵
3.1將id_rsa.pub里面的內容原樣全部復制起來。
3.2打開github.com,登入賬戶,點擊頭像旁邊的下拉按鈕,選擇settings —- ssh and gpg keys —— new ssh key —- 粘貼 —- 保存。


完成這些就可以向github上上傳項目代碼了,否則會出現報錯 git@github.com: Permission denied (publickey)。
鑽研不易,轉載請注明出處。
