如何通过VSCode Remote SSH将Windows主机的Git Credential Manager转发至Ubuntu虚拟机?
如何通过VSCode Remote SSH将Windows主机的Git Credential Manager转发到Ubuntu虚拟机
可以实现,以下是两种可行的配置方案:
方案一:通过自定义脚本让Ubuntu调用Windows主机的GCM
确保Windows主机开启SSH服务并支持无密码登录
- 在Windows上启用OpenSSH服务器(设置→应用→可选功能→添加功能→OpenSSH服务器),启动服务并设置为自动启动。
- 在Ubuntu虚拟机执行
ssh-keygen -t ed25519生成SSH密钥对,将公钥文件~/.ssh/id_ed25519.pub的内容复制到Windows主机的C:\Users\<你的用户名>\.ssh\authorized_keys文件中,确保Ubuntu能无密码SSH连接到Windows主机。
在Ubuntu上创建凭证助手脚本
- 创建脚本文件
/usr/local/bin/win-gcm-helper,内容如下(替换为你的Windows主机用户名和IP/主机名):#!/bin/bash ssh your_win_username@your_win_host_ip "git credential $1" - 给脚本添加执行权限:
chmod +x /usr/local/bin/win-gcm-helper
- 创建脚本文件
配置Ubuntu的Git使用该助手
- 执行命令设置全局Git凭证助手:
git config --global credential.helper /usr/local/bin/win-gcm-helper
- 执行命令设置全局Git凭证助手:
方案二:在Ubuntu安装GCM for Linux复用Windows凭证(可选)
如果不想通过SSH回连,可在Ubuntu安装Git Credential Manager for Linux,配置后复用Windows主机的微软账户凭证(适用于Windows和Ubuntu使用同一微软账户的场景):
- 在Ubuntu安装GCM:
curl -sSL https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/microsoft-prod.list sudo apt update && sudo apt install git-credential-manager-core - 配置GCM适配Azure DevOps:
首次克隆项目时会弹出登录窗口,使用和Windows主机相同的微软账户登录后,凭证会被自动缓存。git config --global credential.azuredevops.useHttpPath true git config --global credential.helper manager-core
验证配置
执行Azure DevOps项目的HTTPS克隆命令测试:
git clone https://dev.azure.com/your-org/your-project/_git/your-repo
此时应自动复用Windows主机已保存的凭证,无需重复输入。
内容的提问来源于stack exchange,提问作者gptech
相关产品推荐
相关产品推荐

