Skip to content

这个错误 No supported authentication methods available (server sent: publickey) 表示 SSH 客户端无法提供服务器接受的认证方式,通常是因为:


问题原因

  1. 本地 SSH 私钥未正确加载(未添加到 ssh-agent 或路径错误)。
  2. SSH 配置文件(~/.ssh/config)未正确指定密钥
  3. TortoiseGit 使用了错误的 SSH 客户端(如 PuTTY 的 plink 而非 OpenSSH)。
  4. GitLab 服务器未收到有效的公钥(公钥未添加或权限错误)。

解决方案

1. 确认 SSH 密钥已正确配置

(1) 检查密钥是否存在

Git BashCMD 中运行:

bash
ls ~/.ssh/id_rsa
ls ~/.ssh/id_rsa

如果不存在,先生成密钥:

bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

(直接按回车,使用默认路径)

(2) 将公钥添加到 GitLab

  1. 复制公钥内容:
    bash
    cat ~/.ssh/id_rsa.pub
    cat ~/.ssh/id_rsa.pub
  2. 登录 GitLab → 点击右上角头像 → PreferencesSSH Keys → 粘贴并保存。

2. 确保 SSH 私钥已加载到 ssh-agent

bash
# 启动 ssh-agent(如果未运行)
eval $(ssh-agent -s)

# 添加私钥
ssh-add ~/.ssh/id_rsa
# 启动 ssh-agent(如果未运行)
eval $(ssh-agent -s)

# 添加私钥
ssh-add ~/.ssh/id_rsa

如果提示 Could not open a connection to your authentication agent,先运行:

bash
exec ssh-agent bash
exec ssh-agent bash

3. 检查 TortoiseGit 的 SSH 客户端配置

  1. 右键 → TortoiseGit → Settings → Network
  2. 确保 SSH Client 指向系统的 ssh.exe(而非 PuTTY 的 plink.exe):
    C:\Windows\System32\OpenSSH\ssh.exe
    C:\Windows\System32\OpenSSH\ssh.exe
    或 Git 自带的 SSH:
    C:\Program Files\Git\usr\bin\ssh.exe
    C:\Program Files\Git\usr\bin\ssh.exe
  3. 点击 Apply 保存。

4. 测试 SSH 连接

在命令行手动测试:

bash
ssh -Tv git@gitlab.com
ssh -Tv git@gitlab.com
  • 如果成功,会返回:
    Welcome to GitLab, @your_username!
    Welcome to GitLab, @your_username!
  • 如果失败,会显示详细错误(如密钥未加载)。

5. 检查 SSH 配置文件(可选)

编辑 ~/.ssh/config,确保指定了正确的密钥:

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

6. 尝试使用 HTTPS 替代 SSH(临时方案)

如果 SSH 仍失败,改用 HTTPS:

bash
git clone https://gitlab.com/group-meter/meter-cloud.git
git clone https://gitlab.com/group-meter/meter-cloud.git

(需输入 GitLab 用户名和密码或 Personal Access Token)


常见问题排查

错误现象解决方案
Permission denied检查 SSH 密钥是否添加到 GitLab
Could not open a connection to ssh-agent先运行 exec ssh-agent bash
TortoiseGit 仍失败改用 Git Bash 或 VSCode 测试
公司网络限制 SSH尝试 HTTPS 或 VPN

终极解决方案

如果所有方法均无效:

  1. 完全删除 TortoiseGit 的 SSH 缓存
    • 删除 %USERPROFILE%\.ssh\known_hosts
  2. 重装 Git 和 TortoiseGit,确保使用 OpenSSH。

附:SSH 连接调试命令

bash
ssh -Tvvv git@gitlab.com
ssh -Tvvv git@gitlab.com

通过 -vvv 参数查看详细的连接过程,定位具体失败步骤。