概览
SSH Key 是基于公钥密码体制的身份验证方式,相比密码认证更安全、便捷。在 Git、服务器连接等场景中广泛使用。
一、SSH Key 生成
1.1 基本生成步骤
生成标准的 RSA 密钥
1
| ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
|
参数说明:
-t rsa - 密钥类型(RSA、ECDSA、ED25519)
-b 4096 - 密钥长度(位数),4096位推荐用于 RSA
-C "comment" - 注释,通常用邮箱标识密钥用途
生成 ED25519 密钥(推荐)
1
| ssh-keygen -t ed25519 -C "your_email@example.com"
|
优势:
- 更短的密钥长度(256位)
- 更强的安全性
- 更快的签名速度
1.2 密钥存储位置
执行命令后会提示输入文件名:
1
| Enter file in which to save the key (/Users/username/.ssh/id_rsa):
|
默认位置: ~/.ssh/id_rsa(私钥)和 ~/.ssh/id_rsa.pub(公钥)
1.3 设置密钥密码
1
| Enter passphrase (empty for no passphrase):
|
选择:
- ✅ 建议设置密码 - 增加安全性,防止私钥被直接利用
- ✅ 使用 ssh-agent - 避免每次输入密码
1.4 完整生成示例
1
2
3
4
5
| # 生成默认密钥
ssh-keygen -t ed25519 -C "Tamino@example.com"
# 生成特定名称的密钥(用于多个账户)
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
|
二、SSH Key 基础用法
2.1 查看公钥内容
输出示例:
1
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDx... Tamino@example.com
|
2.2 配置 GitHub/GitLab/Gitee 等平台
- 复制公钥内容:
1
2
| cat ~/.ssh/id_rsa.pub | pbcopy # macOS
cat ~/.ssh/id_rsa.pub | xclip # Linux
|
- 进入平台设置页面
- GitHub: Settings → SSH and GPG keys → New SSH key
- GitLab: Settings → SSH Keys
- Gitee: 设置 → SSH 公钥
- 粘贴公钥内容并保存
2.3 测试 SSH 连接
1
2
3
4
5
6
7
8
| # 测试 GitHub
ssh -T git@github.com
# 测试 GitLab
ssh -T git@gitlab.com
# 测试 Gitee
ssh -T git@gitee.com
|
成功提示:
1
| Hi Tamino! You've successfully authenticated, but GitHub does not provide shell access.
|
2.4 查看已加载的密钥
2.5 清除所有密钥
2.6 添加密钥到 ssh-agent(输入密码一次)
三、多个 Git 仓库管理方案
3.1 场景描述
- 多个 GitHub 账户(个人 + 工作)
- 多个 Git 平台(GitHub、GitLab、Gitee)
- 需要灵活切换和管理
3.2 方案一:使用 SSH Config 文件(推荐)
步骤 1:生成多个密钥对
1
2
3
4
5
6
7
8
| # 个人账户
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_github_personal
# 工作账户
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_github_work
# Gitee 账户
ssh-keygen -t ed25519 -C "gitee@example.com" -f ~/.ssh/id_ed25519_gitee
|
步骤 2:创建或编辑 SSH Config 文件
编辑 ~/.ssh/config:
添加以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| # GitHub - Personal Account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_personal
AddKeysToAgent yes
# GitHub - Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
AddKeysToAgent yes
# GitLab
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
AddKeysToAgent yes
# Gitee
Host gitee
HostName gitee.com
User git
IdentityFile ~/.ssh/id_ed25519_gitee
AddKeysToAgent yes
|
步骤 3:配置 Git 仓库
方式A:克隆时指定 Host
1
2
3
4
5
6
7
8
| # 使用 github-personal
git clone git@github-personal:username/repo.git
# 使用 github-work
git clone git@github-work:workusername/repo.git
# 使用 gitee
git clone git@gitee:giteeuser/repo.git
|
方式B:修改已有仓库配置
进入仓库目录,编辑 .git/config:
1
2
| cd my-repo
cat .git/config
|
修改 remote URL:
1
2
| [remote "origin"]
url = git@github-personal:username/repo.git
|
或使用命令行修改:
1
| git remote set-url origin git@github-personal:username/repo.git
|
步骤 4:配置 Git 用户信息
对于不同仓库,配置不同的用户名和邮箱:
1
2
3
4
5
6
7
8
| # 全局配置(默认)
git config --global user.name "Tamino"
git config --global user.email "Tamino@example.com"
# 特定仓库配置(覆盖全局)
cd /path/to/work-repo
git config user.name "Work Name"
git config user.email "work@company.com"
|
3.3 方案二:使用 Git 目录级别配置
创建目录结构:
1
2
3
4
5
6
| ~/projects/
├── personal/
│ └── .gitconfig-personal
├── work/
│ └── .gitconfig-work
└── .gitconfig
|
主 .gitconfig:
1
2
3
4
5
6
7
8
9
| [includeIf "gitdir:~/projects/personal/"]
path = ~/projects/personal/.gitconfig-personal
[includeIf "gitdir:~/projects/work/"]
path = ~/projects/work/.gitconfig-work
[user]
name = Default Name
email = default@example.com
|
personal/.gitconfig-personal:
1
2
3
| [user]
name = Tamino
email = Tamino@example.com
|
work/.gitconfig-work:
1
2
3
| [user]
name = Work Name
email = work@company.com
|
3.4 方案三:使用 Git 别名
在 ~/.gitconfig 中添加:
1
2
3
| [alias]
clone-personal = !git clone git@github-personal:
clone-work = !git clone git@github-work:
|
使用:
1
2
| git clone-personal username/repo.git
git clone-work workusername/repo.git
|
3.5 测试多密钥配置
1
2
3
4
5
6
7
8
9
10
11
| # 测试 github-personal
ssh -T git@github-personal
# 测试 github-work
ssh -T git@github-work
# 测试 gitee
ssh -T git@gitee
# 详细输出(调试)
ssh -vT git@github-personal
|
四、密钥管理最佳实践
4.1 安全建议
| 建议 |
说明 |
| ✅ 设置密码 |
保护私钥,防止直接使用 |
| ✅ 定期轮换 |
每年或在泄露后更新密钥 |
| ✅ 备份私钥 |
安全存储备份(离线/加密存储) |
| ✅ 限制权限 |
~/.ssh 目录权限 700,私钥权限 600 |
| ❌ 不提交私钥 |
从不将私钥提交到 Git 仓库 |
| ❌ 不在公网分享 |
私钥绝不在邮件、聊天、代码中分享 |
4.2 权限检查
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 检查目录权限
ls -la ~/.ssh
# 输出应该显示:drwx------ user user
# 检查密钥文件权限
ls -la ~/.ssh/id_*
# 私钥应该是:-rw------- (600)
# 公钥应该是:-rw-r--r-- (644)
# 修复权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_*
chmod 644 ~/.ssh/*.pub
|
4.3 从旧密钥迁移
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 1. 生成新密钥
ssh-keygen -t ed25519 -C "new@example.com" -f ~/.ssh/id_ed25519_new
# 2. 添加新公钥到所有平台
cat ~/.ssh/id_ed25519_new.pub
# 3. 测试新密钥是否有效
ssh -T git@github.com
# 4. 从平台移除旧公钥
# 5. 删除旧私钥
rm ~/.ssh/id_rsa
|
4.4 密钥丢失恢复
1
2
3
4
5
6
7
| # 1. 生成新密钥
ssh-keygen -t ed25519 -C "new@example.com"
# 2. 更新所有平台上的公钥
# 3. 更新所有本地仓库配置
# 如果使用了 SSH config,仅需更新 IdentityFile 路径
|
五、常见问题排查
问题 1:Permission denied (publickey)
1
2
3
4
5
6
7
8
| # 检查密钥是否在 ssh-agent 中
ssh-add -l
# 添加密钥
ssh-add ~/.ssh/id_rsa
# 详细调试
ssh -vvv git@github.com
|
问题 2:密钥密码总是被要求
1
2
3
4
5
| # 添加密钥到 ssh-agent(输入一次密码)
ssh-add ~/.ssh/id_rsa
# macOS 使用 Keychain(可选)
ssh-add --apple-use-keychain ~/.ssh/id_rsa
|
问题 3:Wrong host key 错误
1
2
3
4
5
| # 清除已知的 host key
ssh-keygen -R github.com
# 重新连接(会要求确认)
ssh -T git@github.com
|
问题 4:多个密钥时自动选择错误的密钥
在 SSH config 中指定 IdentityFile 并按需排序:
1
2
3
4
5
6
| # 优先使用 work 密钥
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentityFile ~/.ssh/id_ed25519_github_personal
|
六、快速命令参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # 生成密钥
ssh-keygen -t ed25519 -C "email@example.com"
# 查看公钥
cat ~/.ssh/id_ed25519.pub
# 测试连接
ssh -T git@github.com
# 添加密钥到 agent
ssh-add ~/.ssh/id_ed25519
# 查看已加载的密钥
ssh-add -l
# 查看私钥指纹
ssh-keygen -l -f ~/.ssh/id_ed25519
# 导出密钥为其他格式
ssh-keygen -p -N "" -m pem -f ~/.ssh/id_ed25519
# 编辑 SSH config
nano ~/.ssh/config
# 清除已知的 host key
ssh-keygen -R github.com
|
七、参考资源
最后更新: 2026-04-09