No description
  • PowerShell 97.8%
  • Batchfile 2.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-09-07 15:12:54 +08:00
templates 新建密码支持 2026-09-07 15:12:54 +08:00
vscode-ssh 新建密码支持 2026-09-07 15:12:54 +08:00
Install.ps1 新建密码支持 2026-09-07 15:12:54 +08:00
README.md 新建密码支持 2026-09-07 15:12:54 +08:00

VS Code Remote-SSH 自动密码支持

这个文件夹保存了当前 Windows + VS Code Remote-SSH 的密码登录包装器。下次换电脑时,复制本文件夹,按下面几步即可恢复。

快速安装

最核心的安装命令是:

winget install xhcoding.sshpass-win32

也可以在本文件夹打开 PowerShell,执行:

powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\Install.ps1

脚本会执行上面的 Winget 安装命令,并把包装器复制到:

C:\Tools\vscode-ssh

然后在 VS Code 的 settings.json 中加入:

"remote.SSH.path": "C:\\Tools\\vscode-ssh\\ssh-wrapper.cmd"

执行 Developer: Reload Window 使设置生效。

SSH 主机配置

打开:

%USERPROFILE%\.ssh\config

把 templates\ssh-config.example 中的主机块合并进去。不要覆盖原有配置,不要复制当前机器的真实密码、私钥或 known_hosts。

普通密码主机:

Host my-server
    # vscode-password: YOUR_SSH_PASSWORD
    HostName 192.0.2.10
    User your-user
    Port 22

vscode-password 必须写在对应的 Host 块内,且别名必须和 VS Code 连接时选择的名称完全一致。它是包装器识别的注释,不是 OpenSSH 的正式字段。

VS Code 编辑 root 文件

如果 SSH 账号可以 sudo,但普通登录不是 root:

Host my-server-root
    # vscode-password: YOUR_SSH_PASSWORD
    # vscode-sudo-root: yes
    HostName 192.0.2.10
    User your-user
    Port 22

SSH 密码和 sudo 密码相同时,只写 vscode-password。如果不同,再加:

    # vscode-sudo-password: YOUR_SUDO_PASSWORD

不需要额外写 RemoteCommand sudo -i,包装器会为 VS Code 的远端 shell 完成 sudo 提权。

首次连接和主机指纹

带有 vscode-password 的主机会自动使用:

StrictHostKeyChecking=accept-new

因此首次连接会自动把新指纹写入 %USERPROFILE%\.ssh\known_hosts;以后如果同一主机指纹变化,SSH 仍会拒绝连接。不要改成 StrictHostKeyChecking=no。

验证

C:\Tools\vscode-ssh\ssh-wrapper.cmd -V
powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\Tools\vscode-ssh\ssh-wrapper.ps1 -G my-server | Select-String '^stricthostkeychecking\s+'

第二条命令对带密码标记的主机应显示:

stricthostkeychecking accept-new

常见问题

sshpass.exe not found

重新打开 PowerShell 后再次执行:

winget install xhcoding.sshpass-win32

然后在 VS Code 执行 Developer: Reload Window。

仍然弹出密码

检查 remote.SSH.path、Host 别名和 vscode-password 的位置是否正确。包装器按别名精确匹配,不会猜测主机。

想重新测试首次连接

先确认服务器指纹确实可信,再按主机和端口删除对应条目,例如:

ssh-keygen -R '[host.example.com]:2222' -f "$env:USERPROFILE\.ssh\known_hosts"

包内文件

  • Install.ps1:安装 sshpass 并复制包装器;
  • vscode-ssh\ssh-wrapper.cmd:VS Code 调用入口;
  • vscode-ssh\ssh-wrapper.ps1:读取 SSH 配置注释并调用 sshpass;
  • templates\ssh-config.example:无真实密码的 SSH 配置模板;
  • templates\settings-snippet.json:VS Code 设置片段。

本文件夹不应包含 .ssh\config、known_hosts、私钥或真实密码。当前方案把密码明文放在本机 SSH 配置的注释中,这是为了便利而接受的本机安全取舍。