添加多个SSH key之后验证失败

git账户生成了多个ssh key之后总是验证不过,一直用的老的ssh key

Initialized empty Git repository inXXX
The authenticity of host '[gerrit.ericsson.se]:29418 ([142.133.164.50]:29418)' can't be established.
RSA key fingerprint is 0d:31:2e:57:bf:29:36:02:04:81:12:85:fd:b8:87:3b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[gerrit.ericsson.se]:29418,[142.133.164.50]:29418' (RSA) to the list of known hosts.
Agent admitted failure to sign using the key.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

 

解决方法:

ssh-add ~/.ssh/id_rsa

之后就可以了。

 

有一篇文章可以参考:

github使用SSH与客户端连接。如果是单用户(first),生成密钥对后,将公钥保存至github,每次连接时SSH客户端发送本地私钥(默认~/.ssh/id_rsa)到服务端验证。单用户情况下,连接的服务器上保存的公钥和发送的私钥自然是配对的。但是如果是多用户(first,second),我们在连接到second的帐号时,second保存的是自己的公钥,但是SSH客户端依然发送默认私钥,即first的私钥,那么这个验证自然无法通过。不过,要实现多帐号下的SSH key切换在客户端做一些配置即可。

首先cd到~/.ssh 使用 ssh-keygen -t -rsa -C ‘second@mail.com’ 生成新的SSH key:id_rsa_second,生成完后将新的SSH public key添加到github。

ssh-keygen -t rsa -C 'second@mail.com'

默认SSH只会读取id_rsa,所以为了让SSH识别新的私钥,需要将其添加到SSH agent

ssh-add ~/.ssh/id_rsa_second

该命令如果报错:Could not open a connection to your authentication agent.无法连接到ssh agent,可执行ssh-agent bash命令后再执行ssh-add命令。

完成以上步骤后在~/.ssh目录创建config文件,该文件用于配置私钥对应的服务器。内容如下:

# Default github user(first@mail.com)
Host github.com
 HostName github.com
 User git
 IdentityFile C:/Users/username/.ssh/id_rsa

# second user(second@mail.com)
Host github-second
 HostName github.com
 User git
 IdentityFile C:/Users/username/.ssh/id_rsa_second

Host随意即可,方便自己记忆,后续在添加remote是还需要用到。

配置完成后,在连接非默认帐号的github仓库时,远程库的地址要对应地做一些修改,比如现在添加second帐号下的一个仓库test,则需要这样添加:

git remote add test git@github-second:second/test.git #并非原来的git@github.com:second/test.git

这样每次连接都会使用id_rsa_second与服务器进行连接。至此,大功告成!

注意:github根据配置文件的user.email来获取github帐号显示author信息,所以对于多帐号用户一定要记得将user.email改为相应的email(second@mail.com)。

参考github帮助文档:


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

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

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

打赏作者

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

抵扣说明:

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

余额充值