拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 同一台计算机生成多份ssh私钥和公钥,映射多个GitHub账号

同一台计算机生成多份ssh私钥和公钥,映射多个GitHub账号

白鹭 - 2022-03-07 1948 0 0

问题说明

当我们使用 Git 进行代码版本控制时,经常出现一台计算机需要连接多个Git 账号的情况,此时需要在一台计算机上生成多份 ssh 私钥和密钥,同时映射多个 Git 账号;这里我们需要同时连接 GitHub,码云,两个账号;

配置

1.生成 ssh 密钥档案;

ssh-keygen -t rsa -C "Zbc521.gitee.com" -f ~/.ssh/id_rsa_gitee
ssh-keygen -t rsa -C "Zbc521.github.com" -f ~/.ssh/id_rsa_github

图示:

2.通过ssh-add添加密钥至ssh-agent;

ssh-add ~/.ssh/id_rsa_gitee
ssh-add ~/.ssh/id_rsa_github

3.添加 config 组态档分别映射不同的 Git 账号;

进入~/.ssh目录,新建config档案,添加如下内容:

# GitHub 公钥
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

# Gitee 码云 公钥
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee

4.向你的 Git 账户中添加 SSH Key 认证,这里以 GitHub 账号为例 ;

  • 登录 GitHub 账号,打开 /settings/SSH and GPG keys,新建 SSH key;
  • 将 本地生成的 id_rsa_github.pub 公钥档案内容,拷贝到 GitHub 中;

5.测验连接配置是否成功;

输入命令:

ssh -T git@github.com

出现如下信息,表示成功:

Hi Zbc521! You've successfully authenticated, but GitHub does not provide shell access.
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *