在 Windows 上使用 VSCode SSH 连接 Linux 服务器的完整指南

在 Windows 上使用 VSCode SSH 连接 Linux 服务器的完整指南

前言

本文详细介绍如何在 Windows 系统上使用 Visual Studio Code(VSCode)通过 SSH 安全地连接 Linux 服务器。无需每次输入密码,享受本地开发般的流畅体验。

一、环境准备

1.1 软件安装

安装 VSCode

  1. 访问 VSCode 官网 下载安装
  2. 安装完成后打开 VSCode

安装必备扩展

在 VSCode 扩展市场搜索并安装:

  • Remote - SSH(Microsoft 官方扩展)
  • Remote - SSH: Editing Configuration Files

安装 SSH 客户端(二选一)

选项A:Windows 内置 OpenSSH(Windows 10/11)

1
2
3
4
5
# 以管理员身份打开 PowerShell
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'

# 如果未安装,执行:
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

选项B:Git for Windows(推荐)

  1. 访问 Git for Windows 官网 下载
  2. 安装时注意勾选:
    • ✅ Git Bash Here
    • ✅ Git GUI Here
    • Use the OpenSSH(重要!)

1.2 验证 SSH 客户端

1
2
3
# 在 PowerShell 中运行
ssh -V
# 应该显示类似:OpenSSH_8.6p1, OpenSSL 1.1.1k 25 Mar 2021

二、SSH 密钥认证配置

2.1 生成 SSH 密钥对

在 Windows 上生成密钥

1
2
3
4
5
# 打开 PowerShell
ssh-keygen -t ed25519 -C "your_email@example.com"

# 或者使用 RSA(兼容性更好)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

生成过程中会询问:

  • 保存路径:按 Enter 使用默认路径 C:\Users\你的用户名\.ssh\id_ed25519
  • 密码短语:可选,设置后增加安全性

2.2 复制公钥到 Linux 服务器

方法1:使用 ssh-copy-id(最简单)

1
2
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
# 输入服务器密码

方法2:手动复制

1
2
3
4
5
6
7
# 1. 显示公钥内容
cat ~/.ssh/id_ed25519.pub

# 2. 复制输出的全部内容

# 3. 登录服务器并添加公钥
ssh username@server_ip "mkdir -p ~/.ssh && echo '粘贴的公钥内容' >> ~/.ssh/authorized_keys"

2.3 配置服务器端权限

1
2
3
# 登录服务器执行
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

三、SSH 配置文件设置

3.1 创建 SSH 配置文件

在 Windows 上创建 C:\Users\你的用户名\.ssh\config 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 单个服务器配置示例
Host myserver # 自定义别名
HostName 192.168.1.100 # 服务器 IP 或域名
User username # 登录用户名
Port 22 # SSH 端口,默认 22
IdentityFile ~/.ssh/id_ed25519 # 私钥路径
ServerAliveInterval 60 # 每60秒发送保活包
ServerAliveCountMax 3 # 最多重试3次

# 多个服务器配置示例
Host web-server
HostName web.example.com
User admin
IdentityFile ~/.ssh/id_rsa_web

Host db-server
HostName db.example.com
User dba
Port 2222
IdentityFile ~/.ssh/id_rsa_db

# 跳板机配置(通过跳板连接内网服务器)
Host internal-server
HostName 10.0.0.100
User internal-user
ProxyJump jumpserver

Host jumpserver
HostName jump.example.com
User jump-user
IdentityFile ~/.ssh/id_rsa_jump

3.2 设置文件权限

1
2
3
# 在 PowerShell 中设置正确权限
icacls "$env:USERPROFILE\.ssh" /inheritance:r /grant:r "$env:USERNAME:F"
icacls "$env:USERPROFILE\.ssh\id_ed25519" /inheritance:r /grant:r "$env:USERNAME:R"

四、VSCode 连接配置

