Git clone遇到git@github.com: Permission denied (publickey). fatal: Could not read

3,342 阅读1分钟

问题描述与排查

今天进行对一个repo进行git clone报错如下:

git clone git@github.com:JiexingQi/picard.git
Cloning into 'picard'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

参考网上的博客,我们可以先使用这个命令来检查一下.

(picard) jxqi@han-server-01:~/text2sql$ ssh -T git@github.com
git@github.com: Permission denied (publickey).

果不其然,连正常的github都无法连接。提示我们缺少ssh公钥(publickey)。因而考虑是否在该服务器上正确配置ssh密钥信息。

可以使用如下命令进行检查是否存在ssh配置文件:

ls -al ~/.ssh

显示结果如下:

(picard) jxqi@han-server-01:~/text2sql$ ls -al ~/.ssh
total 16
drwxrwxr-x  2 jxqi jxqi 4096 Dec 20 17:02 .
drwxr-xr-x 17 jxqi jxqi 4096 Feb 11 16:43 ..
-rw-rw-r--  1 jxqi jxqi 2302 Jan 25 09:38 authorized_keys
-rw-r--r--  1 jxqi jxqi 1110 Jan  4 09:55 known_hosts

可以看到,这里缺少本地SSH 密钥。

问题解决

找到问题后,我们只需正确配置ssh密钥即可。配置命令如下:

ssh-keygen -t rsa -b 4096 -C "*********@qq.com"

这里只需要填写自己的github上的注册邮箱即可。我这里是qq邮箱。

反馈结果如下:

(picard) jxqi@han-server-01:~/text2sql$ ssh-keygen -t rsa -b 4096 -C "*******@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jxqi/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/jxqi/.ssh/id_rsa.
Your public key has been saved in /home/jxqi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VmyKNm5jC98ewW/BJ0f+ydD5JcMD2zUsxlcGn4XNj3rYs ********@qq.com
The key's randomart image is:
+---[RSA 4096]----+
|              .+=|
|         .    .o+|
|          + o. k++|
|       o = od *+o*|
|      + S + * O++|
|     o o o = +.*.|
|    . = . o  .+..|
|     = + o  E .  |
|      o.o        |
+----[SHA256]-----+

之后检查ssh配置文件和github连接情况。

(picard) jxqi@han-server-01:~/text2sql$ ls -al ~/.ssh
total 24
drwxrwxr-x  2 jxqi jxqi 4096 Feb 13 14:06 .
drwxr-xr-x 17 jxqi jxqi 4096 Feb 11 16:43 ..
-rw-rw-r--  1 jxqi jxqi 2302 Jan 25 09:38 authorized_keys
-rw-------  1 jxqi jxqi 3243 Feb 13 14:06 id_rsa
-rw-r--r--  1 jxqi jxqi  743 Feb 13 14:06 id_rsa.pub
-rw-r--r--  1 jxqi jxqi 1110 Jan  4 09:55 known_hosts
(picard) jxqi@han-server-01:~/text2sql$ ssh -T git@github.com
Hi JiexingQi! You've successfully authenticated, but GitHub does not provide shell access.

可以看到,已经能够正常连接了,接下来进行git clone就顺利很多了。

(picard) jxqi@han-server-01:~/text2sql$ git clone git@github.com:JiexingQi/picard.git
Cloning into 'picard'...