同一台电脑关于多个SSH KEY管理

比如有 aaa,bbb,ccc 三个帐号需要生成不同的 PUBLIC KEY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_aaa.
Your public key has been saved in /root/.ssh/id_rsa_aaa.pub.
The key fingerprint is:
9b:92:f6:1f:d2:72:bd:72:19:45:42:5f:e4:65:33:64 root@AY140122145815620396Z
The key's randomart image is:
+--[ RSA 2048]----+
| .. .E=|
| ..o++|
| o. .|
| . |
| S . |
| . + .. |
| + = + .o |
| . o +..o. |
| ...o. |
+-----------------+

我们在 /root/.ssh/ 目录下创建 id_rsa_aaa 私钥 和 id_rsa_aaa.pub 公钥.

1
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa  #设置路径,如果不设置默认生成 id_rsa  和  id_rsa.pub

按照上面的步骤逐个生成 bbb 和 ccc 对应的公钥和私钥

查看系统 ssh-key 代理,执行如下命令

1
2
$ ssh-add -l
Could not open a connection to your authentication agent.

如果发现上面的提示,说明系统代理里没有任何 key,执行如下操作

1
exec ssh-agent bash

如果系统已经有 ssh-key 代理 ,执行下面的命令可以删除

1
ssh-add -D

把 .ssh 目录下的 3 个私钥添加的 ssh-agent

1
2
3
ssh-add ~/.ssh/id_rsa_aaa
ssh-add ~/.ssh/id_rsa_bbb
ssh-add ~/.ssh/id_rsa_ccc

依次执行上面三条 shell 把三个私钥添加到 ssh-key 代理里面。

打开 github 或者 开源中国 ssh 管理页面把 对应的公钥提交保存到代码管理服务器 (.pub 结尾)

在 .ssh 目录创建 config 配置文件

1
nano ~/.ssh/config

输入如下配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#aaa  (github 配置)
Host aaa
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_aaa

#bbb (开源中国 配置)
Host bbb
HostName git.oschina.net
User git
IdentityFile ~/.ssh/id_rsa_bbb

#ccc
........

记住上面一步 Host 里设置的别名,开始克隆项目,以开源中国为例

1
git clone git@git.oschina.net:userName/projectName.git

到此为止多个 ssh-key 已经可以同时使用.