返回列表

转载:多github帐号的SSH key切换

默认分类 2015/01/14 03:19

多github帐号的SSH key切换

我有两个github帐号,一个是个人所用,一个是为公司项目所用。如果是单用户(single-user),很方便,默认拿id_rsa与你的github服务器的公钥对比;如果是多用户(multi-user)如user1,user2,那么就不能用在user2的身上了,这个时候就要配置一下了:

1、新建user2的SSH Key

#新建SSH key:
$ cd ~/.ssh     # 切换到C:\Users\Administrator\.ssh
ssh-keygen -t rsa -C "mywork@email.com"  # 新建工作的SSH key
# 设置名称为id_rsa_work
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_work

2、新密钥添加到SSH agent中

因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:

ssh-add ~/.ssh/id_rsa_work

如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:

ssh-agent bash 或 ssh-ahent sh
ssh-add ~/.ssh/id_rsa_work

3、修改config文件
在~/.ssh目录下找到config文件,如果没有就创建:

touch config        # 创建config

然后修改如下: 我的config配置如下:

Host github.com_camps
 HostName github.com
 User git
 IdentityFile C:/Users/username/.ssh/id_rsa

Host github.com_mvc
 HostName github.com
 User git
 IdentityFile C:/Users/username/.ssh/id_rsa_femvc

如果存在的话,其实就是往这个config中添加一个Host:

#建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github.com_mvc
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_femvc

其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github.com_mvc,那么原地址是:

git@github.com:funpeng/Mywork.git

替换后应该是:

git@github.com_mvc:funpeng/Mywork.git

4、打开新生成的 ~/.ssh/id_rsa_femvc.pub 文件,将里面的内容添加到GitHub后台。

可不要忘了添加到你的另一个github帐号下的SSH Key中。

5、测试:

Administrator@FANGPENG /e/work
$ ssh -T git@github.com_camps
Hi fecamps! You've successfully authenticated, but GitHub does not provide shel
l access.

Administrator@FANGPENG /e/work
$ ssh -T git@github.com_mvc
Hi femvc! You've successfully authenticated, but GitHub does not provide shell access.

6、应用

测试成功,那么我尝试在我的work目录下克隆我账号下的远程仓库。如下:

username@WIN-8BNSQDEB70D:/C/baiduyun/fecampus $ git clone git@github.com_camps:fecamps/nodejs-text-db.git
Cloning into 'nodejs-text-db'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 9 (delta 1), reused 5 (delta 0)
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.

username@WIN-8BNSQDEB70D:/C/baiduyun/fecampus/nodejs-text-db [master]$ git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To git@github.com_camps:fecamps/nodejs-text-db.git
   bd4be9d..f886fa8  master -> master


username@WIN-8BNSQDEB70D:/C/baiduyun/fecampus $ git clone git@github.com_mvc:fe
mvc/HTMLParser.git
Cloning into 'HTMLParser'...
remote: Counting objects: 21, done.
remote: Total 21 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (21/21), 6.56 KiB | 0 bytes/s, done.
Resolving deltas: 100% (8/8), done.
Checking connectivity... done.

username@WIN-8BNSQDEB70D:/C/baiduyun/fecampus/HTMLParser [master]$ git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 226 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 1 (delta 1)
To git@github.com_mvc:femvc/HTMLParser.git
   e7149b6..ffca602  master -> master

克隆修改上传成功,大功告成了!