git 使用出现: Permission denied (publickey). Could not read from remote repository.

出现错误的场景

git clone命令为:[email protected]:wuchao226/ProvinceCityDistrict.git
在ternimal下执行这条语句的时候,出现错误:

[email protected]: Permission denied (publickey). Could not read from remote repository.  
Please make sure you have the correct access rights and the repository exists.

原因

原因是由于你在本地(或者服务器上)没有生成 ssh key

cd ~/.ssh 和 ls 命令来查看是否有文件 id_rsa 以及文件 id_rsa.pub,如下图所示:(我的已经生成了,所以我ls后会显示。)


ssh.png

解决办法

1、生成新的ssh key

如果你没有ssh key的话,在ternimal下输入命令:ssh-keygen -t rsa -C “[email protected]”, [email protected]改为自己的邮箱即可,途中会让你输入密码啥的,不需要管,一路回车即可,会生成你的ssh key。(如果重新生成的话会覆盖之前的ssh key。)

默认会在相应路径下(/your_home_path)生成id_rsa和id_rsa.pub两个文件,如下面代码所示

gcl:~ smartlei$ ssh-keygen -t rsa -C "http://igit.58corp.com"
Generating public/private rsa key pair.
//说明会在这个路径下/Users/smartlei/.ssh/id_rsa 生成id_rsa和id_rsa.pub文件,
//如果你敲入别的地址则为在别的地址生生成,例如我在/Users/smartlei/.ssh/id_rsa_igit
//这个文件下生成id_rsa_igit 文件文件名也可以修改()
Enter file in which to save the key (/Users/smartlei/.ssh/id_rsa): /Users/smartlei/.ssh/id_rsa_igit
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/smartlei/.ssh/id_rsa_igit.
Your public key has been saved in /Users/smartlei/.ssh/id_rsa_igit.pub.
The key fingerprint is:
SHA256:++Ik2wTYxuEz5UP/G+XEo7ju8pM8SZhol0ifwdZa15U http://igit.58corp.com
The key's randomart image is:
+---[RSA 2048]----+
|                .|
|               E.|
|      ..o.   . . |
|     =.=+.o ...  |
|    ..O=SO..  =  |
|     .++Oo.o = . |
|     ...+o.o+ .  |
|       *o.*. o   |
|      ..o**o.    |
+----[SHA256]-----+

2、在 ternimal下执行命令:

ssh -v [email protected]

最后两句会出现:

No more authentication methods to try.
Permission denied (publickey).

3、再在ternimal下输入:

ssh-agent -s

然后会提示类似的信息:

SSH_AUTH_SOCK=/tmp/ssh-GTpABX1a05qH/agent.404; export SSH_AUTH_SOCK;

SSH_AGENT_PID=13144; export SSH_AGENT_PID;

echo Agent pid 13144;

4、接着再输入:

ssh-add ~/.ssh/id_rsa

这时候应该会提示:

Identity added: …(这里是一些ssh key文件路径的信息)

(注意)如果出现错误提示:

Could not open a connection to your authentication agent.

请执行命令:eval ssh-agent -s后继续执行命令 ssh-add ~/.ssh/id_rsa,这时候一般没问题啦。

5、打开你刚刚生成的 id_rsa.pub,

将 id_rsa.pub 里面的内容复制,进入你的 github 账号,在 settings 下,SSH and GPG keys下new SSH key,title随便取一个名字,然后将 id_rsa.pub 里的内容复制到Key中,完成后Add SSH Key。

6、最后一步,验证Key

在ternimal下输入命令:

ssh -T [email protected]

提示:Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.

这时候你的问题就解决啦,可以使用命令 git clone –recursive [email protected]:wuchao226/ProvinceCityDistrict.git 去下载你的代码啦。

你可能感兴趣的:(git 使用出现: Permission denied (publickey). Could not read from remote repository.)