SSH 多账号的配置

SSH 支持在 ~/.ssh/config 里为不同服务器配置不同的 Key,而且这是管理多个 GitHub/GitLab/服务器账号最推荐的方式。

1. 基本结构

例如你有:

~/.ssh/
├── config
├── id_ed25519
├── id_ed25519.pub
├── id_ed25519_work
├── id_ed25519_work.pub
├── id_ed25519_github
└── id_ed25519_github.pub

~/.ssh/config 可以写成:

# 默认账号
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
 
# 公司 GitHub 账号
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes
 
# 个人 GitHub 账号
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes

这里最关键的是:

Host github-work

这个 github-work 不是实际服务器地址,而是你自己定义的 SSH 别名。


2. GitHub 多账号怎么用

比如公司账号对应:

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

那么 Git remote 不要写:

git@github.com:company/project.git

而是写:

git@github-work:company/project.git

例如:

git clone git@github-work:company/project.git

个人账号:

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes

对应:

git clone git@github-personal:yourname/project.git

虽然 SSH 实际连接的都是 github.com,但因为 Host 不同,SSH 会自动选择不同的 Key。


3. 为什么推荐 IdentitiesOnly yes

这个配置非常重要:

IdentitiesOnly yes

它告诉 SSH:

只使用这里 IdentityFile 指定的 Key,不要把 ssh-agent 里面其他 Key 也拿出来尝试。

如果你有很多 SSH Key,没有这个配置时,可能遇到:

Too many authentication failures

或者 GitHub/GitLab 认证到了错误的账号。


4. 如果是不同服务器

例如:

Host server-a
    HostName 192.168.1.100
    User root
    Port 22
    IdentityFile ~/.ssh/server_a
    IdentitiesOnly yes
 
Host server-b
    HostName 10.0.0.20
    User ubuntu
    Port 22
    IdentityFile ~/.ssh/server_b
    IdentitiesOnly yes

以后直接:

ssh server-a

或者:

ssh server-b

SSH 会自动选择对应的 Key。


5. 一个比较完整的实际配置

我比较推荐这种写法:

# =========================
# GitHub - Personal
# =========================
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes
 
 
# =========================
# GitHub - Work
# =========================
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes
 
 
# =========================
# GitLab - Personal
# =========================
Host gitlab-personal
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitlab
    IdentitiesOnly yes
 
 
# =========================
# Production Server
# =========================
Host production
    HostName xxx.xxx.xxx.xxx
    User ubuntu
    IdentityFile ~/.ssh/id_ed25519_production
    IdentitiesOnly yes

然后:

ssh -T git@github-personal
ssh -T git@github-work
ssh production

分别测试。


6. 修改已有 Git 项目的 remote

如果项目之前是:

git@github.com:company/project.git

现在想使用 github-work

git remote set-url origin git@github-work:company/project.git

查看:

git remote -v

应该看到:

origin  git@github-work:company/project.git (fetch)
origin  git@github-work:company/project.git (push)

7. 注意权限

~/.ssh/config 最好设置:

chmod 600 ~/.ssh/config

私钥:

chmod 600 ~/.ssh/id_ed25519*

公钥可以:

chmod 644 ~/.ssh/*.pub

如果你告诉我你现在具体是 「多个 GitHub 账号」「GitHub + GitLab」,还是 「多个 Linux 服务器」,我可以直接按照你的 Key 文件名给你写一份可以复制到 ~/.ssh/config 的配置。