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

在尝试使用Git克隆GitHub仓库时,遇到了SSH主机认证失败和权限被拒绝的问题。问题源于自定义了SSH密钥文件名,并未在GitHub正确配置。解决方法包括:一是直接使用ssh-keygen生成默认密钥;二是创建.ssh/config文件,指定私钥路径。通过ssh -T git@github.com测试连接,确保问题已解决。
摘要由CSDN通过智能技术生成

问题记录

git clone github仓库代码时,出现以下bug:

$  git clone git@github.com:ximury/vue.git
Cloning into 'vue'...
The authenticity of host '[ssh.github.com]:443 ([18.138.202.180]:443)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443,[18.138.202.180]:443' (RSA) to the list of known hosts.
git@ssh.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.

在这里插入图片描述

再次尝试后,就是这种情况:

$  git clone git@github.com:ximury/vue.git
Cloning into 'vue'...
git@ssh.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.

在这里插入图片描述

原因分析

在.ssh目录下生成一个github的SSH-Key时,自定义了文件名,如下:

$ ssh-keygen -t rsa -C 'xxxxx@qq.com' -f ~/.ssh/github_id_rsa

然后就将.pub文件复制粘贴到github网站上的SSH keys中。

之后直接进行clone代码,就出现了以上BUG。

问题解决

方式一

使用命令 ssh-keygen -t rsa 直接生成默认的rsa文件,之后在github上配置pub即可

方式二

生成rsa文件时自定义文件名,需要在 .ssh 目录下添加一个config文件,样例如下:

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

注意: 其中 Host 和 HostName 后填写 git 服务器的域名,IdentityFile 指定私钥的路径(只需要修改 IdentityFile )

测试成功与否

ssh -T git@github.com

如果成功:

Hi ***! You've successfully authenticated, but GitHub does not provide shell access.

在这里插入图片描述

如果测试结果失败:

Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'C:\\Users\\wyj/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "C:\\Users\\wyj/.ssh/id_rsa": bad permissions
git@github.com: Permission denied (publickey).

在这里插入图片描述

执行下面命令制定目标Git账号:

ssh-add -k id_rsa

id_rsa文件对应之前生成的rsa文件

之后再执行测试命令即可!

在这里插入图片描述

Last

最后,附上成功的截图:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九思梦鹿

喜欢,请记得点赞或赞赏哟

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值