window 平台 git 配置多个账号的 ssh-key

window 平台 git 配置多个账号的 ssh-key

配置单个账号的 ssh-key

创建一个 gitlab 的 ssh-key

1
2
ssh-keygen -t rsa -C "邮箱账号"
# 然后一路按回车键

然后会在: ~/.ssh/ 目录下看到生成的秘钥文件, 密钥文件一般存放在: C:Usersusername.ssh 目录下

1
2
# 查看秘钥
cat ~/.ssh/id_rsa

把秘钥在 gitlab 上添加好了后, 验证是否配置成功

1
2
$ ssh -T git@gitlab.com
Hi ****! You've successfully authenticated, but Gitlab does not provide shell access.
配置多个账号的 ssh-key

比如说在上一步的基础上, 再创建一个 github 的 ssh-key

第一步, 生成秘钥

1
2
3
4
ssh-keygen -t rsa -C "邮箱账号"
# 回车后会要求输入秘钥文件的名字, 为了作区分, 可以自己定义一个名字
Enter file in which to save the key (/c/Users/Gato/.ssh/id_rsa): github_private_id_rsa
# 然后一路按回车

创建配置文件(命令行操作也可换成手动创建, 秘钥存放位置一般在 C:Usersusername.ssh 文件夹下)

1
2
3
4
# 进入到秘钥文件夹下
cd ~/.ssh/
# 创建配置文件, 进入到 vim 编辑器
vim config

config 文件配置(IdentityFile 改成自己密钥文件的实际位置):

1
2
3
4
5
6
7
8
9
10
11
12
# ~/.ssh/config 配置多个git的ssh-key
# 第一个默认的SSH Key
Host gitlab.com
HostName gitlab.com
IdentityFile C:Usersusername.sshid_rsa
PreferredAuthentications publickey

# 第二个SSH Key
Host github.com
HostName github.com
IdentityFile C:Usersusername.sshgitlab_private_id_rsa
PreferredAuthentications publickey

验证是否配置成功

1
2
$ ssh -T git@github.com
Hi ****! You've successfully authenticated, but GitHub does not provide shell access.

window 平台 git 配置多个账号的 ssh-key
http://example.com/2023/08/02/window-平台-git-配置多个账号的-ssh-key/
作者
lwp
发布于
2023年8月2日
许可协议