在日常开发工作中,我们经常需要同时连接多个代码托管平台,例如个人开源项目使用 GitHub,国内项目或工作项目使用 Gitee(码云)。

默认情况下,SSH 会使用 ~/.ssh/id_rsa 或 ~/.ssh/id_ed25519 作为默认私钥。如果不同平台使用不同账号、不同邮箱,或者出于安全规范需要密钥隔离,就需要为每个平台单独生成独立的 SSH Key,并通过 ~/.ssh/config 进行自动路由分发。

1. 密钥目录结构

以 macOS 环境为例,所有 SSH 相关密钥和配置文件均存放在当前用户的 ~/.ssh 目录下:

1
2
3
4
5
6
7
/Users/ericxu/.ssh/
├── config # SSH 配置文件(负责路由分发)
├── id_ed25519_github # GitHub 私钥
├── id_ed25519_github.pub # GitHub 公钥
├── id_ed25519_gitee # Gitee 私钥
├── id_ed25519_gitee.pub # Gitee 公钥
└── known_hosts # 已知主机的公钥列表

2. 生成多平台专属 SSH Key

推荐使用更安全、性能更好的 ed25519 算法(如果平台或系统较旧,也可以换成 -t rsa -b 4096)。

打开终端,进入 ~/.ssh 目录分别生成两个平台的密钥对:

2.1 生成 GitHub 密钥对

1
2
cd ~/.ssh
ssh-keygen -t ed25519 -C "your_github_email@example.com" -f ~/.ssh/id_ed25519_github

执行后直接按回车跳过 passphrase(如需密码可自行设置),生成完成后会得到:

  • id_ed25519_github(私钥,妥善保管,不能泄漏)
  • id_ed25519_github.pub(公钥,用于上传到 GitHub)

2.2 生成 Gitee 密钥对

1
ssh-keygen -t ed25519 -C "your_gitee_email@example.com" -f ~/.ssh/id_ed25519_gitee

生成完成后同样会得到:

  • id_ed25519_gitee(私钥)
  • id_ed25519_gitee.pub(公钥)

3. 将公钥添加到对应平台

复制对应的公钥内容,并分别添加到对应平台的后台设置中。

3.1 复制公钥内容

1
2
3
# macOS 下可直接使用 pbcopy 复制到剪贴板
pbcopy < ~/.ssh/id_ed25519_github.pub
pbcopy < ~/.ssh/id_ed25519_gitee.pub

3.2 平台配置入口

  1. GitHub:

    • 登录 GitHub → 右上角头像 Settings → 侧边栏 SSH and GPG keys。
    • 点击 New SSH key,Title 自定义(如 MacBook-Pro-GitHub),Key 粘贴公钥内容,点击 Add SSH key。
  2. Gitee:

    • 登录 Gitee → 右上角头像 设置 → 侧边栏 安全设置 中的 SSH公钥。
    • 标题自定义(如 MacBook-Pro-Gitee),公钥处粘贴内容,点击 确定。

4. 配置 ~/.ssh/config 文件

创建或编辑 ~/.ssh/config 文件:

1
vim ~/.ssh/config

添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
# GitHub 平台
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github

# Gitee 平台
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_ed25519_gitee

参数说明

参数 说明
Host 别名或匹配的主机名,Git 克隆地址中 @ 后面的域名部分会匹配该名称
HostName 真实的服务器域名或 IP 地址(GitHub 为 github.com,Gitee 为 gitee.com)
User SSH 连接用户名,Git 托管平台统一为 git
IdentityFile 该 Host 对应的私钥本地绝对路径

权限提示:
macOS 和 Linux 对 SSH 相关文件权限要求较严,建议确保权限正确:

1
2
3
4
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519_*
chmod 644 ~/.ssh/id_ed25519_*.pub

5. 测试 SSH 连通性

配置完成后,分别测试与 GitHub 和 Gitee 的连通性:

5.1 测试 GitHub

1
ssh -T git@github.com

首次连接可能会提示是否信任该主机,输入 yes 并回车。如果输出如下内容,说明认证成功:

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

5.2 测试 Gitee

1
ssh -T git@gitee.com

如果输出如下欢迎语,说明 Gitee 认证成功:

1
Hi username! You've successfully authenticated, but GITEE.COM does not provide shell access.

6. 注意事项:多账号提交人信息管理

使用多平台时,经常会遇到不同平台需要展示不同的用户名和邮箱。

方式一:单独在仓库内设置(最稳妥)

在每个项目的仓库根目录下,单独配置该项目的作者信息,覆盖全局配置:

1
2
3
4
5
6
7
8
9
# 进入 GitHub 项目目录
cd ~/Workspace/my-github-repo
git config user.name "YourGitHubName"
git config user.email "your_github_email@example.com"

# 进入 Gitee 项目目录
cd ~/Workspace/my-gitee-repo
git config user.name "YourGiteeName"
git config user.email "your_gitee_email@example.com"

方式二:利用 Git 的 includeIf 按目录自动隔离

如果代码分目录存放(如 ~/Workspace/github/ 和 ~/Workspace/gitee/),可以在 ~/.gitconfig 中配置规则自动切换:

1
2
3
4
5
6
# ~/.gitconfig
[includeIf "gitdir:~/Workspace/github/"]
path = ~/.gitconfig-github

[includeIf "gitdir:~/Workspace/gitee/"]
path = ~/.gitconfig-gitee

分别在 ~/.gitconfig-github 和 ~/.gitconfig-gitee 里写入对应的 [user] 信息即可,无需每次手动设置。