ziv*_*ziv 3 git ssh wsl-2 vscode-devcontainer
我已经使用 WSL 中的 ssh 密钥为我的 gitlab 帐户设置了 ssh 密钥。
现在我想在 VScode 开发容器中使用相同的 ssh git 凭据。
如何将 ssh 密钥传递到开发容器?
在 Vscode dev-container文档中,他们解释了如何使用 SSH 代理将 shh 密钥传递到容器:
首先找到您系统上的密钥文件。为此,请在 WSL 终端(例如 Ubuntu)中运行以下命令ls ~/.ssh/
。默认情况下,shh 密钥文件名以id_
. 寻找这样的文件(例如id_ed25519)。
在终端运行eval "$(ssh-agent -s)"
. 然后运行ssh-add ~/.ssh/id_ed25519
(将文件名替换为您的密钥文件)。
顺便说一句,要列出已添加的密钥,请运行ssh-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
Run Code Online (Sandbox Code Playgroud)
请注意末尾的 ssh-add 行。这是因为 Linux 中的 ssh-agent 不像 Windows 中那样保留密钥。
重新启动计算机或通过运行 重新启动 WSL wsl --shutdown
。这将提示 docker 发出一条消息,要求 Windows 重新启动。打开一个新的 WSL 终端作为 typeshh-add -l
来查看密钥是否在那里。
现在在 VScode 开发容器中启动一个项目并在终端中键入shh-add -l
。应列出与 WSL 中相同的密钥。
归档时间: |
|
查看次数: |
2604 次 |
最近记录: |