使用ssh key這種方式進行clone ,pull github上面的項目,使用 git clone或者git pull origin master出現permission denied (publickey),原因是因為ssh key過期失效或者沒有ssh key。 那么解決這種的問題的方法就是重新生成一個新的ssh key ,然后將這個ssh key添加到github賬戶上面,就可以了。
(1) 檢查SSH key是否已經存在
用這個命令 ls ~/.ssh/
進行檢查 id_rsa.pub 是否存在,如果存在,就不用生成一個新的SSH key了,直接跳到下面的第3步。
(2)如果第1步中的SSH key不存在,生成一個新的SSH key
命令如下:
ssh-keygen -t rsa -b 2048 -C “your_email@example.com”
其中,your_email@example.com要修改成你的郵箱地址。
回車后輸出如下:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxx/.ssh/id_rsa):
其中,xxx是你的用戶名,直接回車,會將key保存到默認文件中。
接着會輸出:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
這兩步是讓你輸入一個密碼,以及確認密碼,這個密碼在你提交代碼到Github時會用到【注意:記住這個密碼,最簡單的方式就是設置的和github賬戶登入密碼一樣,容易記住】
回車后就提示成功了:
Your identification has been saved in /home/xxx/.ssh/id_rsa.
Your public key has been saved in /home/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
到這一步,你會發現 ~/.ssh/id_rsa.pub 文件已經生成了。
(3)將SSH key添加到ssh-agent
先確認ssh-agent處於啟用狀態:
eval “$(ssh-agent -s)”
輸出類似於:
Agent pid 32070
然后將SSH key添加到ssh-agent:
ssh-add ~/.ssh/id_rsa
這時又會要你輸入密碼:
Enter passphrase for /home/xxx/.ssh/id_rsa:
輸入剛才記起來的密碼,然后回車
(4)將SSH key添加到Github賬戶中
這一步,不用像網上說的那么復雜,直接在打開id_rsa.pub這個文件(這就是我們剛剛生成的ssh key文件),一般大致路徑如下(每個人電腦不同,路徑也會不同):系統盤符 —- 用戶名 —- 計算機用戶名 —-.ssh ,在這里名就可以看到 id_rsa、id_rsa.pub 、known_host這三個文件,打開id_rsa.pub,將里面的內容原樣全部復制起來。打開github.com,登入賬戶,點擊頭像旁邊的下拉按鈕,選擇settings —- ssh and gpg keys —— new ssh key —- 粘貼 —- 保存。如下圖:
(5)保存后,就能在上面看見剛建立的ssh key,之后在git 客戶端就能夠使用了