【问题标题】:SSH forwarding using WSL2 and VS code containers on Windows在 Windows 上使用 WSL2 和 VS 代码容器进行 SSH 转发
【发布时间】:2021-04-20 11:41:49
【问题描述】:

我在 Windows 上的 WSL2 下运行 Ubuntu。在 Ubuntu 中,我克隆了我的存储库,该存储库设置为运行 docker。当我在项目中运行docker-compose up时,它成功启动,我可以在Windows上通过VS代码打开容器。

当我尝试从 VS 代码中使用任何 git 功能时,就会出现问题。我刚收到permission denied (publickey)。如果我在 VS 代码中打开终端(连接到容器),在运行 git pull 时会出现同样的错误。

如果我从 Ubuntu 终端运行docker-compose run web bash,我可以成功运行git pull。所以代理被转发到容器,它只是在 VS Code 中不起作用。

我缺少一些设置吗?

【问题讨论】:

    标签: docker visual-studio-code windows-subsystem-for-linux


    【解决方案1】:

    要让 VS Code 在 WSL2 后端上运行的 Docker 容器内使用 WSL2 实例中的 SSH 密钥,您需要告诉 WSL2 在启动时创建一个 ssh-agent将您的 ssh 密钥添加到代理。当 VS Code 附加到在 WSL2 后端运行的容器时,它会自动获取正在运行的 ssh-agent,并允许您使用容器内的 WSL2 SSH 密钥进行身份验证。

    对于任何一种方法,您都需要在 WSL2 中安装 socat

    sudo apt install socat
    

    方法 1 - 手动 Bash 脚本

    要告诉您的 WSL2 发行版在启动时启动它的 ssh-agent,您需要将这些行添加到您的 ~/.bash_profile 或 ~/.zprofile(对于 Zsh),以便 ssh-agent 在 WSL2 启动时启动:

    if [ -z "$SSH_AUTH_SOCK" ]; then
       # Check for a currently running instance of the agent
       RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
       if [ "$RUNNING_AGENT" = "0" ]; then
            # Launch a new instance of the agent
            ssh-agent -s &> $HOME/.ssh/ssh-agent
       fi
       eval `cat $HOME/.ssh/ssh-agent`
    fi
    
    # also add your key to this ssh-agent session
    # 
    # When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa, 
    # ~/.ssh/id_ecdsa, ~/.ssh/id_ecdsa_sk, ~/.ssh/id_ed25519, and ~/.ssh/id_ed25519_sk. 
    # Source: https://man.cx/ssh-add
    ssh-add 
    
    # if you had to create ~/.bash_profile, these lines may also be needed
    # to load your ~/.bashrc config
    #
    # test and run the .bashrc file if it exists (this is the default on Ubuntu for WSL2) 
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
            . "$HOME/.bashrc"
        fi
    fi
    

    方法 2 - 钥匙串

    Keychain 基本上与上述相同,但使用一个简单的命令。默认情况下不会安装它,因此您必须使用发行版的包管理器或从源代码安装它。

    sudo apt install keychain
    

    安装钥匙串后,将以下内容添加到您的~/.bash_profile~/.zprofile

    # keychain will start the ssh agent and add the keys, or reuse the ssh agent
    # if it is already running
    eval `keychain --eval --agents ssh id_rsa`
    
    # ...
    

    您还可以使用钥匙串来设置您的 GPG 密钥(如果有)。

    如果~/.bash profile 尚不存在,(它不适用于在 WSL2 上默认安装的 Ubuntu),那么您需要将以下行添加到您的 ~/.bash_profile 的末尾,以便 ~/.bashrc 是使用 bash 时来源正确。

    # run the .bashrc file if it exists (this is the default on WSL2 if this does not already exist)
    # these lines may already exist if .bash_profile already exists
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
            . "$HOME/.bashrc"
        fi
    fi
    

    参考文献

    https://code.visualstudio.com/docs/remote/containers#_using-ssh-keys

    https://github.com/microsoft/vscode-remote-release/issues/2925#issuecomment-652558889

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多