VS Code SSH连接为何还要求密码?SSH密钥已配置?

摘要:问题描述 SSH config 已经配置好了密钥登录: Host MyServer HostName xx.xx.xx.xx User root Port 22 IdentityFile "C:Usersone
问题描述 SSH config 已经配置好了密钥登录: Host MyServer HostName xx.xx.xx.xx User root Port 22 IdentityFile "C:\Users\onefly\.ssh\id_rsa" ForwardAgent yes 在 Git Bash 中 ssh MyServer 可以正常免密登录,但通过 VS Code Remote SSH 连接时却弹出密码输入框。 排查过程 1. 检查服务器端 ~/.ssh/authorized_keys 中已有对应公钥 ~/.ssh 权限 700,authorized_keys 权限 600 sshd_config 中 PermitRootLogin yes,PubkeyAuthentication 默认启用 服务器端一切正常。 2. 发现关键线索 查看 VS Code 的 Remote SSH 日志(输出面板 → Remote - SSH),发现关键错误: debug1: Offering public key: C:\\Users\\ranxi\\.ssh\\id_rsa RSA SHA256:xxxxx explicit debug1: Server accepts key: C:\\Users\\ranxi\\.ssh\\id_rsa RSA SHA256:xxxxx explicit Bad permissions. Try removing permissions for user: MSI\CodexSandboxUsers (S-1-5-21-xxxx) on file C:/Users/ranxi/.ssh/id_rsa. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions for 'C:\\Users\\ranxi\\.ssh\\id_rsa' are too open. This private key will be ignored. Load key "C:\\Users\\ranxi\\.ssh\\id_rsa": bad permissions debug1: Next authentication method: password 服务器已经接受了密钥,但客户端在签名阶段拒绝使用它,因为私钥文件权限"太开放"。 3. 根本原因 VS Code Remote SSH 使用的是 Windows 自带的 OpenSSH(C:\Windows\System32\OpenSSH\ssh.exe),而不是 Git Bash 自带的 OpenSSH。 两者的区别: Git Bash OpenSSH Windows OpenSSH 路径 C:\Program Files\Git\usr\bin\ssh.exe C:\Windows\System32\OpenSSH\ssh.exe 权限检查 使用 POSIX 模拟,较宽松 使用 Windows ACL,严格检查 Windows OpenSSH 发现私钥文件被 CodexSandboxUsers 用户组拥有继承的读取权限((I)(RX)),认为不安全,直接拒绝使用密钥,fallback 到密码认证。 解决方案 在 PowerShell(管理员) 或 CMD 中执行: icacls C:\Users\ranxi\.ssh\id_rsa /inheritance:r /grant ranxi:F /grant SYSTEM:F /grant Administrators:F 这条命令做了三件事: /inheritance:r — 移除继承的权限(去掉 CodexSandboxUsers 等继承来的 ACL) /grant onefly:F — 给当前用户完全控制权 /grant SYSTEM:F /grant Administrators:F — 保留系统和管理员权限 执行后验证: icacls C:\Users\ranxi\.ssh\id_rsa 输出应该只有: C:\Users\ranxi\.ssh\id_rsa BUILTIN\Administrators:(F) NT AUTHORITY\SYSTEM:(F) MSI\ranxi:(F) 没有 CodexSandboxUsers 了,重新连接 VS Code Remote SSH 即可免密登录。 总结 现象:Git Bash SSH 正常,VS Code Remote SSH 要密码 原因:两者用的是不同的 SSH 客户端,Windows OpenSSH 对私钥文件权限检查更严格 修复:移除私钥文件上多余用户组的继承权限