Skip to content

Instantly share code, notes, and snippets.

@KairuDeibisu
Last active June 12, 2023 16:24
Show Gist options
  • Save KairuDeibisu/73b248462cb37de09bf2e4c1d73c4389 to your computer and use it in GitHub Desktop.
Save KairuDeibisu/73b248462cb37de09bf2e4c1d73c4389 to your computer and use it in GitHub Desktop.
Forward SSH from WSL 2
# Configure ssh forwarding
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
# Use pgrep to check if the process is running
pgrep -f "npiperelay -ei -s //./pipe/openssh-ssh-agent" > /dev/null
IS_ALREADY_RUNNING=$?
if [[ $IS_ALREADY_RUNNING -ne 0 ]]; then
if [[ -S $SSH_AUTH_SOCK ]]; then
# Not expecting the socket to exist as the forwarding command isn't running
echo "Removing previous socket..."
rm $SSH_AUTH_SOCK
fi
echo "Starting SSH-Agent relay..."
# Setsid to force new session to keep running
# Set socat to listen on $SSH_AUTH_SOCK and forward to npiperelay which then forwards to openssh-ssh-agent on windows
(setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment