git与SSH公私key

[TOC]

需求:

我有不同平台github,hf.co账户,其中hf.co有两个账户a,b。。。我想尽可能清晰较少文件地管理所有私钥公钥。应该先后如何生成公私密钥对。。给出最终的.ssh目录结构,以及内部文件对应的内容示例?

我之前的做法是,
各个平台的首个账户,即GitHub与HF.co账户a用的都是同一个密钥对。公私密钥生成时选用默认命名(该命名在不影响鉴权的前提下能否重命名,如何做)
若相同平台上存在第一个账户,链接第二个账户时,才新建新的密钥对并做第二个账户名标记。

SSH密钥管理方案

密钥生成策略

1. 首先生成默认密钥对(用于主要平台的首个账户)

1
2
ssh-keygen -t ed25519 -C "[email protected]"
# 使用默认路径和命名:~/.ssh/id_ed25519 和 ~/.ssh/id_ed25519.pub

2. 为HF.co的第二个账户b生成专用密钥对

1
ssh-keygen -t ed25519 -f ~/.ssh/hf/id_ed25519_hf_b -C "[email protected]"

最终.ssh目录结构

1
2
3
4
5
6
7
~/.ssh/
├── config # SSH配置文件
├── id_ed25519 # 默认私钥(用于GitHub和HF.co账户a)
├── id_ed25519.pub # 默认公钥
└── hf
├── id_ed25519_hf_b # HF.co账户b的私钥
└── id_ed25519_hf_b.pub # HF.co账户b的公钥

配置文件内容 (~/.ssh/config)

ssh-config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# GitHub - 使用默认密钥
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes

# HF.co 账户a - 使用默认密钥
Host hf.co
HostName hf.co
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes

# HF.co 账户b - 使用专用密钥
Host hf-b.co
HostName hf.co
User git
IdentityFile ~/.ssh/hf/id_ed25519_hf_b
IdentitiesOnly yes

密钥重命名方法

如果您想重命名现有的密钥文件(不影响鉴权):

  1. 重命名文件:

    1
    2
    mv ~/.ssh/id_ed25519 ~/.ssh/id_ed25519_main
    mv ~/.ssh/id_ed25519.pub ~/.ssh/id_ed25519_main.pub
  2. 更新SSH配置:

    ssh-config

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_main

    Host hf.co
    HostName hf.co
    User git
    IdentityFile ~/.ssh/id_ed25519_main

    Host hf_b.co
    HostName hf.co
    User git
    IdentityFile ~/.ssh/id_ed25519_hf_b
  3. 更新SSH代理(如果使用):

    1
    2
    3
    4
    5
    6
    ssh-add -D  # 清除所有密钥

    # 重启 SSH 代理
    eval "$(ssh-agent -s)"

    ssh-add ~/.ssh/id_ed25519_main # 添加重命名后的密钥

使用说明

  1. GitHub仓库:使用标准URL [email protected]:username/repo.git
  2. HF.co账户a:使用标准URL [email protected]:spaces/username/repo
  3. HF.co账户b:使用特殊URL [email protected]:spaces/username/repo

公钥分发

将公钥内容添加到相应平台:

  • GitHub: 将 ~/.ssh/id_ed25519.pub 内容添加到GitHub账户
  • HF.co账户a: 将 ~/.ssh/id_ed25519.pub 内容添加到账户a
  • HF.co账户b: 将 ~/.ssh/id_ed25519_hf_b.pub 内容添加到账户b

测试连接

1
2
3
4
5
6
7
8
# 测试GitHub连接
ssh -T [email protected]

# 测试HF.co账户a连接
ssh -T [email protected]

# 测试HF.co账户b连接[注意ssh-config中的Host别名为`hf_b.co`]
ssh -T git@hf_b.co

这种配置方式既清晰又简洁,使用最少的文件管理所有平台的SSH密钥,同时通过SSH配置文件实现了智能路由。

拓展:

公私密钥的2种编码

