0%

Git生成多个ssh key

当使用了多个不同的git版本控制系统,分别有不同账号时,如一个在github上面有项目,一个在coding或者开源中国上面的有项目时,如果2者的邮箱不同时,就会涉及一个问题,生成的ssh key 会相互覆盖,必然有一个无法使用;

下面记录下解决方法:

生成ssh-key


1
2
//创建github的ssh key
ssh-keygen -t rsa -C "your_email@example.com" -f /c/user/username/.ssh/github_rsa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

example:
//我的秘钥保存路径C:\Users\MMA\.ssh\test,邮箱使用your_email@example.com
ssh-keygen -t rsa -C "your_email@example.com" -f /c/Users/MMA/.ssh/test/github_rsa
//运行之后弹出
$ ssh-keygen -t rsa -C "your_email@example.com" -f /c/User/MMA/.ssh/test/test/github_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):

回车默认即可,出现下面提示,则创建成功

Your identification has been saved in /c/Users/MMA/.ssh/test/github_rsa.
Your public key has been saved in /c/Users/MMA/.ssh/test/github_rsa.pub.
The key fingerprint is:
SHA256:AjEAqCT5VeTZsdHpklyMIVQQWbBVtAufE/P/GzkEw9I your_email@example.com
The key's randomart image is:
+---[RSA 2048]----+
|oo..o+OBBBo. |
|+. .+.*.+=.o |
|+. .. +.+++. E |
|. . . +o.*. o |
| . S.= . . |
| . . .. .|
| .+ |
| .o|
| .o|
+----[SHA256]-----+


查看本地目录,GitHub ssh key生成成功
github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//创建coding的ssh key
ssh-keygen -t rsa -C "my_email@example.com" -f /c/Users/MMA/.ssh/test/coding_rsa

和创建github的相似
$ ssh-keygen -t rsa -C "my_email@example.com" -f /c/Users/MMA/.ssh/test/coding_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/MMA/.ssh/test/coding_rsa.
Your public key has been saved in /c/Users/MMA/.ssh/test/coding_rsa.pub.
The key fingerprint is:
SHA256:aqcMT/i9ZgXB6aCHus1zdf5JQlrLBYfUnnIBNJ37WIQ my_email@example.com
The key's randomart image is:
+---[RSA 2048]----+
| . o=+ o |
| . +. oE . |
| o o .o..= |
| o . o .o= . |
| . . S.oo.+ |
| . . ..=oo. . |
| +o +.o++ . |
| . +B.+o .o . |
| o=oo. .o |
+----[SHA256]-----+

github+coding

添加私钥到SSH agent中


1
2
ssh-add /c/Users/MMA/.ssh/test/coding_rsa
ssh-add/c/Users/MMA/.ssh/test/github_rsa

如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:ssh-agent bash

重新添加即可

1
2
3
4
5
6
7
8
9
10
11
12
13
MMA@MMA-PC MINGW64 ~/Desktop
$ ssh-add /c/Users/MMA/.ssh/test/github_rsa
Identity added: /c/Users/MMA/.ssh/test/github_rsa (/c/Users/MMA/.ssh/test/github_rsa)

MMA@MMA-PC MINGW64 ~/Desktop
$ ssh-add /c/Users/MMA/.ssh/test/coding_rsa
Identity added: /c/Users/MMA/.ssh/test/coding_rsa (/c/Users/MMA/.ssh/test/coding_rsa)

MMA@MMA-PC MINGW64 ~/Desktop
$ ssh-add -l
2048 SHA256:aqcMT/i9ZgXB6aCHus1zdf5JQlrLBYfUnnIBNJ37WIQ /c/Users/MMA/.ssh/test/coding_rsa (RSA)
2048 SHA256:AjEAqCT5VeTZsdHpklyMIVQQWbBVtAufE/P/GzkEw9I /c/Users/MMA/.ssh/test/github_rsa (RSA)

// 可以通过 ssh-add -l 来确私钥列表

ssh-add -l

// 可以通过 ssh-add -D 来清空私钥列表

ssh-add -D

修改config文件


在/c/Users/MMA/.ssh/test 目录下新建一个config文件

1
2
3
4
5
6
7
8
9
# coding
Host git.coding.net
PreferredAuthentications publickey
IdentityFile /c/Users/MMA/.ssh/test/coding_rsa
# github
Host github.com
PreferredAuthentications publickey
IdentityFile /c/Users/MMA/.ssh/test/github_rsa

添加公钥到git平台


[coding教程]https://coding.net/help/doc/git/ssh-key.html)

[github教程]https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account)