【问题标题】:How to pass git shh credentials from wsl to VScode dev-container如何将 git ssh 凭据从 wsl 传递到 VScode dev-container
【发布时间】:2022-12-06 22:34:15
【问题描述】:

我已经使用 WSL 中的 ssh 密钥为我的 gitlab 帐户设置了 ssh 密钥。

现在我想在 VScode 开发容器中使用相同的 ssh git 凭据。

如何将 ssh 密钥传递给开发容器?

【问题讨论】:

    标签: git ssh wsl-2 vscode-devcontainer


    【解决方案1】:

    在 Vscode dev-container documentation 中,他们解释了如何使用 SSH 代理将 shh 密钥传递给容器:

    首先找到您系统上的关键文件。为此,请在您的 WSL 终端(例如 Ubuntu)ls ~/.ssh/ 中运行以下命令。默认情况下,shh 密钥文件名以 id_ 开头。寻找这样的文件(例如 id_ed25519)。

    在终端运行eval "$(ssh-agent -s)"。然后运行ssh-add ~/.ssh/id_ed25519(将文件名替换为您的密钥文件)。

    顺便说一句,列出已经添加的密钥,运行shh-add -l

    问题是在 linux 中,ssh-agent 不会在启动时自动启动。所以我们需要将它添加到~/.bash_profile 文件中。

    在终端运行code ~/.bash_profile 以在 vscode 中打开文件。然后将以下内容添加到文件中:

    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`
       ssh-add ~/.ssh/id_ed25519
    fi
    

    注意末尾的 ssh-add 行。这是因为 linux 中的 ssh-agent 不像在 windows 中那样保留密钥。

    重新启动计算机或通过运行 wsl --shutdown 重新启动 WSL。这将提示来自 docker 的消息以重新启动 Windows。打开一个新的 WSL 终端,键入 shh-add -l 以查看密钥是否存在。

    现在在 VScode 开发容器和终端类型 shh-add -l 中启动一个项目。应列出与您的 WSL 中相同的密钥。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2022-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多