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

Ubuntu WSL下GitHub SSH认证失败:Permission denied (publickey)

问题

之前在Ubuntu WSL上配置了两个GitHub账号(学校.edu账号及个人账号),通过git config user.name/user.email切换账号,提交代码、克隆仓库均正常。后续安装KDE.org的GitLab并完成仓库复刻与SSH认证后,尝试修改全局配置时强制退出(:q!),重启电脑后使用个人账号推送代码出现「Permission denied (publickey)」错误。按Ubuntu社区教程设置.ssh目录700权限、密钥文件600权限无效;重新生成GitLab和GitHub的SSH密钥也未解决问题。

.ssh/config配置片段

...

#personal account
Host github.com-victoriaemily
    HostName github.com
    User victoriaemily
    IdentityFile ~/.ssh/victoriaemily
    IdentitiesOnly yes

.ssh目录文件说明

从提供的截图可见,目录下包含victoriaemily(私钥)、victoriaemily.pub(公钥)、config、known_hosts等文件。

ssh -vT git@github.com调试日志

OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /home/victoriaemily/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [140.82.114.4] port 22.
debug1: Connection established.
debug1: identity file /home/victoriaemily/.ssh/id_rsa type -1
debug1: identity file /home/victoriaemily/.ssh/id_rsa-cert type -1
...
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
debug1: Remote protocol version 2.0, remote software version babeld-4ce3b487
debug1: no match: babeld-4ce3b487
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM
debug1: Host 'github.com' is known and matches the ECDSA host key.
debug1: Found key in /home/victoriaemily/.ssh/known_hosts:3
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/victoriaemily/.ssh/id_rsa
debug1: Will attempt key: /home/victoriaemily/.ssh/id_dsa
...
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh...
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/victoriaemily/.ssh/id_rsa
debug1: Trying private key: /home/victoriaemily/.ssh/id_dsa
...
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

解决方案

1. 修复SSH配置匹配问题

从调试日志可知,执行ssh -vT git@github.com时,SSH未匹配到github.com-victoriaemily的配置条目,只会尝试默认密钥文件(id_rsa、id_dsa等),不会加载你指定的victoriaemily密钥。可二选一修复:

方式A:修改仓库远程地址

针对个人账号的仓库,将远程地址替换为config中定义的Host别名:

git remote set-url origin git@github.com-victoriaemily:你的个人用户名/仓库名.git

后续推送时,SSH会自动加载对应的victoriaemily密钥。

方式B:修改.ssh/config匹配github.com

若想直接使用git@github.com,在config中新增或修改条目:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/victoriaemily
    IdentitiesOnly yes

注意:若需保留学校账号,需给学校账号单独设置Host别名(如github.com-school),并让学校仓库使用对应的远程地址。

2. 验证SSH连接

修改后执行对应命令验证:

  • 方式A:ssh -vT git@github.com-victoriaemily
  • 方式B:ssh -vT git@github.com
    查看日志是否加载了~/.ssh/victoriaemily密钥且认证成功。

3. 确认Git账号配置

虽然SSH认证与user.name/email无关,但需确保当前仓库的配置正确:

# 查看当前仓库配置
git config user.name
git config user.email
# 配置错误则修正
git config user.name "你的个人账号用户名"
git config user.email "你的个人账号邮箱"

4. 重置SSH代理(可选)

若之前启动过ssh-agent缓存了旧密钥,重启代理并重新加载密钥:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/victoriaemily

再重新测试SSH连接。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:05:38