轉自http://www.javashuo.com/article/p-rxqxecov-ht.htmlgit
使用ssh key這種方式進行clone ,pull github上面的項目,使用 git clone或者git pull origin master出現permission denied (publickey),緣由是由於ssh key過時失效或者沒有ssh key。 那麼解決這種的問題的方法就是從新生成一個新的ssh key ,而後將這個ssh key添加到github帳戶上面,就能夠了。github
(1) 檢查SSH key是否已經存在markdown
用這個命令 ls ~/.ssh/
進行檢查 id_rsa.pub 是否存在,若是存在,就不用生成一個新的SSH key了,直接跳到下面的第3步。ssh
(2)若是第1步中的SSH key不存在,生成一個新的SSH keyide
命令以下:.net
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 文件已經生成了。blog
(3)將SSH key添加到ssh-agentget
先確認ssh-agent處於啓用狀態:
eval 「$(ssh-agent -s)」it
輸出相似於:io
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 客戶端就可以使用了