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
原因:
Agent 有多个 Key 时:
key1
key2
key3
SSH 可能依次尝试。
推荐方式
WSL 保存公钥:
~/.ssh/
config
github-work.pub
github-personal.pub
rn-vps.pub
agent.sock
不保存:
id_rsa
id_ed25519
*.pem
配置:
Host github-work
HostName github.com
User git
IdentityAgent ~/.ssh/agent.sock
IdentityFile ~/.ssh/github-work.pub
IdentitiesOnly yes
Host github-personal
HostName github.com
User git
IdentityAgent ~/.ssh/agent.sock
IdentityFile ~/.ssh/github-personal.pub
IdentitiesOnly yes
Host rn-vps
HostName 1.2.3.4
User root
IdentityAgent ~/.ssh/agent.sock
IdentityFile ~/.ssh/rn-vps.pub
IdentitiesOnly yes
8. 为什么 IdentityFile 可以使用公钥?
SSH流程:
IdentityFile (.pub)
|
↓
查找对应私钥
|
↓
SSH Agent
|
↓
KeePassXC保存的私钥
所以:
- 私钥不离开 KeePassXC
- WSL 可以指定身份
- 不会尝试其它 Key
9. 导出公钥
Windows:
ssh-add -L
复制对应公钥。
WSL保存:
nano ~/.ssh/github-work.pub
权限:
chmod 644 ~/.ssh/*.pub
10. 测试
查看 Agent:
ssh-add -l
测试 GitHub:
ssh -T git@github-work
调试:
ssh -vT git@github-work
应该看到:
Offering public key: github-work.pub
Server accepts key
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均可使用