Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案

Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案

0 写这个的原因是,本来很简单的一个问题,我看了一下网上的帖子,大部分都是在ctrl-c/v加胡说八道,也不注明出处,诶…


1 报错分析

今天在gitee上面准备clone一个项目,由于我使用的云服务器很久没用了,出现了以下报错:

[root@VM-4-16-centos home]# git clone git@gitee.com:heiyueGit/springboot-flowable.git demo-app
Cloning into 'demo-app'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因翻译过来就是:“拒绝服务(公共秘钥)”,也就是说由于ssh公钥的某些问题导致连接失败,因此我查看了以下我的gitee的ssh设置(gitee网页点头像-设置-ssh公钥)如下:

Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案
……发现以前的公钥信息被删除了,这就是导致连接失败的原因。
接下来,我们在使用ssh连接一下测试

[root@VM-4-16-centos .ssh]# ssh -t git@gitee.com
Permission denied (publickey).
[root@VM-4-16-centos .ssh]# ssh -tv git@gitee.com
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
……(省略)……
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

……大致意思就是找来找去都没找到publickey,所以连接会失败

2 解决方案

既然找到了原因,那我就不具体分析了,直接重新生成ssh密钥对就行了,然后把该密钥对的公钥部分设置到上面那个地方就行了!
操作过程我尽力做到面面俱到,因此下面的操作兼顾了win和linux平台,兼顾github和gitee!

2.1 Linux

[root@VM-4-16-centos ~]# cd ~/.ssh  
[root@VM-4-16-centos .ssh]# ll
total 0
[root@VM-4-16-centos .ssh]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:aD92e78/rhnpxF2DSVmMkJfEMvTbJWnMNxkczakdKJk root@VM-4-16-centos
The key's randomart image is:
+---[RSA 2048]----+
|          .oB.*=+|
|           E.Oo*=|
|            =+Oo+|
|       .    .o=+o|
|      o S    + o.|
|     . .    . o o|
|        + .  = . |
|       . o .+ o. |
|          .. ==+o|
+----[SHA256]-----+
[root@VM-4-16-centos .ssh]# ll
total 8
-rw------- 1 root root 1679 May 21 22:20 id_rsa
-rw-r--r-- 1 root root  401 May 21 22:20 id_rsa.pub
[root@VM-4-16-centos .ssh]# cat id_rsa.hub
-bash: caid_rsa.hub: command not found
[root@VM-4-16-centos .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA(马赛克)QDu7haN1azW4SkcbM8zn+fV7bdr56MTw1gMlQSl1Bfh2h2ky0CszAasSj3L1yrw8RJYj5YGavYgTfQ4Dtxfc5I9X4Shk0k/Y94BklMxlNhb1uq8GEBgmFMm(马赛克)/lzfyk7QYMqxdYlFu7T7ku+vFHh2ZBduTrLV9iqiPXetJxqJ5EGvMaQxfKdn8u+Al8+99bi8ObBFAjN1G/DrCxIIjg+fvZ root@VM-4-16-centos
[root@VM-4-16-centos .ssh]# 
  1. 进入~/.ssh目录,里面应该是什么都没有(有其他的ssh密码要信息也是正常的)
  2. ssh-keygen 生成ssh密钥对,一路回车就可以了,生成的密钥对拥有默认名称 id_rsa和id_rsa.pub,pub那个就是公钥的意思。
  3. 复制公钥信息
  4. (对于gitee的操作)进入gitee或gitee密钥对设置页面,标题随便,公钥粘贴进来(一定要完整),确定4. (对于github的操作)和上面一模

Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案

  1. 测试
[root@VM-4-16-centos .ssh]# ssh -t git@gitee.com
Warning: Permanently added 'gitee.com,212.64.62.183' (ECDSA) to the list of known hosts.
Hi imoder! You've successfully authenticated, but GITEE.COM does not provide shell access.
Connection to gitee.com closed.

[root@VM-4-16-centos .ssh]# ssh -t git@github.com
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi imoderiii! You've successfully authenticated, but GitHub does not provide shell access.
[root@VM-4-16-centos .ssh]# 

……有没有看到successfully 。ok(警告先不管,后面再说!)

clone一下试试?

[root@VM-4-16-centos home]# git clone git@gitee.com:heiyueGit/springboot-flowable.git
Cloning into 'springboot-flowable'...
remote: Enumerating objects: 899, done.
remote: Counting objects: 100% (899/899), done.
remote: Compressing objects: 100% (694/694), done.
remote: Total 899 (delta 192), reused 816 (delta 166), pack-reused 0
Receiving objects: 100% (899/899), 4.17 MiB | 0 bytes/s, done.
Resolving deltas: 100% (192/192), done.

……解决了有没有。

2.2 wins

  1. 打开你的cmd或者git的bash,如下操作,
iModer@DESKTOP-G2HBVIK MINGW64 /f
$ ssh-keygen.exe
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/John/.ssh/id_rsa):
Created directory '/c/Users/John/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/John/.ssh/id_rsa.
Your public key has been saved in /c/Users/John/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YFDOEXuUjGfbECTlLfB9FRK1XXFQGukRbOhovMqpu6I iModer@DESKTOP-G2HBVIK
The key's randomart image is:
+---[RSA 3072]----+
|    ..**=o  o=BB=|
|     +.O*o  .o==o|
|      *o==oo.oo..|
|     . o..=.. .  |
|        S. .     |
|          .      |
|       . o       |
|    .   +        |
|  E. .++         |
+----[SHA256]-----+

……生成ssh密钥对,唯一的不同就是……额,生成的目标文件夹不同,在/c/Users/yours/.ssh’下,然后复制下公共秘钥,后面的操作就和前面一样了。

3 Warning: Permanently added ‘gitee.com,212.64.62.183’ (ECDSA) to the list of known hosts.的解决方案

其实建议可以在进行域名解析时将相应的域名设置为永久,也就是在你的hosts文件中显示配置一下:
linux下hosts文件为**/etc/hosts**
Permission denied (publickey). fatal: Could not read from remote repository.问题法解决方案
wins的hosts文件应该在C:\Windows\System32\drivers\etc文件夹下?(你可以百度一下),添加的内容和上面一模一样,保存退出就可以了。
测试一下:

[root@VM-4-16-centos home]# ssh -t git@gitee.com
Hi imoder! You've successfully authenticated, but GITEE.COM does not provide shell access.
Connection to gitee.com closed.

大功告成!,溜了溜了。

上一篇:git clone时出现Permission denied (publickey)


下一篇:Django rest Framework(DRF)源码解析——权限