Skip to content
AstroPaper
Go back

Windows + WSL2 + KeePassXC + SSH Agent 配置指南

Edit page

1. 背景

开发环境:

  • Windows 11
  • WSL2 Ubuntu 24.04
  • GitHub / GitLab / VPS 多 SSH 身份
  • KeePassXC 管理 SSH 私钥

目标:

  • 私钥只保存在 KeePassXC
  • Windows Git 和 WSL Git 共用 SSH Agent
  • WSL 不保存私钥
  • 多账号、多服务器明确选择身份
  • 避免 SSH 尝试多个 Key 导致认证失败

最终结构:

Windows
│
├── KeePassXC
│     └── SSH 私钥
│
├── OpenSSH Authentication Agent
│
└── WSL2 Ubuntu
      │
      ├── ~/.ssh/config
      ├── ~/.ssh/*.pub
      ├── ~/.ssh/agent.sock
      └── ssh / git

2. Windows SSH Agent

确认 Windows Agent:

PowerShell:

Get-Service ssh-agent

应该:

Status   Name
Running  ssh-agent

查看加载的 Key:

ssh-add -l

示例:

256 SHA256:xxxx keepassxc_git_remote (ED25519)
256 SHA256:xxxx keepassxc_rn_vps (ED25519)

说明:

  • KeePassXC 已加载私钥
  • Windows SSH Agent 工作正常

3. WSL 安装依赖

Ubuntu:

sudo apt install -y socat

4. Windows Agent 转发到 WSL

4.1 安装 npiperelay

下载:

npiperelay.exe

放:

C:\Users\zhang\.ssh\npiperelay.exe

WSL路径:

/mnt/c/Users/zhang/.ssh/npiperelay.exe

4.2 创建 Agent Socket

测试:

rm -f ~/.ssh/agent.sock

socat -d -d \
UNIX-LISTEN:$HOME/.ssh/agent.sock,fork \
EXEC:"/mnt/c/Users/zhang/.ssh/npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork

成功:

listening on /home/fs/.ssh/agent.sock

另开终端:

export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock

ssh-add -l

应该看到 Windows 中相同 Key。


5. 自动启动

推荐使用 systemd user service。

创建:

mkdir -p ~/.config/systemd/user

文件:

~/.config/systemd/user/ssh-agent-bridge.service

内容:

[Unit]
Description=Windows OpenSSH Agent Bridge

[Service]
Type=simple

ExecStartPre=/usr/bin/rm -f %h/.ssh/agent.sock

ExecStart=/usr/bin/socat \
 UNIX-LISTEN:%h/.ssh/agent.sock,fork \
 EXEC:"/mnt/c/Users/zhang/.ssh/npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork

Restart=always

[Install]
WantedBy=default.target

启用:

systemctl --user daemon-reload

systemctl --user enable --now ssh-agent-bridge

检查:

systemctl --user status ssh-agent-bridge

6. 设置 SSH_AUTH_SOCK

.bashrc

加入:

export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock

不要在 .bashrc 中启动 socat。

原因:

  • 每打开一个终端都会执行
  • 容易产生多个 socket
  • 不方便排错

7. SSH Config 配置

不推荐

Host github.com
    IdentityAgent ~/.ssh/agent.sock

原因:

只写 IdentityAgent 而不写 IdentityFile 时,SSH 不会从 Agent 中加载任何密钥(只会尝试 ~/.ssh/id_rsa 等默认私钥文件),直接认证失败:

$ ssh -vT git@github.com
debug1: Trying private key: /home/fs/.ssh/id_ed25519
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

并非”依次尝试多个 Key”,而是”一个都不试”。


推荐方式

WSL 只保存公钥:

~/.ssh/

config
git_remote.pub
agent.sock

不保存私钥:

id_rsa
id_ed25519
*.pem

配置:

Host github.com
    HostName github.com
    User git
    IdentityAgent ~/.ssh/agent.sock
    IdentityFile ~/.ssh/git_remote.pub
    IdentitiesOnly yes

说明:

  • IdentityFile 指向公钥 .pub,SSH 按指纹从 Agent 匹配对应私钥
  • IdentitiesOnly yes 阻止尝试其它密钥

多台服务器(如 VPS)时,按相同格式复制段,把 Host 换成主机名、IdentityFile 换成对应公钥即可。


8. 为什么 IdentityFile 可以使用公钥?

SSH流程:

IdentityFile (.pub)
        |
        ↓
计算公钥指纹
        |
        ↓
按指纹从 Agent 匹配私钥
        |
        ↓
KeePassXC保存的私钥

SSH 读取 .pub 文件后,用它的指纹(如 SHA256:xxxx)在 Agent 中查找指纹相同的私钥。Agent 里有多个密钥时,只 Offering 匹配的那一个,不会逐个尝试:

$ ssh -vT git@github.com
debug1: get_agent_identities: agent returned 2 keys
debug1: Will attempt key: /home/fs/.ssh/git_remote.pub ... explicit agent
debug1: Offering public key: /home/fs/.ssh/git_remote.pub
debug1: Server accepts key

所以:

  • 私钥不离开 KeePassXC
  • 不会尝试其它 Key

9. 导出公钥

Windows:

ssh-add -L

复制对应公钥。

WSL保存:

nano ~/.ssh/git_remote.pub

权限:

chmod 644 ~/.ssh/*.pub

10. 测试

查看 Agent:

ssh-add -l

测试 GitHub:

ssh -T git@github.com

调试:

ssh -vT git@github.com

应该看到:

Offering public key: git_remote.pub
Server accepts key

测试 Git:

git push origin main

11. 常见问题

ssh-add:

Could not open a connection to your authentication agent

检查:

echo $SSH_AUTH_SOCK

应该:

/home/fs/.ssh/agent.sock

agent.sock不存在

检查:

ls -l ~/.ssh/agent.sock

重新启动:

systemctl --user restart ssh-agent-bridge

Windows没有pipe

PowerShell:

Get-ChildItem \\.\pipe\ | findstr ssh

应该:

openssh-ssh-agent

最终效果

Windows:

KeePassXC
    |
    ↓
OpenSSH Agent

WSL:

git
 |
ssh
 |
SSH_AUTH_SOCK
 |
npiperelay
 |
Windows Agent
 |
KeePassXC

特点:

  • 私钥集中管理
  • 多账号明确选择
  • WSL无私钥泄漏风险
  • Git/VSCode/OpenCode/Claude Code均可使用

Edit page