目的:
一台电脑可以让github、gitee等账号同时存在,让不同账号配置不同的密钥
第一步:创建不同平台的SSH公钥
执行命令:
bash
ssh-keygen -t rsa -C "对应仓库邮箱地址" -f ~/.ssh/id_rsa.github
如果执行上面的命令,得到如下结果

那你可以使用这样的命令:
bash
ssh-keygen -t rsa -C "平台对应的邮箱地址"
注意,使用上面这条命令创建第2个ssh公钥时,因为第1个公钥的名字已经使用了默认的rsa,所以不要急于按回车键,需要输入公钥的别名:
创建完,目录如下:

一个是gitee平台,一个是github平台
查看生成的密钥
bash
cat ~/.ssh/id_rsa.pub
将密钥配置到对应的平台
以github平台为例:

注册密钥到ssh-agent
多账号必须配置 config
文件(文件地址:C:\Users\用户名\.ssh
,文件名:config
),需要这一步是因为ssh默认会去查找那个唯一的id_rsa秘钥,不会识别我们前文自定义生成的秘钥,所以需要将自定义的密钥注册到ssh-agent
注意:以下命令是在git.bash执行
1.注册密钥
bash
ssh-add ~/.ssh/id_rsa.别名
或者输入全路径:
bash
ssh-add C:\Users\Administrator\.ssh\id_rsa别名
注意:ssh-add出现Error connecting to agent: No such file or directory的解决方法
【以管理员身份运行】在 PowerShell 执行,依次执行如下命令:
bash
Set-Service ssh-agent -StartupType Manual
Start-Service ssh-agent

2、查看注册结果(将看到设置的两条记录)
bash
ssh-add -l
3、在ssh文件夹下创建config文件并进入编辑模式
bash
cd ssh #进入ssh文件夹
touch config #创建config文件
vim config #进入config文件
按下i键进入编辑模式
编辑如下内容(还是以Github和gitee为例):
bash
Host github.com
HostName github.com
User Shawn #别名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
Host gitee.com
HostName gitee.com
User ShawnWong #别名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
编辑完成后按esc并输入:wq退出config文件

5、分别测试连通性
bash
# 这里对应的是config文件中设置的HostName
ssh -T git@github.com


本地ssh目录:
