惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
K
Kaspersky official blog
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
博客园 - Franky
T
Tenable Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
J
Java Code Geeks
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
O
OpenAI News
The Cloudflare Blog
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
V
V2EX
罗磊的独立博客
美团技术团队
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏

阿尔的代码屋 | 全栈技术笔记

在 Android Termux 环境下安装 Hermes Agent 的踩坑与完美解决实践 开发日志| 阿尔的代码屋 VS Code 连接 WSL 精确每 10 分钟掉线 排坑笔记 | 阿尔的代码屋 Android 模拟器代理联网与 No Internet WiFi 锁死排坑笔记 | 阿尔的代码屋 [object Object] typing_extensions 有用(四):使用 TypeIs 替代危险的 cast,做最严谨的类型收窄 | 阿尔的代码屋 GoRouter 结合 Isar 运行 Widget 测试并发/粘性线程死锁卡死排坑笔记 | 阿尔的代码屋 typing_extensions 有用(三):使用 Unpack 结合 TypedDict 给 **kwargs 装上透视眼 | 阿尔的代码屋 typing_extensions 有用(二):使用 @override 打造重构代码时的“防呆神器” | 阿尔的代码屋 Flutter 并发测试踩坑实录 - IsarCore 动态库下载冲突与 Widget 测试 HTTP 拦截全链路解决 | 阿尔的代码屋 typing_extensions 有用(一):使用 Self 终结继承时的类型推断灾难 | 阿尔的代码屋 基于 Cloudflare Pages 的纯前端 WebAssembly 应用自动化部署实践 | 开发日志 | 阿尔的代码屋 基于 VS Code 远程开发的 GPU Docker 容器自动清理方案实践 开发日志| 阿尔的代码屋 Patrol iOS 集成测试排坑实录 - xcodebuild exit code 70 全链路解决 | 阿尔的代码屋 Flutter E2E 测试从 integration_test 迁移到 Patrol - 实践笔记 | 阿尔的代码屋 Linux/macOS 下 micromamba 报错 Shard Index not available 与极度卡顿 排坑笔记 | 阿尔的代码屋 critical libmamba Shell not initialized micromamba报错 subprocess 无法修改父 Shell - 排坑笔记 | 阿尔的代码屋 Git 报警: inexact rename detection was skipped - 排坑笔记 | 阿尔的代码屋 解决群晖 NAS SSL 证书自动部署难题 | 阿尔的代码屋 Windows 下 Git error: fatal: refusing to work with credential missing host field - 排坑笔记 | 阿尔的代码屋 VS Code 调试 Python 第三方库源码的配置 - 开发技巧| 阿尔的代码屋 VS Code Remote Tunnels本地连接调试Kaggle GPU - 开发技巧| 阿尔的代码屋 Windows下Git error: open(xxxx) Filename too long - 排坑笔记 | 阿尔的代码屋 记录2026年零预约港卡开户实录避坑 告别孤岛:拥抱 MCP 协议,为大模型打造标准“USB 接口” - 大模型实战 08 完结篇 | 阿尔的代码屋 从 ReAct 到 Workflow:基于云端 API 构建事件驱动的智能体 - 大模型实战 07 额外篇 | 阿尔的代码屋 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent - 大模型实战 07 | 阿尔的代码屋 在 Kaggle 上用 Unsloth 极速微调 Qwen3 - 大模型实战 06 | 阿尔的代码屋 大模型微调实战详解 - 大模型实战 05 | 阿尔的代码屋 基于 ChromaDB 打造工程级 RAG 系统 - 大模型实战 04 | 阿尔的代码屋 拆解 Transformers 原理与图解 - 大模型实战 03 | 阿尔的代码屋 云端炼丹房 2:Kaggle 上手指南 - 大模型实战 03预备 | 阿尔的代码屋 云端炼丹房:Google Colab 免费 GPU 上手指南 - 大模型实战 03预备 | 阿尔的代码屋 Open WebUI 部署与 RAG 实战 - 大模型实战 02 | 阿尔的代码屋
VS Code Remote-SSH: 彻底告别重复输入密码 - 排坑笔记 | 阿尔的代码屋
Algieba · 2026-05-19 · via 阿尔的代码屋 | 全栈技术笔记

核心摘要 (TL;DR)

  • 问题现象:在 VS Code 中使用 Remote - SSH 插件连接服务器,或打开新终端时,系统频繁弹窗要求输入远程主机的登录密码。
  • 根本原因:SSH 客户端与服务端之间未建立公钥信任机制,导致鉴权退化为最基础的密码验证交互。
  • 极速解法:本地生成密钥对 -> 使用 ssh-copy-id 自动写入公钥到服务器 -> 在本地 .ssh/config 绑定 IdentityFile

问题概览卡片

基本信息

  • 问题分类:SSH 鉴权配置 / 开发流优化
  • 环境说明:任意支持 OpenSSH 的本地终端(如 Git Bash, macOS Terminal)及远程 Linux 服务器
  • 触发条件:新环境初始化、首次配置 VS Code 远程工作区。
  • 核心目标:建立非对称加密信任关系,实现 VS Code 丝滑直连。

现场执行日志(以 Git Bash 部署为例)

以下是使用 ssh-copy-id 命令将本地公钥推送至远程主机的标准中间过程证明(已脱敏):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ssh-copy-id -i ~/.ssh/id_rsa.pub your_username@your_remote_host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/your_username/.ssh/id_rsa.pub"
The authenticity of host 'your_remote_host (10.x.x.x)' can't be established.
ED25519 key fingerprint is SHA256:abcdfssxxxxxxxxxxxxxxxxxAA.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:34: your_remote_host.domain.net
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
your_username@your_remote_host's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'your_username@your_remote_host'"
and check to make sure that only the key(s) you wanted were added.

1. 现象描述与痛点还原

VS Code 的 Remote 架构会在后台建立多个 SSH 连接(用于文件同步、终端进程、插件扩展等)。如果你只依赖传统的密码认证,由于 VS Code 本身并不缓存 SSH 密码,就会导致每一次新建底层连接时,顶部命令面板都会强制弹出密码输入框。不仅打断开发心流,一旦输错还需要重新走一遍加载流程。

2. 原理解析与根本原因

  1. 鉴权机制降级:SSH 协议优先尝试基于密钥(Key-based)的认证。当找不到有效私钥,或远程服务器没有录入对应的公钥时,系统就会降级到键盘交互式的密码认证(Password-based)。
  2. 首次连接的安全屏障:从上方日志可以看出,首次连接某台主机时,SSH 会触发 authenticity can't be established 的警告。这是防范中间人攻击的安全机制,必须手动输入 yes 将该主机的指纹记录到本地的 known_hosts 文件中,随后再输入密码,才能完成公钥的下发。

3. 标准化解决方案

步骤一:本地生成密钥对(若已有可跳过)

打开本地终端(Windows 用户请务必使用 Git Bash 或 PowerShell),执行生成命令:

1
2
3
4

ssh-keygen -t ed25519


步骤二:向远程服务器签发公钥

利用日志中展示的 ssh-copy-id 工具,将本地公钥安全地追加到服务器的授权列表中:

1
2

ssh-copy-id -i ~/.ssh/id_ed25519.pub your_username@your_remote_host

输入最后一次密码后,看到提示 Number of key(s) added: 1 即表示公钥已成功入驻远程服务器。

步骤三:修改 VS Code 的 SSH 配置文件

告知 VS Code 应该使用哪把“钥匙”去开门。

  1. 在 VS Code 按 Ctrl+Shift+P (Mac 为 Cmd+Shift+P)。
  2. 搜索并点击 Remote-SSH: Open SSH Configuration File...
  3. ~/.ssh/config 中为你配置的主机添加 IdentityFile 字段:
1
2
3
4
5
Host my-dev-server
HostName 10.x.x.x
User your_username
Port 22
IdentityFile ~/.ssh/id_ed25519

配置保存后,点击 VS Code 的连接按钮,即可全程无感直达远程工作区。


4. 常见排坑与预防建议

  • 严格的权限控制(最容易踩的坑)
    SSH 守护进程(sshd)对存放公钥的文件权限要求极其苛刻。如果服务器端的目录权限过大,出于安全考虑,免密登录会直接失效。请确保远程主机执行了以下授权:

    1
    2
    3
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

1
2
- **配置覆盖问题**:
如果你的电脑上配置了多个 Git 账号或多把私钥,一定要在 `~/.ssh/config` 中通过不同的 `Host` 节点将 `IdentityFile` 隔离开来,避免密钥串联导致的认证失败。