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

M1 Mac配置GitHub多SSH密钥遇权限问题求助

SSH配置后直接连接github.com权限拒绝的解决方法

问题根源

你在~/.ssh/config里全局设置了IdentitiesOnly yes,这个参数会强制SSH只使用对应Host块里明确指定的密钥文件,不会自动尝试SSH Agent中的密钥或默认的密钥文件(比如id_ed25519)。

当你直接执行ssh -T git@github.com或git clone git@github.com:...时,这个连接请求没有匹配到config里的Host personal-github块,SSH找不到可用的密钥配置,因此返回权限拒绝。而ssh personal-github能成功,是因为它精准匹配了对应的Host块,使用了你指定的github-personal密钥,并且通过AddKeysToAgent yes把密钥加入了Agent,但全局的IdentitiesOnly yes阻止了直接连接github.com时使用Agent中的密钥。

解决方法(推荐方案)

修改~/.ssh/config,把IdentitiesOnly yes移到Host块内,同时让Host匹配github.com和你的别名personal-github,这样两种连接方式都会使用指定密钥:

AddKeysToAgent yes

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

验证方法

  1. 保存修改后的config文件
  2. 执行ssh -T git@github.com,应该能成功验证
  3. 执行git clone git@github.com:my-user-name/my-repo,也能正常克隆

其他可选方案

如果不需要严格限制密钥使用,也可以移除全局的IdentitiesOnly yes,这样SSH会自动尝试SSH Agent中的密钥(因为ssh personal-github已经把密钥加入Agent),但这种方式在多账户场景下容易出现密钥混淆,不推荐长期使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 06:45:36