Skip to content

Instantly share code, notes, and snippets.

@Sieboldianus
Created January 25, 2023 10:18
Show Gist options
  • Save Sieboldianus/0227528ee8f4a4f7d9ef65cdcb176cd0 to your computer and use it in GitHub Desktop.
Save Sieboldianus/0227528ee8f4a4f7d9ef65cdcb176cd0 to your computer and use it in GitHub Desktop.
Use Windows Pageant SSH-Agent in WSL2 with socat wsl-ssh-agent-relay

The instructions below are based on rupor-github/wsl-ssh-agent and benpye/wsl-ssh-pageant/issues/33.

Install dependencies:

sudo apt update && sudo apt install socat p7zip-full

Get npiperelay.exe, store it on the Windows side and make it available in Linux with correct permissions.

  • replace {username} with your Linux username (e.g. whoami)
  • replace [USER] with your Windows username
cd /tmp
wget https://github.com/jstarks/npiperelay/releases/download/v0.1.0/npiperelay_windows_386.zip
7z e -y /tmp/npiperelay_windows_386.zip
sudo cp npiperelay.exe /c/Users/[USER]
sudo chmod +x /c/Users/[USER]/npiperelay.exe
sudo chown {username}:{username} /c/Users/[USER]/npiperelay.exe
You can check which Windows named pipes exist (with "ssh" in name) using Powershell:
[System.IO.Directory]::GetFiles("\\.\\pipe\\") | Select-String -Pattern ssh

Get wsl-ssh-agent-relay and store it correct permissions:

cd /tmp
wget https://raw.githubusercontent.com/rupor-github/wsl-ssh-agent/master/docs/wsl-ssh-agent-relay
sudo cp wsl-ssh-agent-relay ~/.local/bin/
sudo chmod +x ~/.local/bin/wsl-ssh-agent-relay
sudo chown {username}:{username} ~/.local/bin/wsl-ssh-agent-relay

Edit wsl-ssh-agent-relay and modify paths to npiperelay.exe and wsl-ssh-agent.sock:

nano ~/.local/bin/wsl-ssh-agent-relay

Edit:

  • replace [USER] with your Windows username
RELAY_BIN="/c/Users/[USER]/npiperelay.exe"
WSL_AGENT_SSH_SOCK="${HOME}/.ssh/wsl-ssh-agent.sock"

Prepare SSH directory:

mkdir -m 700 -p ~/.ssh

Update ~/.bashrc to auto-start relay and socat pipe:

nano ~/.bashrc
  • replace [USER] with your Windows username
${HOME}/.local/bin/wsl-ssh-agent-relay start
export SSH_AUTH_SOCK=${HOME}/.ssh/wsl-ssh-agent.sock
setsid nohup socat EXEC:"/c/Users/[USER]/npiperelay.exe //./pipe/\ssh-pageant" \
    UNIX-LISTEN:${SSH_AUTH_SOCK},unlink-close,unlink-early,fork \
    >/dev/null 2>&1 &

Reload ~/.bashrc bashrc:

source ~/.bashrc

Test:

ssh-add -l
> 2048 SHA256:
> ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment