新建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**
具体演示过程如下图:
![](https://img.haomeiwen.com/i1744308/e1d69a6920becfbe.jpg)
![](https://img.haomeiwen.com/i1744308/ab05b61ce531dafa.jpg)
密钥添加到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
![](https://img.haomeiwen.com/i1744308/72f706696f3bb2c5.jpg)
修改config文件
在~/.ssh目录下找到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.
![](https://img.haomeiwen.com/i1744308/a706a2c595f8d3f0.jpg)
打开新生成的~/.ssh/id_rsa_user2.pub文件,将里面的内容添加到GitHub后台。
第一个github帐号下的SSH Key中也要记得添加。
![](https://img.haomeiwen.com/i1744308/4a4ff7723938a1a5.jpg)
![](https://img.haomeiwen.com/i1744308/ea45208a51aeceb9.jpg)
测试
$ ssh -T git@github.com
Hi 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.
![](https://img.haomeiwen.com/i1744308/82cc480e5e7048d2.jpg)
6
应用
测试成功,尝试克隆1047353200账号下的远程仓库。如下:
$ git clone git@github2:1047353200/git-demo.git
![](https://img.haomeiwen.com/i1744308/5ec21a7ca30751f9.jpg)
![](https://img.haomeiwen.com/i1744308/ee6515fd339e8522.jpg)
网友评论