报错:git@ssh.github.com: Permission denied (publickey),记录了本地git在使用ssh连接github等远程仓库时出现的Permission denied报错,原因是由于本地.ssh
文件夹下有多个key导致,需要配置config文件进行甄别
问题记录
在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
最后,附上成功的截图: