You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Bitbucket和Git配置多套SSH,让其自动匹配正确账号?

解决多Bitbucket账号SSH配置冲突问题

问题根源

你之前的两种配置都存在问题:

  • 第一种配置里两个Host均为bitbucket.org,SSH会优先匹配第一个条目,因此永远只会使用账号1的密钥。
  • 第二种配置虽然用了别名,但你未修改对应项目的Git远程地址,Git仍默认使用git@bitbucket.org:...的地址,SSH找不到匹配的别名配置,自然还是用默认密钥(或残留配置),导致连接失败。

正确配置步骤

1. 调整SSH配置文件(~/.ssh/config)

用不同的Host别名区分两个账号,同时添加IdentitiesOnly yes强制SSH仅使用指定密钥,避免自动尝试其他密钥:

# 账号1配置
Host bitbucket-account1
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa_1
    IdentitiesOnly yes

# 账号2配置
Host bitbucket-account2
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa_2
    IdentitiesOnly yes

注意:Bitbucket的SSH连接用户固定为git,不是你的账号用户名,之前配置里的User user1是错误的,这也是导致失败的核心原因之一!

2. 修改对应Git项目的远程地址

针对每个项目,将远程仓库地址中的bitbucket.org替换为配置好的别名:

  • 账号1的项目执行:
    git remote set-url origin git@bitbucket-account1:your-account1-username/your-repo.git
    
  • 账号2的项目执行:
    git remote set-url origin git@bitbucket-account2:your-account2-username/your-repo.git
    

3. 测试配置是否生效

分别测试两个账号的SSH连接:

ssh -T git@bitbucket-account1
ssh -T git@bitbucket-account2

若输出logged in as your-username.,则说明配置成功。

额外说明

SSH不会自动遍历所有配置尝试匹配,只会匹配最符合当前连接地址的Host条目。因此必须通过别名区分不同账号,再配合修改Git远程地址,才能让不同项目使用对应的密钥。

内容的提问来源于stack exchange,提问作者Héctor Valls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:02:19