github/gitlab同时管理多个ssh key

生成 ssh key

1¥ cd \.\Users\[username]\.ssh 
2¥ ssh ssh-keygen -t rsa -C "youremail@yourcompany.com"

同理生成个人邮箱 ssh key (for github)

1~\.ssh 
2¥ ssh-keygen -t rsa -C "youremail@personal.com"
3
4Generating public/private rsa key pair.
5Enter file in which to save the key (C:\Users\[user]/.ssh/id_rsa): id_rsa_test
6Enter passphrase (empty for no passphrase):
7Enter same passphrase again:
8Your identification has been saved in id_rsa_test.
9Your public key has been saved in id_rsa_test.pub.
10The key fingerprint is:
11SHA256:C155hCQy9TQZme0JcihWJWwKU7EBvgfqywcsD3Glmas youremail@personal.com
12The key's randomart image is:
13+---[RSA 3072]----+
14|  .o*==o**       |
15| .o +==**o.      |
16|  oO.+ ooo..     |
17|..=o.    oo      |
18|oo... . S .      |
19|+o.. . o o       |
20|.=.   . .        |
21|E o.             |
22| o.              |
23+----[SHA256]-----+
24
25 ~\.ssh 
26¥ ls
27config  id_rsa  id_rsa.pub  id_rsa_github  id_rsa_github.pub  known_hosts  id_rsa_test  id_rsa_test.pub
28

生成 id_rsa_test, id_rsa_test.pub 文件, 首次创建可以不输入名称, 默认为 id_rsa, 之后再创建不输入名称会 覆盖 id_rsa

添加私钥

1¥ ssh-add ~/.ssh/id_rsa
2¥ ssh-add ~/.ssh/id_rsa_github

配置文件

在 ~/.ssh 目录下新建一个 config 文件

1¥ touch config

输入

1# gitlab
2Host gitlab.company.com
3    HostName gitlab.company.com
4    Port 50022
5    PreferredAuthentications publickey
6    IdentityFile ~/.ssh/id_rsa
7# github
8Host github.com
9    HostName github.com
10    PreferredAuthentications publickey
11    IdentityFile ~/.ssh/id_rsa_github

在 github / gitlab 上添加 ssh key

查看文件内容, 将文本拷贝到 github ssh

1¥ cat id_rsa_github.pub

显示如下, 即为成功

1~\.ssh 
2¥ ssh -T git@github.com
3Hi Vsion! You've successfully authenticated, but GitHub does not provide shell access.
4
5 ~\.ssh 
6¥ ssh -T git@gitlabcode.21vianet.com
7Welcome to GitLab, @xxx!