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

EC2上克隆多个GitHub仓库遇.ssh配置错误问题排查

解决GitHub多SSH密钥克隆权限拒绝问题

问题根源分析

从错误信息和配置来看,核心问题有三个:

  • SSH Config的Host格式错误,导致密钥匹配逻辑混乱
  • 配置里指定了公钥文件(带.pub后缀)用于认证,但SSH实际需要使用私钥
  • 私钥文件权限不符合SSH安全规范,被系统拒绝读取

修正步骤

1. 重新编写SSH Config文件

编辑/home/ubuntu/.ssh/config,替换为以下内容:

Host aimer-ui
  HostName github.com
  IdentityFile /home/ubuntu/.ssh/id_rsa_new
  User git

Host aimer-service
  HostName github.com
  IdentityFile /home/ubuntu/.ssh/id_rsa
  User git
  • Host字段仅需写自定义别名,无需附加github.com
  • IdentityFile必须指向私钥文件(不带.pub后缀),公钥是上传到GitHub部署密钥的,本地认证依赖私钥
  • 添加User git确保连接GitHub的用户身份正确

2. 修复私钥与目录权限

SSH对私钥权限要求严格,必须设置为仅所有者可读可写:

# 设置私钥权限为600
chmod 600 /home/ubuntu/.ssh/id_rsa_new
chmod 600 /home/ubuntu/.ssh/id_rsa

# 设置.ssh目录权限为700
chmod 700 /home/ubuntu/.ssh

3. 使用自定义别名克隆仓库

放弃原有的git@github.com地址,改用配置的Host别名:

  • 克隆aimer-ui:
git clone git@aimer-ui:BeA-Brand/aimer-ui.git
  • 克隆aimer-service:
git clone git@aimer-service:BeA-Brand/aimer-service.git

4. 验证配置有效性

执行以下命令测试SSH连接是否正常:

ssh -T git@aimer-ui
ssh -T git@aimer-service

如果输出类似Hi BeA-Brand/aimer-ui! You've successfully authenticated, but GitHub does not provide shell access.的信息,说明配置已生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 15:56:08