Managing SSH keys securely while using Windows Subsystem for Linux 2 (WSL2) can be a challenge, especially when you want to leverage KeePassXC for SSH key storage and agent functionality. This post outlines a practical workaround to enable SSH access within WSL2 while utilizing SSH keys managed by KeePassXC on the Windows side.
KeePassXC is a popular open-source password manager that also supports storing SSH keys securely within its database. While KeePassXC itself does not provide a native SSH agent, it serves SSH keys to the Windows OpenSSH agent. This integration allows the Windows OpenSSH agent to handle key authentication requests, including keys managed by KeePassXC.
In a WSL2 environment, however, the Linux subsystem is isolated from the Windows environment, causing SSH clients inside WSL2 to not automatically communicate with the Windows SSH agent where the KeePassXC-managed keys reside. To resolve this, a mechanism to bridge communication between WSL2 and the Windows SSH agent is required.
The approach presented here simplifies usage by transparently redirecting SSH client commands in WSL2 to their Windows counterparts, which can communicate directly with the Windows OpenSSH agent, gaining access to the KeePassXC-stored SSH keys.
Add the following aliases to your ~/.bashrc file in WSL2:
# Redirect SSH commands to Windows executables
alias ssh='ssh.exe -F ~/.ssh/config'
alias ssh-add='ssh-add.exe'
alias scp='scp.exe'
alias sftp='sftp.exe'
# Configure Git to use Windows SSH client
git config --global core.sshCommand "ssh.exe -F ~/.ssh/config"
This setup instructs the shell to use the Windows versions of SSH, SCP, and SFTP, which inherently communicate with the Windows SSH agent (and by extension, with KeePassXC’s SSH key management). The option -F ~/.ssh/config ensures that SSH configuration inside WSL2 is respected when invoking Windows SSH.
Additionally, ensure your ~/.profile sources the .bashrc to load these aliases:
# Load bashrc if present
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
npiperelay and socat, which require more complex configuration.