How to set up 1Password and WSL

Published at: March 6, 2023
cd ..

Original source

We’re going to pass the SSH socket from 1Password for Windows to WSL with a few programs. First, make sure to turn on the SSH agent.

Once, you’ve done that, you need to install npiperelay on Windows. Running $ winget install npiperelay in the CMD/Powershell will do it.

Then, you’ll install socat on Linux. If you’re using Ubuntu, you can run $ sudo apt install socat.

Finally, you’ll need to add this script to your .bashrc or similar.

.bashrc
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
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        echo "removing previous socket..."
        rm $SSH_AUTH_SOCK
    fi
    echo "Starting SSH-Agent relay..."
    (setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
fi

Alternatively, you can use this simplified script.

.bashrc
export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
(
  set -eu
  piperelay=(setsid socat "UNIX-LISTEN:$SSH_AUTH_SOCK,fork" "EXEC:npiperelay.exe -ei -s //./pipe/openssh-ssh-agent,nofork")
  if ! pgrep "-fxU$UID" "${piperelay[*]}" >/dev/null; then
    rm -f "$SSH_AUTH_SOCK"
    ("${piperelay[@]}" &) >/dev/null
  fi
)