1
2
ssh-keygen -t rsa -b 4096 -f ~/.ssh/github -C "[email protected]"
ssh-keygen -t ed25519 -f ~/.ssh/github -C "[email protected]"

1. RSA 4096 位密钥

  • 算法: RSA (Rivest-Shamir-Adleman)
  • 密钥长度: 4096 位

2. Ed25519 密钥

  • 算法: Ed25519 (基于椭圆曲线密码学)
  • 密钥长度: 256 位(相当于约 3000 位的 RSA 安全性)

-C 参数的作用和影响

-C 参数的含义

  • -C 参数用于添加注释,通常包含邮箱或标识信息
  • 这个注释会出现在公钥的末尾,仅用于标识目的
  • 不影响密钥的加密功能或认证过程

使用相同 -C 值的影响


SSH 配置与密钥管理详解

1. SSH 配置片段分析

ssh-config

1
2
3
4
5
Host github.com
CheckHostIP no
HostName ssh.github.com
User git
Port 443

这个配置的含义:

  • 目的:通过 HTTPS 端口(443)建立 SSH 连接,绕过某些网络限制
  • 具体设置
    • HostName ssh.github.com:实际连接的主机名
    • Port 443:使用 HTTPS 端口而非默认的 SSH 端口 22
    • User git:使用 git 用户连接
    • CheckHostIP no:不检查主机 IP 是否与 known_hosts 中的记录匹配

是否正确?

完全正确。这是 GitHub 官方推荐的备用连接方式,特别适用于:

  • 公司或学校网络限制 SSH 端口(22)的情况
  • 防火墙阻止了标准 SSH 连接的环境

2. ssh-add 命令详解

命令作用:

1
2
ssh-add -D  # 从 SSH 代理中移除所有已缓存的密钥
ssh-add ~/.ssh/id_ed25519_main # 将指定密钥添加到 SSH 代理

使用场景:

  1. 密钥重命名后:更新 SSH 代理中的密钥路径
  2. 切换身份时:清除旧密钥,添加新密钥
  3. 安全考虑:会话结束后移除缓存的密钥
  4. 调试问题:重新加载密钥以解决连接问题

工作流程示例:

1
2
3
4
5
6
7
8
9
10
11
# 1. 查看当前代理中的密钥
ssh-add -l

# 2. 清除所有密钥
ssh-add -D

# 3. 添加新密钥(需要密码时输入)
ssh-add ~/.ssh/id_ed25519_main

# 4. 验证密钥已添加
ssh-add -l

3. known_hosts 文件详解

是什么?

~/.ssh/known_hosts 是一个存储已知 SSH 主机密钥的文件。

作用:

  1. 安全验证:防止中间人攻击(Man-in-the-Middle)
  2. 主机身份验证:确保连接的是真正的主机而非恶意服务器
  3. 自动信任:首次连接后记录主机密钥,后续连接自动验证

文件内容示例:

1
2
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmd...
hf.co ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...

常见操作:

1
2
3
4
5
6
7
8
# 查看 known_hosts 内容
cat ~/.ssh/known_hosts

# 删除特定主机的记录(当主机密钥变更时)
ssh-keygen -R github.com

# 手动添加主机密钥(不推荐,有安全风险)
ssh-keyscan github.com >> ~/.ssh/known_hosts

重要提示:

  • 首次连接时会出现确认主机密钥的提示:

    1
    2
    3
    The authenticity of host 'github.com (IP_ADDRESS)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no/[fingerprint])?
  • 输入 yes 后,该主机的密钥会被添加到 known_hosts

  • 如果主机密钥意外变更,连接会被拒绝(安全保护机制)

总结

  1. GitHub 的 443 端口配置是有效的备用连接方案
  2. ssh-add 用于管理 SSH 代理中的密钥缓存
  3. known_hosts 是重要的安全机制,确保连接的主机身份真实

这样的配置和管理方式既保证了连接的灵活性,又确保了安全性。