在一台电脑上同时使用多个 SSH 账号/密钥
下面是一套在一台电脑上同时使用多个 SSH 账号/密钥的通用做法(适用于 GitHub/GitLab/Bitbucket 以及登录多台服务器)。核心思路是:为每个账号生成独立密钥对,然后在 ~/.ssh/config 里用不同的 Host 别名绑定到不同的密钥。
1) 为每个账号生成独立密钥
建议使用 Ed25519(体积小、安全、默认可用):
# 个人 GitHub
ssh-keygen -t ed25519 -C "github-personal <you@example.com>" \
-f ~/.ssh/id_ed25519_github_personal
# 公司 GitHub
ssh-keygen -t ed25519 -C "github-work <you@company.com>" \
-f ~/.ssh/id_ed25519_github_work
小贴士
- 强烈建议给私钥设置口令(passphrase)。
- 权限要对:
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_* ~/.ssh/config(.pub公钥可 644)。
2)(可选)把密钥加入 ssh-agent(免重复输入口令)
macOS:
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_personal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_work
Linux / WSL / Git Bash:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_github_personal
ssh-add ~/.ssh/id_ed25519_github_work
3) 配置 ~/.ssh/config(关键)
创建或编辑 ~/.ssh/config,为同一主机的不同账号设置不同别名,并绑定对应私钥:
# 个人 GitHub 账号
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_personal
IdentitiesOnly yes
AddKeysToAgent yes
# macOS 可加:UseKeychain yes
# 公司 GitHub 账号
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
AddKeysToAgent yes
# 示例:同一台服务器不同用户/密钥
Host prod-admin
HostName 203.0.113.10
User admin
IdentityFile ~/.ssh/id_ed25519_prod_admin
IdentitiesOnly yes
Host prod-app
HostName 203.0.113.10
User app
IdentityFile ~/.ssh/id_ed25519_prod_app
IdentitiesOnly yes
说明
- 别名(如
github.com-personal、prod-admin)是你本地使用的“虚拟主机名”,用于区分不同身份。IdentitiesOnly yes能避免 ssh-agent 把一堆无关密钥都拿去试,从而导致 “Too many authentication failures”。- 有跳板机还可加
ProxyJump jump-host;需要再说我给样例。
4) 把 公钥 添加到对应平台/服务器
- GitHub:进入 Settings → SSH and GPG keys → New SSH key,粘贴
~/.ssh/id_ed25519_github_*.pub的内容。 - 服务器:把公钥内容追加到目标账号的
~/.ssh/authorized_keys。
快速拷贝公钥到剪贴板:
# macOS
pbcopy < ~/.ssh/id_ed25519_github_personal.pub
# Linux
xclip -sel clip < ~/.ssh/id_ed25519_github_personal.pub
# Windows(Git Bash)
cat ~/.ssh/id_ed25519_github_personal.pub | clip
5) 测试连接是否走对了密钥
ssh -T git@github.com-personal
ssh -T git@github.com-work
ssh -v prod-admin # -v 可打印调试信息,确认用了哪把钥匙
GitHub 成功会提示类似 “You’ve successfully authenticated, but GitHub does not provide shell access.”
6) Git 使用(以 GitHub 为例)
方式 A:用 Host 别名克隆
# 个人仓库
git clone git@github.com-personal:yourname/yourrepo.git
# 公司仓库
git clone git@github.com-work:yourorg/yourrepo.git
方式 B:为不同目录自动切换身份(可选进阶)
在 ~/.gitconfig 中按目录包含不同配置(比如 ~/work/ 放公司项目):
# ~/.gitconfig
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
~/.gitconfig-work:
[user]
name = Your Work Name
email = you@company.com
# 把所有 github.com 的 URL 自动重写为你的工作别名主机
[url "git@github.com-work:"]
insteadOf = https://github.com/
insteadOf = git@github.com:
这样你无论
git clone https://github.com/...还是git@github.com:...,在~/work/目录里都会被重写为走github.com-work(即工作密钥)。
7) 常见问题排查
-
Permission denied (publickey)
逐项检查:~/.ssh/config的Host是否用对(克隆/连接时必须用别名)。- 对应公钥是否已添加到平台/服务器。
- 私钥权限是否为 600。
- 用
ssh -v git@github.com-work看看实际用了哪把钥匙。
-
Too many authentication failures
在对应Host下确保有IdentitiesOnly yes,并且只配置需要的IdentityFile。 -
想为单个仓库强制使用某把钥匙(不想改远端 URL):
在仓库里执行git config core.sshCommand 'ssh -i ~/.ssh/id_ed25519_github_work -o IdentitiesOnly=yes'(此法会覆盖全局
~/.ssh/config,用于应急很方便。)
模板与建议
-
推荐把每把钥匙命名清晰:
id_ed25519_<平台/用途>_<身份>。 -
大量主机时可以把配置拆分:
# ~/.ssh/config Include ~/.ssh/conf.d/*.conf -
更高安全性:可考虑 FIDO2/安全密钥(
-t ed25519-sk)或配合硬件令牌使用。
如果你告诉我操作系统(macOS / Linux / Windows/WSL)和你的平台/服务器组合(例如 “GitHub 个人 + GitHub 企业 + 两台云主机”),我可以直接给你一份可粘贴的 ~/.ssh/config 模板和对应的一键脚本。

浙公网安备 33010602011771号