一文学会配置Github与Gitlab双账户

1,832 阅读1分钟

文章目录
0.具体思路
1.生成对应不同账号的秘钥
2.修改 config 配置文件
3.在 github 和 gitlab 上添加个人公钥
4.测试

关键词:Github GitLab 双账户 设置 同时起效


0. 具体思路

在 config 文件中设置不同的 Host ,然后对应使用不同的 .pub 公钥文件

1. 生成对应不同账号的秘钥

1.1 生成 github 秘钥

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "GithubAccount"

1.2 生成 gitlab 秘钥

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitlab -C "GitlabAccount"



2. 修改 config 配置文件

2.1 切换到 ~/.ssh 目录

$ cd ~/.ssh
$ ls
config id_rsa_github id_rsa_github.pub id_rsa_gitlab id_rsa_gitlab.pub known_hosts

2.2 修改配置文件

$ sudo vim config
# github
# host 与 hostname 需要相同
Host github.com
HostName github.com
# 你的github账号
User GithubAccount
# github对应的rsa秘钥文件
IdentityFile ~/.ssh/id_rsa_github

# gitlab   
# host 与 hostname 需要相同
Host gitlab.com
HostName gitlab.com
# 你的gitlab账号
User GitlabAccount
# gitlab对应的rsa秘钥文件
IdentityFile ~/.ssh/id_rsa_gitlab


3. 在 github 和 gitlab 上添加个人公钥

3.1 打开 Settings

在这里插入图片描述

3.2 新建 SSH Key

在这里插入图片描述

3.3 填写 title 和 key

将对应的 xx.pub 文件内容复制到 key 里面就行

比如,我要配置的是 github 账号

我就复制 ~/.ssh/id_rsa_github.pub 的内容到下图中的 Key 中去

在这里插入图片描述


4. 测试

4.1 github 测试

$ ssh -T git@github.com
Hi GithubAccount! You've successfully authenticated, but GitHub does not provide shell access.
# 出现上边这句,表示连接成功

4.2 gitlab 测试

$ ssh -T git@gitlab.com
Hi GitlabAccount!