生成本地密钥

假如我们有2个git账号,分别来自githubcoding,进入目录C:\Users\Administrator\.ssh

首先生成github密钥,注意区分密钥文件名

1
ssh-keygen -t rsa -C "email@gmail.com" -f id_rsa_github

生成coding密钥

1
ssh-keygen -t rsa -C "email@qq.com" -f id_rsa_coding

生成结果如下所示,以pub结尾的就是公钥文件

1
2
3
4
-rw-------+ 1 Administrator None 1679 Dec 19 12:17 id_rsa_coding
-rw-r--r--+ 1 Administrator None 397 Dec 19 12:17 id_rsa_coding.pub
-rw-------+ 1 Administrator None 1679 Dec 17 10:29 id_rsa_github
-rw-r--r--+ 1 Administrator None 399 Dec 17 10:29 id_rsa_github.pub


添加私钥

1
2
ssh-add id_rsa_github
ssh-add id_rsa_coding

如果返回could not open a connection to your authentication agent, 则先执行以下命令

1
ssh-agent bash


创建配置文件

在.ssh目录下新建配置文件config

1
touch config

并添加以下配置内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
# *配置github.com*
Host github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User git

# 配置git.coding.net
Host coding.net
HostName coding.net
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa_coding
PreferredAuthentications publickey
User git


上传公钥

以github为例, 进入Settings -> SSH and GPG keys, 点击”New SSH key”, 并将id_rsa_github.pub文件里的内容复制到Key中
图片


测试结果

1
ssh -T git@github.com

如果返回以下结果表示配置成功

1
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

Comments

⬆︎TOP