1. 程式人生 > >git之生成SSH key

git之生成SSH key

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上上傳專案程式碼了,否則會出現報錯   [email protected]: Permission denied (publickey)。

 

 

  鑽研不易,轉載請註明出處。