文章

玩转 SSH Config:多账号 Git 管理与连接优化指南

玩转 SSH Config:多账号 Git 管理与连接优化指南

在日常开发中,我们经常会遇到需要同时管理多个 Git 账号(如个人 GitHub、公司 GitLab、Gitee 等)的情况,或者需要连接多台远程服务器。如果不进行配置,频繁切换密钥或输入冗长的连接命令会非常痛苦。

本文将详细讲解如何通过 SSH Config 文件优雅地解决这些问题。

1. 为什么需要 SSH Config?

默认情况下,SSH 会使用 ~/.ssh/id_rsa 作为默认密钥。但当你拥有多个账号时(例如两个 GitHub 账号),你需要为每个账号使用不同的密钥对。SSH Config 文件 (~/.ssh/config) 可以让你:

  • 为不同的服务器指定不同的密钥 (IdentityFile)。
  • 为服务器设置别名,简化连接命令(如 ssh myserver 代替 ssh root@192.168.1.100 -p 2222)。
  • 保持连接活跃,防止断连。

2. 生成更安全的密钥 (Ed25519)

虽然 RSA 是老牌标准,但 Ed25519 算法更安全、更快速且生成的密钥更短。推荐优先使用 Ed25519。

生成命令

在终端(PowerShell 或 Bash)中执行:

1
2
3
4
5
6
7
# 生成默认密钥 (示例)
ssh-keygen -t ed25519 -C "your_email@example.com"

# 为特定平台生成密钥 (推荐指定文件名)
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github -C "github_email"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_gitee -C "gitee_email"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work_email"

注意:生成后,记得将 .pub 文件(公钥)的内容添加到对应平台(GitHub/Gitee)的 SSH Key 设置中。

3. 配置文件详解

SSH 配置文件通常位于 ~/.ssh/config (Windows 下为 C:\Users\用户名\.ssh\config)。

基础配置模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 全局默认配置(可选)
Host *
    # 每60秒发送心跳包,防止防火墙切断空闲连接
    ServerAliveInterval 60
    ServerAliveCountMax 5

# GitHub 账号
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    # 强制只使用指定的密钥,避免多密钥时的认证尝试失败
    IdentitiesOnly yes

# Gitee 账号
Host gitee.com
    HostName gitee.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitee
    IdentitiesOnly yes

关键参数解析

  • Host: 别名。你在 git clonessh 命令中使用的名字。
  • HostName: 真实的服务器地址(域名或 IP)。
  • User: 登录用户名。对于 GitHub/Gitee/GitLab,必须是 git
  • IdentityFile: 私钥文件的绝对路径。
  • IdentitiesOnly yes: (重要) 告诉 SSH 客户端只使用配置文件中指定的密钥进行尝试。如果不加这一行,SSH Agent 可能会尝试所有已加载的密钥,导致因尝试次数过多被服务器拒绝 (“Too many authentication failures”)。
  • ServerAliveInterval 60: 对于远程开发(如 VS Code Remote SSH)非常有用,防止因长时间无数据传输而断开连接。

4. 实战:配置多个 GitHub 账号

假设你有一个个人账号和一个工作账号,都需要连接 GitHub。

步骤 1: 生成两对密钥

1
2
ssh-keygen -t ed25519 -f ~/.ssh/id_github_personal -C "personal@email.com"
ssh-keygen -t ed25519 -f ~/.ssh/id_github_work -C "work@email.com"

步骤 2: 编写 Config

在 config 文件中定义两个 Host,其中一个使用别名(如 github-work)。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 个人账号 (默认)
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_personal
    IdentitiesOnly yes

# 工作账号 (使用别名)
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_work
    IdentitiesOnly yes

步骤 3: 使用别名克隆/操作

个人项目(使用默认 github.com):

1
git clone git@github.com:personal-user/repo.git

工作项目(使用别名 github-work):

关键点:将 github.com 替换为 config 中的 Host 别名。

1
2
3
4
5
# 克隆时
git clone git@github-work:work-org/project.git

# 如果项目已经存在,修改远程地址
git remote set-url origin git@github-work:work-org/project.git

5. 常用技巧与排错

测试连接

配置完成后,使用 -T 参数测试连接情况:

1
2
3
4
5
ssh -T git@github.com
# 输出: Hi username! You've successfully authenticated...

ssh -T git@github-work
# 输出: Hi work-username! You've successfully authenticated...

权限问题 (Linux/Mac)

在 Linux 或 macOS 上,SSH 对文件权限非常敏感:

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

Windows 10/11 通常会自动处理好权限,但如果遇到 WARNING: UNPROTECTED PRIVATE KEY FILE!,请在文件属性 -> 安全中,移除除当前用户外的所有用户权限。

总结

通过 SSH Config,我们可以:

  1. 隔离环境:通过 IdentityFile 完美隔离工作与个人密钥。
  2. 提升稳定:通过 ServerAliveInterval 解决连接超时断开问题。
  3. 解决冲突:通过 IdentitiesOnly 避免密钥尝试过多的错误。

配置一次,受益永久。

本文由作者按照 CC BY 4.0 进行授权