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.comIdentityFile必须指向私钥文件(不带.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
相关产品推荐
相关产品推荐

