手把手教你同时使用多github帐号的SSH key

2025-05-29 17:08:36

1、新建user1、user2的SSH Key#新建user1 SSH key:$ cd ~/.ssh # 切换到C:\Users\Administrator\.ssh#ssh-keygen -t rsa -C "user1@email.com" 一路回车即可;# 新建user2的SSH key#ssh-keygen -t rsa -C "user2@email.com"# 设置名称为Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): /c/Users/Administrator/.ssh/id_rsa_user2具体演示过程如下图:

手把手教你同时使用多github帐号的SSH key
手把手教你同时使用多github帐号的SSH key

2、密钥添加到SSH agent中因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:ssh-add ~/.ssh/id_rsa_user2如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:ssh-agent bash ssh-add ~/.ssh/id_rsa_user2

手把手教你同时使用多github帐号的SSH key

3、修改config文件在~/.s衡痕贤伎sh目录下找到config文件,如果没有就创建:touch config 然后修改我的config配置剧安颌儿如下:如果存在的话,其实就是往这个config中添加一个Host:#建一个github别名,新建的帐号使用这个别名做克隆和更新其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github2,那么原地址 是:git@github.com:test/Mywork.git,替换后应该是:github2:test/Mywork.git.

手把手教你同时使用多github帐号的SSH key

4、打开新生成的~/.ssh/id_rsa_user2.pub文件,将里面的内容添加到GitHub后台。第一个github帐号下的SSH Key中也要记得添加。

手把手教你同时使用多github帐号的SSH key
手把手教你同时使用多github帐号的SSH key

5、测试$ ssh -T git@github.comHi chenlianjiang! You've successfully authenticated, but GitHub does not provide shell access.$ ssh -T git@github2 Hi 1047353200! You've successfully authenticated, but GitHub does not provide shell access.

手把手教你同时使用多github帐号的SSH key

6、应用测试成功,尝试克隆1047353200账号下的远程仓库。如下:$ git clone git@github2:1047353200/git-demo.git

手把手教你同时使用多github帐号的SSH key
手把手教你同时使用多github帐号的SSH key
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