permission denied (publickey)問題的解決方法

使用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

先:

cd /Users/your_user_name/.ssh 

命令如下:

ssh-keygen -t rsa -b 2048 -C "[email protected]"

其中,`[email protected]`要修改成你的郵箱地址。 

回車後輸出如下: 

Generating public/private rsa key pair. Enter file in which to save the key (/Users/your_user_name/.ssh/id_rsa): 

直接回車,會將key保存到默認文件中。 

接着會輸出: 

Enter passphrase (empty for no passphrase): Enter same passphrase again: 

這兩步是讓你輸入一個密碼,以及確認密碼,這個密碼在你提交代碼到Github時會用到【注意:記住這個密碼,最簡單的方式就是設置的和github賬戶登入密碼一樣,容易記住】 

回車後就提示成功了: 

Your identification has been saved in /Users/your_user_name/.ssh/id_rsa. Your public key has been saved in /Users/your_user_name/.ssh/id_rsa.pub. The key fingerprint is: 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected] 

到這一步,你會發現 `/Users/your_user_name/.ssh/id_rsa.pub` 文件已經生成了。

3. 將ssh key添加到git

打開`id_rsa.pub`文件,複製裏面的內容,然後粘貼到git中相關的位置中。

例如對於gitlab 來說:

 

4. 把ssh 添加到keychain中

這個時候如果去git clone代碼,會讓你輸入密碼,如果一個還好說,如果關聯了很多的話,那就比較麻煩了,這個時候的解決方法就是添加到keychain中:

ssh-add -K /Users/youre_user_name/.ssh/id_rsa 

注:

`.ssl` 文件是隱藏的

可以用如下的方式顯示:

開啓顯示隱藏文件:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE

關閉顯示隱藏文件:

defaults write com.apple.finder AppleShowAllFiles -bool FALSE

運行完命令後,要重啓一下Finder。