4.1 首次连接步骤

  1. 打开远程连接界面

    • 点击 VSCode 左下角绿色远程连接按钮
    • 或按 F1 打开命令面板
  2. 配置 SSH 主机

    1
    2
    3
    选择:Remote-SSH: Connect to Host...
    然后选择:Configure SSH Hosts...
    选择配置文件:C:\Users\你的用户名\.ssh\config
  3. 选择要连接的主机

    • 从列表中选择你在 config 文件中配置的别名(如 myserver
  4. 选择平台类型

    1
    2
    3
    4
    Select the platform of the remote host
    ▸ Linux
    Windows
    macOS

    选择 Linux

  5. 等待连接

    • 首次连接会安装 VS Code Server
    • 连接成功后,左下角显示 “SSH: myserver”

4.2 VSCode 设置优化

Ctrl+, 打开设置,搜索 “ssh”,建议配置:

1
2
3
4
5
6
7
8
9
10
{
"remote.SSH.path": "C:\\Windows\\System32\\OpenSSH\\ssh.exe",
"remote.SSH.configFile": "C:\\Users\\你的用户名\\.ssh\\config",
"remote.SSH.showLoginTerminal": true,
"remote.SSH.useLocalServer": false,
"remote.SSH.remotePlatform": {
"myserver": "linux",
"web-server": "linux"
}
}

4.3 使用 Git Bash 的 SSH

如果使用 Git Bash 的 SSH 客户端:

1
2
3
{
"remote.SSH.path": "C:\\Program Files\\Git\\usr\\bin\\ssh.exe"
}

五、SSH Agent 配置(免密码输入)

5.1 启用 SSH Agent 服务

1
2
3
4
5
6
7
# Windows 设置 SSH Agent 自动启动
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent

# 添加密钥到 Agent
ssh-add ~/.ssh/id_ed25519
# 输入密钥密码(如果设置了)

5.2 验证 Agent 运行

1
2
3
4
5
# 查看已加载的密钥
ssh-add -l

# 测试连接(应该无需输入密码)
ssh myserver

5.3 自动化脚本

创建启动脚本 C:\Users\你的用户名\ssh-startup.ps1

1
2
3
4
5
# 启动 SSH Agent 并添加密钥
if ((Get-Service ssh-agent).Status -ne 'Running') {
Start-Service ssh-agent
}
ssh-add ~/.ssh/id_ed25519

添加到计划任务实现开机自启。

六、高级功能配置

6.1 端口转发

1
2
3
4
5
6
Host dev-server
HostName dev.example.com
User dev
LocalForward 8888 localhost:8888 # Jupyter Notebook
LocalForward 3306 localhost:3306 # MySQL
LocalForward 8080 localhost:80 # Web 服务器

6.2 多因素认证支持

1
2
3
4
5
Host secure-server
HostName secure.example.com
User admin
IdentityFile ~/.ssh/id_ed25519
PreferredAuthentications publickey,keyboard-interactive

6.3 连接复用(加快后续连接)

1
2
3
4
Host *
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
ControlPersist 10m

七、文件传输与同步

7.1 使用 SCP 传输文件

1
2
3
4
5
6
7
8
# 上传文件到服务器
scp local_file.txt username@server_ip:/remote/path/

# 下载文件到本地
scp username@server_ip:/remote/file.txt ./

# 传输整个文件夹
scp -r local_folder username@server_ip:/remote/path/

7.2 VSCode 内置文件管理器

连接成功后,可以使用:

  • 资源管理器:直接拖放文件
  • 终端:使用 scprsync 命令
  • 扩展:安装 SFTP 扩展获得更强大功能

八、故障排除指南

8.1 连接测试与诊断

基础连接测试

1
2
3
4
5
6
7
8
# 测试网络连通性
Test-NetConnection server_ip -Port 22

# 测试 SSH 连接
ssh -T myserver

# 详细模式查看错误
ssh -vvv myserver

检查各环节

1
2
3
4
5
6
7
8
# 1. 检查密钥是否存在
Test-Path ~/.ssh/id_ed25519

# 2. 检查配置语法
ssh -G myserver

# 3. 检查服务器 SSH 服务
ssh username@server_ip "systemctl status sshd"

8.2 常见错误及解决方案

错误1:Permission denied (publickey)

1
2
3
4
5
6
7
8
# 服务器端检查
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519

# 检查 authorized_keys 格式
cat ~/.ssh/authorized_keys
# 确保每行一个公钥,无多余空格

错误2:Connection timed out

1
2
3
4
5
6
7
8
# 检查防火墙
Test-NetConnection server_ip -Port 22

# 检查服务器 SSH 配置
ssh username@server_ip "sudo systemctl status sshd"

# 临时关闭 Windows 防火墙测试
New-NetFirewallRule -DisplayName "Allow SSH" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow

错误3:Bad owner or permissions

1
2
3
# Windows 权限修复
icacls "$env:USERPROFILE\.ssh\id_ed25519" /reset
icacls "$env:USERPROFILE\.ssh\id_ed25519" /inheritance:r /grant:r "$env:USERNAME:R"

8.3 查看 VSCode 日志

  1. 命令面板:Remote-SSH: Open SSH Log
  2. 输出面板:选择 “Remote - SSH”
  3. 查看详细错误信息

九、安全最佳实践

9.1 服务器安全加固

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 创建普通用户,避免使用 root
adduser yourusername
usermod -aG sudo yourusername

# 2. 禁用 root SSH 登录
sudo nano /etc/ssh/sshd_config
# 修改:PermitRootLogin no

# 3. 使用非标准端口
# 修改:Port 22222

# 4. 限制用户登录
# 添加:AllowUsers yourusername

# 5. 重启 SSH 服务
sudo systemctl restart sshd

9.2 客户端安全建议

  1. 密钥加密:生成密钥时设置强密码
  2. 定期轮换:每 6-12 个月更换密钥
  3. 密钥分类:不同服务器使用不同密钥
  4. 备份密钥:安全备份 .ssh 文件夹
  5. 禁用密码登录:服务器配置 PasswordAuthentication no

十、实用技巧与优化

10.1 快捷连接方式

1
2
3
4
# 创建桌面快捷方式
# vscode-ssh.bat
@echo off
start "" "C:\Users\你的用户名\AppData\Local\Programs\Microsoft VS Code\Code.exe" --remote ssh-remote+myserver

10.2 同步设置与扩展

  1. 安装 “Settings Sync” 扩展
  2. 登录 GitHub/Gitee 账号
  3. 同步:扩展、设置、快捷键

10.3 自定义连接命令

1
2
3
4
5
6
7
8
// VSCode 快捷键绑定
{
"key": "ctrl+alt+s",
"command": "remote-ssh.connect",
"args": {
"host": "myserver"
}
}

十一、性能优化建议

11.1 连接优化配置

1
2
3
4
5
Host *
TCPKeepAlive yes
Compression yes
IPQoS throughput
ConnectTimeout 30

11.2 VSCode 设置优化

1
2
3
4
5
6
7
{
"remote.SSH.localServerDownload": "always",
"remote.SSH.allowLocalServerDownload": true,
"remote.SSH.enableDynamicForwarding": false,
"files.trimTrailingWhitespace": true,
"editor.formatOnSave": true
}

结语

通过本文的详细配置,你可以在 Windows 上实现 VSCode 与 Linux 服务器的无缝 SSH 连接。这种配置不仅提升了开发效率,还确保了连接的安全性。

记住定期检查更新 SSH 密钥,监控连接日志,确保开发环境的安全稳定。如果在配置过程中遇到问题,建议按照故障排除章节逐步检查,或查看 VSCode 的 SSH 输出日志获取详细信息。

Happy coding! 🚀



在 Windows 上使用 VSCode SSH 连接 Linux 服务器的完整指南
https://www.psnow.sbs/2025/12/21/在-Windows-上使用-VSCode-SSH-连接-Linux-服务器的完整指南/
作者
Psnow
发布于
2025年12月22日
许可协议