permission denied (publickey)问题的

2019-03-14  本文已影响0人  传说中的汽水枪

使用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 "your_email@example.com"

其中,your_email@example.com要修改成你的邮箱地址。
回车后输出如下:

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 your_email@example.com 

到这一步,你会发现 /Users/your_user_name/.ssh/id_rsa.pub 文件已经生成了。

3. 将ssh key添加到git

打开id_rsa.pub文件,复制里面的内容,然后粘贴到git中相关的位置中。
例如对于gitlab 来说:

image.png

4. 把ssh 添加到keychain中

这个时候如果去git clone代码,会让你输入密码,如果一个还好说,如果关联了很多的话,那就比较麻烦了,这个时候的解决方法就是添加到keychain中:

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

重启mac后秘钥被清空解决办法

4.1 (评论区留言提出)

如果出现:

Could not open a connection to your authentication agent. 

那么就使用如下命令解决:

ssh-agent bash

注:

.ssl 文件是隐藏的
可以用如下的方式显示:
开启显示隐藏文件:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE

关闭显示隐藏文件:

defaults write com.apple.finder AppleShowAllFiles -bool FALSE

运行完命令后,要重启一下Finder。

上一篇下一篇

猜你喜欢

热点阅读