VS Code 远程开发环境配置完全指南

VS Code 远程开发环境配置完全指南

Someone Lv5

Visual Studio Code 的远程开发扩展套件(Remote Development)让开发者可以在本地 VS Code 中无缝连接远程服务器、容器或 WSL 环境进行开发。本文将系统介绍三种远程开发方案的配置方法与最佳实践。

一、远程开发概述

VS Code Remote Development 由三个核心扩展组成:

  • Remote - SSH:通过 SSH 连接到远程服务器或虚拟机
  • Dev Containers:使用 Docker 容器作为完整的开发环境
  • Remote - WSL:在 Windows 的 WSL 子系统中直接开发

这些方案的核心优势是:本地只运行 VS Code 客户端,所有代码编辑、编译、调试和运行都在远程环境中执行,实现本地体验、远程运行

二、Remote - SSH 配置

2.1 安装扩展

在 VS Code 扩展市场搜索并安装 Remote - SSH 扩展(ms-vscode-remote.remote-ssh)。

2.2 配置 SSH 连接

确保本地已配置 SSH 密钥对并已将公钥添加到远程服务器。VS Code 会自动读取 ~/.ssh/config 文件中的主机配置。

典型的 SSH Config 配置:

1
2
3
4
5
6
7
Host my-server
HostName 192.168.1.100
User ubuntu
Port 22
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
ServerAliveCountMax 3

各配置项说明:

配置项说明推荐值
Host连接别名,用于快速识别语义化的名称
HostName服务器 IP 地址或域名服务器公网 IP
User登录用户名root / ubuntu / admin
PortSSH 端口22(或自定义端口)
IdentityFile私钥文件路径~/.ssh/id_ed25519
ServerAliveInterval心跳间隔(秒)60
ServerAliveCountMax心跳失败最大次数3

2.3 连接远程服务器

  1. 点击 VS Code 左下角绿色 >< 图标,选择 Connect to Host…
  2. 从列表中选择已配置的 SSH 主机
  3. VS Code 会在新窗口中打开,自动安装远程端依赖的服务
  4. 连接成功后,左下角显示 SSH: my-server

2.4 端口转发

远程开发时,如果需要访问远程服务器上运行的 Web 服务(如 3000 端口的 Node.js 应用),可以配置端口转发:

1
2
3
# 在 VS Code 命令面板中
> Forward a Port
# 输入远程端口号(如 3000)

VS Code 会自动将远程端口映射到本地 localhost:3000,并支持通过 VS Code 内置浏览器直接打开。

三、Dev Containers 配置

3.1 前置条件

  • 安装 Docker Desktop(Windows/macOS)或 Docker Engine(Linux)
  • 安装 Dev Containers 扩展(ms-vscode-remote.remote-containers

3.2 创建开发容器配置

在项目根目录创建 .devcontainer/devcontainer.json 文件:

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
{
"name": "My Dev Container",
"image": "mcr.microsoft.com/devcontainers/base:ubuntu-22.04",
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "20"
},
"ghcr.io/devcontainers/features/python:1": {
"version": "3.11"
}
},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"ms-python.python",
"GitHub.copilot"
],
"settings": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
},
"postCreateCommand": "npm install",
"forwardPorts": [3000, 8080],
"remoteUser": "vscode"
}

3.3 使用 Dockerfile 自定义容器

如果需要更精细的环境控制,可以使用 Dockerfile:

