Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save r3code/b7a40769985b4b591e271f06226b0407 to your computer and use it in GitHub Desktop.
Save r3code/b7a40769985b4b591e271f06226b0407 to your computer and use it in GitHub Desktop.
WSL 2 Allow SSH password store and use
# https://1password.community/discussion/128023/ssh-agent-on-windows-subsystem-for-linux
# Script: Do not ask for SSH password in Git/GitLab
# or you will be receiving prompt or error
# USEAGE: adding this to your .bashrc or .zshrc, Restart the ubuntu terminal / resource the rc file (source .bashrc), Test with ssh-add -l Should see your ssh keys.
# Configure ssh forwarding
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
# need `ps -ww` to get non-truncated command for matching
# use square brackets to generate a regex match for the process we want but that doesn't match the grep command running it!
ALREADY_RUNNING=$(ps -auxww | grep -q "[n]piperelay.exe -ei -s //./pipe/openssh-ssh-agent"; echo $?)
if [[ $ALREADY_RUNNING != "0" ]]; then
if [[ -S $SSH_AUTH_SOCK ]]; then
# not expecting the socket to exist as the forwarding command isn't running (http://www.tldp.org/LDP/abs/html/fto.html)
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.exe -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