Skip to content

Instantly share code, notes, and snippets.

@espresso3389
Last active February 19, 2026 08:28
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
# This code is licensed under MIT license https://choosealicense.com/licenses/mit/
#
# Note: I have only tested lightly and this seems to work. I won't take the blame if this makes your
# machine catch fire.
#
# Thanks to Aaron and the original work discussed here:
# https://www.rebelpeon.com/bitwarden-ssh-agent-on-wsl2/
#
# If you want to run this script as is you can:
# curl -fsSL https://gist.githubusercontent.com/espresso3389/6fbbe818cb98b6bab1e73cb39a0aee64/raw/wsl2-bitwarden-ssh-agent.sh | bash
echo "Installing npiperelay..."
/mnt/c/WINDOWS/system32/cmd.exe /c "winget install --id=albertony.npiperelay -e"
echo "Installing socat..."
sudo apt-get update
sudo apt-get install -y socat
echo "Creating SSH agent bridge script..."
SCRIPT_DIR=~/.local/bin
mkdir -p $SCRIPT_DIR
cat << 'EOF' > $SCRIPT_DIR/agent-bridge.sh
#!/bin/bash
# Ensure ~/.ssh directory exists with correct permissions
if [ ! -d "$HOME/.ssh" ]; then
mkdir -m 700 "$HOME/.ssh"
fi
export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
NPIPERELAY="$(wslpath -u "$(/mnt/c/WINDOWS/system32/cmd.exe /c "where npiperelay.exe" 2>/dev/null | tr -d '\r')")"
if ! ss -a 2>/dev/null | grep -q "$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=~/.bashrc
BRIDGE_SOURCE='source ~/.local/bin/agent-bridge.sh'
if ! grep -Fxq "$BRIDGE_SOURCE" "$BASHRC"; then
echo "$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
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