Skip to content

Instantly share code, notes, and snippets.

@espresso3389
Last active May 19, 2026 07:58
Show Gist options
  • Select an option

  • Save espresso3389/6fbbe818cb98b6bab1e73cb39a0aee64 to your computer and use it in GitHub Desktop.

Select an option

Save espresso3389/6fbbe818cb98b6bab1e73cb39a0aee64 to your computer and use it in GitHub Desktop.
Bitwarden SSH Agent relay to WSL2
#!/bin/bash
set -e
echo "Installing npiperelay..."
/mnt/c/WINDOWS/system32/cmd.exe /c "winget install --id=albertony.npiperelay -e --accept-source-agreements --accept-package-agreements"
echo "Installing socat..."
sudo apt-get update
sudo apt-get install -y socat iproute2
echo "Creating SSH agent bridge script..."
SCRIPT_DIR="$HOME/.local/bin"
mkdir -p "$SCRIPT_DIR"
cat << 'EOF' > "$SCRIPT_DIR/agent-bridge.sh"
#!/bin/bash
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
NPIPERELAY_WIN=$(
/mnt/c/WINDOWS/system32/cmd.exe /c "where npiperelay.exe" 2>/dev/null |
tr -d '\r' |
head -n 1
)
if [ -z "$NPIPERELAY_WIN" ]; then
echo "npiperelay.exe was not found in Windows PATH" >&2
return 1 2>/dev/null || exit 1
fi
NPIPERELAY="$(wslpath -u "$NPIPERELAY_WIN")"
if [ ! -x "$NPIPERELAY" ]; then
echo "npiperelay.exe is not executable from WSL: $NPIPERELAY" >&2
return 1 2>/dev/null || exit 1
fi
if ! ss -a 2>/dev/null | grep -Fq -- "$SSH_AUTH_SOCK"; then
rm -f "$SSH_AUTH_SOCK"
(
setsid socat \
"UNIX-LISTEN:$SSH_AUTH_SOCK,fork" \
"EXEC:$NPIPERELAY -ei -s //./pipe/openssh-ssh-agent,nofork" \
&
) >/dev/null 2>&1
fi
EOF
chmod +x "$SCRIPT_DIR/agent-bridge.sh"
echo "Ensuring bridge script is sourced in .bashrc..."
BASHRC="$HOME/.bashrc"
BRIDGE_SOURCE='source ~/.local/bin/agent-bridge.sh'
touch "$BASHRC"
if ! grep -Fxq "$BRIDGE_SOURCE" "$BASHRC"; then
printf '\n%s\n' "$BRIDGE_SOURCE" >> "$BASHRC"
echo "Added agent bridge script to .bashrc"
else
echo "Bridge script already sourced in .bashrc"
fi
echo "Setup complete! Restart your shell or run:"
echo " source ~/.local/bin/agent-bridge.sh"
@espresso3389

Copy link
Copy Markdown
Author
curl -fsSL https://gist.githubusercontent.com/espresso3389/6fbbe818cb98b6bab1e73cb39a0aee64/raw/wsl2-bitwarden-ssh-agent.sh | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment