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

SSH配置错误选用密钥?Bitbucket工作仓库克隆失败求助

问题原因及解决办法

你的问题出在SSH配置的继承逻辑和IdentitiesOnly参数的实际作用上:

  1. 当你执行git clone work:path/to/some.git时,SSH会先匹配Host work段的配置,然后自动继承Host *段中未被覆盖的配置项。
  2. 你初始配置里的Host *段指定了IdentityFile ~/.ssh/id_rsa,而Host work段的IdentitiesOnly yes只是禁止使用SSH agent自动加载的额外密钥,不会阻止配置中明确列出的多个IdentityFile被依次尝试。
  3. 所以SSH会先试id_work,一旦校验过程中出现时序或网络层面的小问题,就会接着尝试id_rsa——而这个密钥关联的是你的个人Bitbucket账号,没有工作仓库的访问权限,最终导致报错。

正确配置方案

可以给个人和工作账号分别配置独立的Host别名,彻底避免密钥交叉尝试:

# 工作Bitbucket账号配置
Host work-bitbucket
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_work
  IdentitiesOnly yes

# 个人Bitbucket账号配置
Host personal-bitbucket
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

# 全局默认配置(不要指定IdentityFile,避免自动继承)
Host *
  AddKeysToAgent yes
  UseKeychain yes

使用时直接对应别名即可:

  • 克隆工作仓库:git clone work-bitbucket:path/to/work-repo.git
  • 克隆个人仓库:git clone personal-bitbucket:path/to/personal-repo.git

内容的提问来源于stack exchange,提问作者delboy1978uk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 15:15:16