git@github.com: Permission denied (publickey).

不念不忘少年蓝@ 2024-03-31 13:39 117阅读 0赞

本地虚拟机ubuntu上安装git,想从github上拉取项目到ubuntu上的过程。

1、在ubuntu上安装git

更新apt指令

  1. sudo apt update

安装git

  1. sudo apt install git

查看安装git版本

  1. git --version

2、ssh认证

首先已经安装了ssh指令

先执行

  1. ssh -T git@github.com
  2. 执行之后提示:Permission denied (publickey).

接下来通过Ubuntu下的ssh-keygen命令创建公钥/私钥对:(使用你注册github时的邮箱)

  1. ssh-keygen -C "1450053947@qq.com" -f ~/.ssh/github

会得到下面提示

  1. root@ljy-virtual-machine:~# ssh-keygen -C "1450053947@qq.com" -f ~/.ssh/github
  2. Generating public/private rsa key pair.
  3. /root/.ssh/github already exists.
  4. Overwrite (y/n)? y
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/github
  8. Your public key has been saved in /root/.ssh/github.pub
  9. The key fingerprint is:
  10. SHA256:i8oSSUf/SVLNw2zPPx2FO55vq2S1b2787gtXOZ/hR4s 1450053947@qq.com

去/root/.ssh下查看

  1. root@ljy-virtual-machine:~# ls ~/.ssh
  2. github github.pub known_hosts

发现有刚才生成的私钥github和公钥github.pub

查看公钥,并将公钥复制到github的框里(下图红框里的内容复制到github框里)

  1. cat /root/.ssh/github.pub

5fdb25a5003243adbbf37e0452b8238e.png

6c9579a8b1ec44089e8290637300529a.png

  1. git@github.com: Permission denied (publickey).

5e31f5386b3e474ebd5a41d23f3e51ef.png

此时在运行指令

  1. ssh -T git@github.com

如果提示让输入yes就输入yes,然后再次执行上面的指令。

如果此时仍然提示

  1. git@github.com: Permission denied (publickey).

那么可使用下面指令进行调试

  1. ssh -T -v git@github.com

得到信息为:

  1. root@ljy-virtual-machine:~# ssh -T -v git@github.com
  2. OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1d 10 Sep 2019
  3. debug1: Reading configuration data /etc/ssh/ssh_config
  4. debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
  5. debug1: /etc/ssh/ssh_config line 21: Applying options for *
  6. debug1: Connecting to github.com [20.205.243.166] port 22.
  7. debug1: Connection established.
  8. debug1: identity file /root/.ssh/id_rsa type -1
  9. debug1: identity file /root/.ssh/id_rsa-cert type -1
  10. debug1: identity file /root/.ssh/id_dsa type -1
  11. debug1: identity file /root/.ssh/id_dsa-cert type -1
  12. debug1: identity file /root/.ssh/id_ecdsa type -1
  13. debug1: identity file /root/.ssh/id_ecdsa-cert type -1
  14. debug1: identity file /root/.ssh/id_ecdsa_sk type -1
  15. debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1
  16. debug1: identity file /root/.ssh/id_ed25519 type -1
  17. debug1: identity file /root/.ssh/id_ed25519-cert type -1
  18. debug1: identity file /root/.ssh/id_ed25519_sk type -1
  19. debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1
  20. debug1: identity file /root/.ssh/id_xmss type -1
  21. debug1: identity file /root/.ssh/id_xmss-cert type -1
  22. debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
  23. debug1: Remote protocol version 2.0, remote software version babeld-2aa8e17d
  24. debug1: no match: babeld-2aa8e17d
  25. debug1: Authenticating to github.com:22 as 'git'
  26. debug1: SSH2_MSG_KEXINIT sent
  27. debug1: SSH2_MSG_KEXINIT received
  28. debug1: kex: algorithm: curve25519-sha256
  29. debug1: kex: host key algorithm: ecdsa-sha2-nistp256
  30. debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
  31. debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
  32. debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
  33. debug1: Server host key: ecdsa-sha2-nistp256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM
  34. debug1: Host 'github.com' is known and matches the ECDSA host key.
  35. debug1: Found key in /root/.ssh/known_hosts:1
  36. debug1: rekey out after 134217728 blocks
  37. debug1: SSH2_MSG_NEWKEYS sent
  38. debug1: expecting SSH2_MSG_NEWKEYS
  39. debug1: SSH2_MSG_NEWKEYS received
  40. debug1: rekey in after 134217728 blocks
  41. debug1: Will attempt key: /root/.ssh/id_rsa
  42. debug1: Will attempt key: /root/.ssh/id_dsa
  43. debug1: Will attempt key: /root/.ssh/id_ecdsa
  44. debug1: Will attempt key: /root/.ssh/id_ecdsa_sk
  45. debug1: Will attempt key: /root/.ssh/id_ed25519
  46. debug1: Will attempt key: /root/.ssh/id_ed25519_sk
  47. debug1: Will attempt key: /root/.ssh/id_xmss
  48. debug1: SSH2_MSG_EXT_INFO received
  49. debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
  50. debug1: SSH2_MSG_SERVICE_ACCEPT received
  51. debug1: Authentications that can continue: publickey
  52. debug1: Next authentication method: publickey
  53. debug1: Trying private key: /root/.ssh/id_rsa
  54. debug1: Trying private key: /root/.ssh/id_dsa
  55. debug1: Trying private key: /root/.ssh/id_ecdsa
  56. debug1: Trying private key: /root/.ssh/id_ecdsa_sk
  57. debug1: Trying private key: /root/.ssh/id_ed25519
  58. debug1: Trying private key: /root/.ssh/id_ed25519_sk
  59. debug1: Trying private key: /root/.ssh/id_xmss
  60. debug1: No more authentication methods to try.
  61. git@github.com: Permission denied (publickey).

看以上信息发现是

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ecdsa_sk
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Trying private key: /root/.ssh/id_ed25519_sk
debug1: Trying private key: /root/.ssh/id_xmss
debug1: No more authentication methods to try.

这几个的问题,意思是一直在尝试获取/root/.ssh/id_rsa /root/.ssh/id_dsa

使用指令,发现没有id_rsa 和id_dsa

  1. root@ljy-virtual-machine:~# ls ~/.ssh
  2. github github.pub known_hosts

因此来到/root/.ssh下

  1. cd ~/.ssh

使用下面指令,copy它们两个

  1. root@ljy-virtual-machine:~/.ssh# cp github id_rsa && cp github.pub id_rsa.pub

再次执行命令

  1. ssh -T git@github.com

得到提示信息:

Hi liujianyon1! You’ve successfully authenticated, but GitHub does not provide shell access.

3、总结

以上就是整个配置git的内容,可能写的不是很仔细,认真看应该没问题,看好你!!!嘻嘻

借鉴博客:

Ubuntu下使用git提交代码至GitHub - 走看看

关于ssh连接主机,git连接github失败的问题:ssh -T git@github.com&&Permission denied (publickey)_SunnyPotter的博客-CSDN博客

发表评论

表情:
评论列表 (有 0 条评论,117人围观)

还没有评论,来说两句吧...

相关阅读