1password+wsl2配置ssh agent

步骤

打开1password的ssh agent

Windows 安装 npiperelay

winget install jstarks.npiperelay

WSL 安装 socat

# ubuntu
sudo apt install socat
# opensuse
sudo zypper install socat

配置环境变量

新建文件.agent-bridge.sh内容

# Code extracted from https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/ with minor modifications

# Configure ssh forwarding
SSH_AUTH_PATH=$HOME/.1password
export SSH_AUTH_SOCK=$SSH_AUTH_PATH/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..."
    if [[ ! -d $SSH_AUTH_PATH ]]; then
        echo "createing 1password directory..."
        mkdir $SSH_AUTH_PATH
    fi
    # 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
bash 用户

添加source ~/.agent-bridge.sh.profile

zsh 用户

添加source ~/.agent-bridge.sh.zprofile

fish 用户

安装 fenv,然后添加fenv source ~/.agent-bridge.sh.profile

重启 wsl 系统

打开cmd终端,并执行如下命令

wsl --shutdown

大功告成

参考资料