.devcontainer/Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y \
curl \
git \
vim \
htop \
build-essential \
&& rm -rf /var/lib/apt/lists/*

# 安装 Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*

# 安装 Python
RUN apt-get update && apt-get install -y python3 python3-pip

WORKDIR /workspace

.devcontainer/devcontainer.json

1
2
3
4
5
6
7
8
9
10
11
{
"name": "Custom Container",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": ["ms-python.python", "dbaeumer.vscode-eslint"]
}
}
}

3.4 在容器中打开项目

  1. 在 VS Code 中打开项目文件夹
  2. 左下角点击 >< 图标,选择 Reopen in Container
  3. 首次打开需要拉取镜像和安装配置,可能需要几分钟
  4. 完成后,左下角显示 Dev Container: My Dev Container

3.5 Docker Compose 多容器方案

对于需要数据库等依赖服务的项目,可以使用 Docker Compose:

.devcontainer/docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ..:/workspace:cached
command: sleep infinity
ports:
- "3000:3000"
depends_on:
- db

db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
ports:
- "5432:5432"

.devcontainer/devcontainer.json

1
2
3
4
5
6
{
"name": "Full Stack Dev",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace"
}

四、Remote - WSL 配置

4.1 安装 WSL

在 Windows 上以管理员身份打开 PowerShell 执行:

1
2
3
4
5
6
7
8
# 安装 WSL 2 和默认 Linux 发行版
wsl --install

# 或者指定发行版
wsl --install -d Ubuntu-22.04

# 查看已安装的发行版
wsl --list --verbose

4.2 安装 WSL 扩展

安装 Remote - WSL 扩展(ms-vscode-remote.remote-wsl)。

4.3 在 WSL 中打开项目

1
2
3
4
5
6
# 方式一:在 WSL 终端中
cd /home/username/my-project
code .

# 方式二:在 VS Code 中
# 左下角 >< → Open Folder in WSL

VS Code 会自动在 WSL 环境中启动服务器,所有的终端命令、代码运行都在 WSL Linux 内核中执行。

五、常见问题与故障排查

5.1 SSH 连接超时

1
2
3
4
5
6
7
8
9
10
# 检查 SSH 是否可连接
ssh -vvv my-server

# 检查服务器 SSH 服务状态(在服务器上)
sudo systemctl status sshd
sudo systemctl restart sshd

# 检查防火墙(在服务器上)
sudo ufw status
sudo ufw allow 22/tcp

5.2 Dev Containers 权限问题

1
2
3
4
5
6
7
# 挂载卷权限修复
# 在 devcontainer.json 中添加
"remoteUser": "vscode",
"updateRemoteUserUID": true

# 或修复文件夹权限(在容器启动后)
sudo chown -R vscode:vscode /workspace

5.3 WSL 磁盘空间不足

1
2
3
4
5
6
7
8
9
10
11
# 查看磁盘使用
df -h

# 定位 WSL 虚拟磁盘位置
# 默认在:C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04...
# 使用 diskpart 或第三方工具压缩 vhdx 文件

# 清理 WSL 空间
sudo apt-get clean
sudo journalctl --vacuum-size=100M
docker system prune -a # 如果有 Docker

5.4 扩展在不同环境中的同步

VS Code 允许为每个远程环境单独管理扩展。建议的扩展配置策略:

1
2
3
4
5
6
7
8
9
10
11
// settings.json 中的远程配置
{
"remote.SSH.defaultExtensions": [
"ms-python.python",
"esbenp.prettier-vscode"
],
"remote.containers.defaultExtensions": [
"ms-python.python",
"dbaeumer.vscode-eslint"
]
}

六、最佳实践建议

  1. 选择适合的方案

    • 团队协作项目 → Dev Containers(环境一致)
    • 已有云服务器 → Remote SSH
    • Windows 环境开发 Linux 应用 → Remote WSL
  2. 安全性

    • SSH 使用密钥认证,禁用密码登录
    • Dev Containers 不要将 SSH 私钥打包到镜像中
    • 定期更新 Docker 镜像和服务器系统
  3. 性能优化

    • 避免在远程打开大量文件
    • 使用 .gitignore 排除 node_modules 等无需同步的目录
    • Remote SSH 可配置 "remote.SSH.showLoginTerminal": true 查看连接日志
  4. 配置版本化

    • .devcontainer/ 目录纳入版本管理
    • SSH Config 文件建议备份到安全位置
    • WSL 发行版可使用 wsl --export 备份

本文由AI辅助生成,内容仅供参考

  • 标题: VS Code 远程开发环境配置完全指南
  • 作者: Someone
  • 创建于 : 2026-06-07 21:41:00
  • 更新于 : 2026-06-18 08:39:57
  • 链接: https://demo-blog.qusite.cn/2026-06-07-vscode-remote-dev-guide/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。