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

Win10+WSL2环境GitLab克隆遇SSH公钥权限错误求助

解决GitLab SSH克隆权限拒绝问题

核心问题分析

从ssh -Tv git@gitlab.com的输出可定位两个关键问题:

  1. SSH尝试加载id_rsa.pub作为私钥文件,但你生成的是id_ed25519密钥对,导致找不到对应文件,认证失败
  2. 你的~/.ssh/config配置存在错误:
    • Host未匹配gitlab.com,导致配置未生效
    • IdentityFile指向了公钥文件(SSH认证需使用私钥,应去掉.pub后缀)
    • User设置为自定义用户名,GitLab SSH认证必须使用git用户

分步修复步骤

1. 修正SSH配置文件

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

Host gitlab.com
    HostName gitlab.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519
    PubkeyAcceptedKeyTypes=+ssh-rsa
    TCPKeepAlive Yes

关键修正点:

  • Host设为gitlab.com,确保针对GitLab的请求应用此配置
  • User指定为git,这是GitLab SSH服务要求的固定用户名
  • IdentityFile指向私钥文件id_ed25519(而非公钥)

2. 验证SSH连接

执行命令测试认证是否正常:

ssh -Tv git@gitlab.com

若输出中出现Welcome to GitLab, @你的用户名!,则说明认证成功。

3. Gitbash环境补充修复

Gitbash使用Windows系统的~/.ssh目录(通常为C:\Users\<Windows用户名>\.ssh),与WSL目录相互独立,需完成以下操作:

  • 将WSL中生成的id_ed25519和id_ed25519.pub复制到Windows的C:\Users\<Windows用户名>\.ssh目录
  • 在Gitbash中启动ssh-agent并添加私钥:
    eval $(ssh-agent -s)
    ssh-add ~/.ssh/id_ed25519
    
  • 确保Gitbash的~/.ssh/config配置与上述修正后的内容一致

额外检查项

  • 确认GitLab账户中添加的公钥是id_ed25519.pub的完整内容,无多余空格或换行
  • 修复WSL中~/.ssh目录及文件权限:
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_ed25519
    chmod 644 ~/.ssh/id_ed25519.pub
    

权限设置不符合要求会导致SSH拒绝使用密钥。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 00:55:34