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

GitLab SSH连接识别错误用户名,仓库拉取权限异常求助

解决GitLab SSH身份验证错误:错误用户名关联及权限问题

执行ssh -T git@gitlab.com时返回Welcome to GitLab, @superfake!,但superfake是我在GitHub等其他服务、旧电脑上使用的用户名,并非当前GitLab的正确用户名superrandom。拉取自己的GitLab仓库时触发权限错误:

remote:
remote: ========================================================================
remote:
remote: ERROR: The project you were looking for could not be found or you don't have permission to view it.

remote:
remote: ========================================================================
remote:
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

该问题在多台设备上持续数日,回忆起多数设备启用了SSH代理转发(-A),推测问题根源在桌面端环境。

排查过程

在XUbuntu 22.04上发现终端行为差异:

  • 新终端窗口执行ssh -T git@gitlab.com显示错误用户名@superfake
  • 新终端标签页执行则显示正确用户名@superrandom,且git fetch可正常运行

通过对比两个终端的环境变量(分别执行env > test1和env > test2,再用vimdiff test1 test2对比),发现异常终端存在SSH_AUTH_SOCK=/run/user/1000/keyring/ssh,正常终端无此变量。在异常终端执行export SSH_AUTH_SOCK=后,身份验证恢复正常,可显示正确用户名。

解决要点

  • 终端环境修复:gnome-terminal存在标签页环境变量不一致问题,已切换至xfce-terminal(适配XUbuntu环境)。
  • 强制指定SSH密钥:Git/GitLab通过SSH密钥而非用户名完成身份验证,将对应GitLab账号的公钥key1.pub添加至GitLab账户后,在~/.ssh/config中添加以下配置强制使用该密钥:
host *gitlab*
  identityfile ~/.ssh/key1.pub
  identitiesonly yes

执行ssh-add -l确认密钥已加载,避免系统自动选用其他无关密钥。

  • SSH代理转发配置规范:使用SSH代理转发(-A)时,中转/目标设备的本地~/.ssh/config仅保留服务器快捷访问配置,避免与转发设置冲突:
host nickname
  hostname server1
  user myname

内容的提问来源于stack exchange,提问作者Jonatan Öström

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 04:12:15