VS Code 使用 OpenSSH 远程连接 Windows 开发完整指南

本文提供了使用 VS Code 通过 OpenSSH 远程连接 Windows 服务器进行开发的详细教程,包括 OpenSSH 服务器安装配置、防火墙设置、VS Code 远程插件配置以及实际开发流程等全方位指南。

一、概述与优势

1. 远程开发的优势

  • 强大服务器性能:利用远程 Windows 服务器的计算资源
  • 统一开发环境:团队成员使用相同的开发环境
  • 灵活办公:可在任意设备上进行开发
  • 数据安全:代码存储在服务器端,本地设备无需保存敏感数据
  • 环境隔离:不同项目可使用独立的开发环境

2. 技术架构

1
2
3
4
5
本地VS Code客户端 ←→ SSH连接 ←→ 远程Windows服务器
↓ ↓
Remote-SSH插件 OpenSSH服务器
↓ ↓
本地编辑界面 实际代码执行环境

二、远程 Windows 服务器准备

1. 安装 OpenSSH 服务器

方法一:通过 Windows 设置(Windows 10/11)

  1. 打开 Windows 设置

    • Win + I 或点击开始菜单 → 设置
  2. 进入应用和功能

    • 点击”应用” → “应用和功能”
    • 点击”可选功能”
  3. 安装 OpenSSH 服务器

    • 点击”添加功能”
    • 搜索”OpenSSH”
    • 选择”OpenSSH 服务器”并点击”安装”

方法二:通过 PowerShell(管理员权限)

1
2
3
4
5
6
7
8
# 检查 OpenSSH 服务器是否已安装
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'

# 安装 OpenSSH 服务器
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

# 安装 OpenSSH 客户端(通常已预安装)
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

方法三:通过 DISM 命令

1
2
# 以管理员身份运行命令提示符
dism /online /add-capability /capabilityname:OpenSSH.Server~~~~0.0.1.0

2. 启动并配置 OpenSSH 服务

启动服务

1
2
3
4
5
6
7
8
# 启动 SSH 服务
Start-Service sshd

# 设置服务自动启动
Set-Service -Name sshd -StartupType 'Automatic'

# 检查服务状态
Get-Service sshd

验证安装

1
2
3
4
5
# 检查 SSH 服务是否正在运行
netstat -an | findstr :22

# 查看 SSH 服务详细状态
sc query sshd

3. 配置 SSH 服务器

编辑配置文件

SSH 配置文件位置:C:\ProgramData\ssh\sshd_config

1
2
3
4
5
# 使用记事本编辑配置文件
notepad C:\ProgramData\ssh\sshd_config

# 或使用其他文本编辑器
code C:\ProgramData\ssh\sshd_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
32
33
34
35
36
37
38
39
# SSH 服务器配置文件示例
# C:\ProgramData\ssh\sshd_config

# 监听端口(默认22,可更改增强安全性)
Port 22
# Port 2222

# 监听地址(0.0.0.0表示监听所有网络接口)
ListenAddress 0.0.0.0

# 允许密码认证
PasswordAuthentication yes

# 允许公钥认证
PubkeyAuthentication yes

# 公钥文件位置
AuthorizedKeysFile .ssh/authorized_keys

# 允许 root 登录(Windows 中对应管理员用户)
PermitRootLogin yes

# 最大认证尝试次数
MaxAuthTries 3

# 空闲超时时间(秒)
ClientAliveInterval 300
ClientAliveCountMax 2

# 禁用 X11 转发(Windows 通常不需要)
X11Forwarding no

# 日志级别
LogLevel INFO

# 特定用户配置示例
# Match User developer
# AllowTcpForwarding yes
# PermitTTY yes

重启 SSH 服务使配置生效

1
2
3
4
5
6
# 重启 SSH 服务
Restart-Service sshd

# 或者
Stop-Service sshd
Start-Service sshd

4. 防火墙配置

Windows Defender 防火墙设置

1
2
3
4
5
6
7
8
# 允许 SSH 通过防火墙(入站规则)
New-NetFirewallRule -DisplayName "OpenSSH-Server-In-TCP" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow

# 查看现有防火墙规则
Get-NetFirewallRule -DisplayName "*SSH*"

# 删除规则(如果需要)
# Remove-NetFirewallRule -DisplayName "OpenSSH-Server-In-TCP"

图形界面配置防火墙

  1. 打开防火墙设置

    • 控制面板 → 系统和安全 → Windows Defender 防火墙
    • 点击”高级设置”
  2. 创建入站规则

    • 右键点击”入站规则” → “新建规则”
    • 选择”端口” → 下一步
    • 选择”TCP”,指定本地端口”22” → 下一步
    • 选择”允许连接” → 下一步
    • 选择应用的网络类型 → 下一步
    • 输入名称”SSH Server” → 完成

5. 用户账户准备

创建开发用户账户

1
2
3
4
5
6
7
8
# 创建新用户
net user developer "StrongPassword123!" /add

# 将用户添加到管理员组(根据需要)
net localgroup administrators developer /add

# 或添加到远程桌面用户组
net localgroup "Remote Desktop Users" developer /add

设置用户主目录权限

1
2
3
4
5
# 确保用户主目录存在
New-Item -ItemType Directory -Path "C:\Users\developer" -Force

# 设置目录权限
icacls "C:\Users\developer" /grant developer:F

三、SSH 密钥认证配置

1. 在本地生成 SSH 密钥对

Windows 本地(PowerShell 或 Git Bash)

1
2
3
4
5
6
7
8
# 生成 RSA 密钥对
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

# 生成 ED25519 密钥对(推荐,更安全)
ssh-keygen -t ed25519 -C "your-email@example.com"

# 指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/windows_server_key -C "windows-server-dev"

执行过程:

1
2
3
4
5
6
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/YourName/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): [输入密码短语,可选]
Enter same passphrase again: [再次输入]
Your identification has been saved in /c/Users/YourName/.ssh/id_ed25519
Your public key has been saved in /c/Users/YourName/.ssh/id_ed25519.pub

2. 将公钥复制到 Windows 服务器

方法一:手动复制

1
2
3
4
# 查看公钥内容
cat ~/.ssh/id_ed25519.pub

# 复制输出的公钥字符串

在 Windows 服务器上:

1
2
3
4
5
6
7
8
9
10
11
# 确保用户 SSH 目录存在
New-Item -ItemType Directory -Path "C:\Users\developer\.ssh" -Force

# 创建 authorized_keys 文件并添加公钥
notepad C:\Users\developer\.ssh\authorized_keys

# 设置正确的权限
icacls "C:\Users\developer\.ssh" /inheritance:r
icacls "C:\Users\developer\.ssh" /grant:r "developer:F"
icacls "C:\Users\developer\.ssh\authorized_keys" /inheritance:r
icacls "C:\Users\developer\.ssh\authorized_keys" /grant:r "developer:R"

方法二:使用 ssh-copy-id(如果可用)

1
2
# 从本地复制公钥到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub developer@your-server-ip

方法三:通过 SCP 复制

1
2
3
4
5
6
7
8
9
10
# 先通过密码登录一次,然后复制公钥
scp ~/.ssh/id_ed25519.pub developer@your-server-ip:~/temp_key.pub

# 在服务器上执行
ssh developer@your-server-ip
mkdir -p ~/.ssh
cat ~/temp_key.pub >> ~/.ssh/authorized_keys
rm ~/temp_key.pub
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

3. 测试 SSH 连接

1
2
3
4
5
6
7
8
9
10
11
# 测试密钥认证
ssh -i ~/.ssh/id_ed25519 developer@your-server-ip

# 测试默认密钥
ssh developer@your-server-ip

# 指定端口
ssh -p 22 developer@your-server-ip

# 详细输出调试信息
ssh -v developer@your-server-ip

四、本地 VS Code 配置

1. 安装必要插件

Remote - SSH 插件

  1. 打开 VS Code

  2. 进入扩展市场

    • 点击左侧活动栏的扩展图标
    • 或使用快捷键 Ctrl + Shift + X
  3. 搜索并安装插件

    • 搜索”Remote - SSH”
    • 安装 Microsoft 发布的”Remote - SSH”插件
    • 建议同时安装”Remote - SSH: Editing Configuration Files”

相关推荐插件

1
2
3
Remote - SSH                    # 核心插件
Remote - SSH: Editing Configuration Files # SSH 配置编辑
Remote Explorer # 远程资源管理器

2. 配置 SSH 连接

打开 SSH 配置文件

  1. 通过命令面板

    • Ctrl + Shift + P
    • 输入”Remote-SSH: Open Configuration File”
    • 选择用户配置文件(通常是 ~/.ssh/config
  2. 直接编辑文件

    1
    2
    3
    # 文件位置
    Windows: C:\Users\YourName\.ssh\config
    macOS/Linux: ~/.ssh/config

SSH 配置文件示例

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
32
33
34
35
36
37
38
39
40
# ~/.ssh/config

# Windows 开发服务器配置
Host windows-dev-server
HostName 192.168.1.100 # 服务器IP地址
User developer # SSH用户名
Port 22 # SSH端口
IdentityFile ~/.ssh/id_ed25519 # 私钥文件路径
ForwardAgent yes # 启用SSH代理转发
ServerAliveInterval 60 # 保持连接活跃
ServerAliveCountMax 3 # 最大重试次数

# 使用密码认证的配置示例
Host windows-dev-password
HostName 192.168.1.101
User developer
Port 22
PreferredAuthentications password
PubkeyAuthentication no

# 使用非标准端口的配置
Host windows-dev-custom
HostName example.com
User developer
Port 2222
IdentityFile ~/.ssh/windows_server_key

# 通过跳板机连接的配置
Host windows-dev-jump
HostName 10.0.1.100
User developer
Port 22
ProxyJump jump-server
IdentityFile ~/.ssh/id_ed25519

Host jump-server
HostName jump.example.com
User jumpuser
Port 22
IdentityFile ~/.ssh/jump_key

3. 建立远程连接

方法一:通过命令面板

  1. 打开命令面板

    • Ctrl + Shift + P
  2. 连接到主机

    • 输入”Remote-SSH: Connect to Host”
    • 选择配置的主机名称(如 windows-dev-server
  3. 选择平台

    • 首次连接会询问远程平台类型
    • 选择”Windows”

方法二:通过远程资源管理器

  1. 打开远程资源管理器

    • 点击左侧活动栏的”远程资源管理器”图标
  2. 添加新连接

    • 点击 SSH TARGETS 旁的”+”图标
    • 输入 SSH 连接字符串:ssh developer@your-server-ip
  3. 连接到主机

    • 右键点击配置的主机
    • 选择”Connect to Host in Current Window”或”Connect to Host in New Window”

方法三:通过状态栏

  1. 点击状态栏左下角

    • VS Code 窗口左下角的绿色按钮
  2. 选择连接选项

    • 在弹出菜单中选择”Connect to Host”

4. 首次连接配置

安装服务器组件

首次连接时,VS Code 会自动在远程服务器上安装必要的组件:

1
2
3
4
[XX:XX:XX.XXX] Installing VS Code Server for x64 (xxxxx)
[XX:XX:XX.XXX] Downloading: 100%
[XX:XX:XX.XXX] Unpacking: 100%
[XX:XX:XX.XXX] VS Code Server for x64 installed

验证连接

  1. 检查状态栏

    • 左下角显示”SSH: windows-dev-server”
  2. 打开终端

    • Ctrl + 或通过菜单”Terminal → New Terminal”
    • 终端应显示远程 Windows 系统信息
  3. 查看文件系统

    • 文件资源管理器显示远程服务器的文件系统

五、远程开发环境配置

1. 安装远程扩展

在远程服务器上安装开发所需的扩展:

常用扩展列表

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
# 编程语言支持
Python # Python 开发
C/C++ # C/C++ 开发
C# Dev Kit # C# 开发
JavaScript (ES6) code snippets # JavaScript 开发
Go # Go 语言开发

# 代码质量
ESLint # JavaScript/TypeScript 代码检查
Pylint # Python 代码检查
Prettier # 代码格式化

# Git 工具
GitLens # Git 增强工具
Git History # Git 历史查看

# 实用工具
Bracket Pair Colorizer # 括号高亮
Auto Rename Tag # HTML/XML 标签重命名
Path Intellisense # 路径自动补全
TODO Highlight # TODO 高亮

# 主题和图标
One Dark Pro # 主题
Material Icon Theme # 图标主题

安装方式

  1. 通过扩展市场

    • 点击左侧的扩展图标
    • 搜索并安装所需扩展
    • 注意:需要在”Install in SSH: windows-dev-server”选项
  2. 批量安装脚本

    1
    2
    3
    4
    5
    # 创建扩展列表文件
    code --list-extensions > extensions.txt

    # 在另一台机器上批量安装
    cat extensions.txt | xargs -n 1 code --install-extension

2. 配置开发环境

配置 Git

1
2
3
4
5
6
7
8
9
# 在远程终端中配置 Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 配置默认编辑器
git config --global core.editor "code --wait"

# 查看配置
git config --list

设置工作区

1
2
3
4
5
6
7
8
9
10
11
12
13
// .vscode/settings.json
{
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"files.encoding": "utf8",
"files.eol": "\n",
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"python.pythonPath": "C:\\Python39\\python.exe",
"go.gopath": "C:\\go",
"java.home": "C:\\Program Files\\Java\\jdk-11.0.2"
}

配置任务和调试

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
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "dotnet",
"args": ["build"],
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
},
{
"label": "run python",
"type": "shell",
"command": "python",
"args": ["${file}"],
"group": "build"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": ".NET Core Launch",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/net5.0/YourApp.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal"
}
]
}

六、实际开发流程示例

1. 创建新项目

Python 项目示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 在远程终端中创建项目目录
mkdir ~/projects/python-web-app
cd ~/projects/python-web-app

# 创建虚拟环境
python -m venv venv

# 激活虚拟环境
.\venv\Scripts\Activate.ps1 # PowerShell
# 或
venv\Scripts\activate.bat # Command Prompt

# 安装依赖
pip install flask requests

# 创建应用文件
code app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# app.py
from flask import Flask, jsonify
import requests

app = Flask(__name__)

@app.route('/')
def hello():
return jsonify({"message": "Hello from remote Windows server!"})

@app.route('/api/data')
def get_data():
# 模拟API调用
response = requests.get('https://api.github.com/users/octocat')
return jsonify(response.json())

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)

.NET 项目示例

1
2
3
4
5
6
# 创建新的 .NET 项目
dotnet new webapi -n WebApiProject
cd WebApiProject

# 运行项目
dotnet run

2. 开发工作流

代码编辑和调试

  1. 文件编辑

    • 直接在 VS Code 中编辑远程文件
    • 支持智能补全、语法高亮等所有本地功能
  2. 断点调试

    • 设置断点:点击行号左侧
    • 启动调试:按 F5 或点击调试按钮
    • 调试控制台显示远程执行结果
  3. 集成终端

    • 终端直接连接到远程服务器
    • 支持 PowerShell、Command Prompt 等

版本控制

1
2
3
4
5
6
7
8
9
10
11
12
13
# 初始化 Git 仓库
git init

# 添加 .gitignore
code .gitignore

# 添加文件并提交
git add .
git commit -m "Initial commit"

# 连接远程仓库
git remote add origin https://github.com/username/project.git
git push -u origin main

端口转发

1
2
3
4
5
# 在 VS Code 中转发端口
# 1. 打开命令面板 (Ctrl+Shift+P)
# 2. 输入 "Remote-SSH: Forward Port from Active Host"
# 3. 输入远程端口号(如 5000)
# 4. 本地访问 localhost:5000

3. 项目部署

构建和测试

1
2
3
4
5
6
7
8
9
10
11
12
# Python 项目
pip install -r requirements.txt
python -m pytest tests/

# .NET 项目
dotnet build
dotnet test

# Node.js 项目
npm install
npm test
npm run build

部署脚本示例

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
# deploy.ps1
param(
[string]$Environment = "development"
)

Write-Host "Deploying to $Environment environment..."

# 停止现有服务
Stop-Process -Name "python" -Force -ErrorAction SilentlyContinue

# 更新代码
git pull origin main

# 安装依赖
pip install -r requirements.txt

# 启动应用
if ($Environment -eq "production") {
# 生产环境使用 gunicorn
Start-Process -FilePath "gunicorn" -ArgumentList "--bind 0.0.0.0:8000 app:app" -NoNewWindow
} else {
# 开发环境
Start-Process -FilePath "python" -ArgumentList "app.py" -NoNewWindow
}

Write-Host "Deployment completed!"

七、高级配置与优化

1. 性能优化

SSH 连接优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ~/.ssh/config 优化配置
Host *
# 连接复用,提高连接速度
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600

# 压缩传输
Compression yes

# 保持连接活跃
ServerAliveInterval 60
ServerAliveCountMax 3

# 禁用不必要的功能
GSSAPIAuthentication no
HashKnownHosts no

VS Code 设置优化

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
{
// 自动保存设置
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,

// 减少网络传输
"remote.SSH.showLoginTerminal": false,
"remote.SSH.useLocalServer": false,

// 排除不必要的文件监控
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/.venv/**": true
},

// 搜索排除
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/.venv": true,
"**/venv": true
}
}

2. 安全性增强

SSH 密钥管理

1
2
3
4
5
6
7
8
# 使用 ssh-agent 管理密钥
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519

# 设置密钥权限
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 700 ~/.ssh

服务器安全配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# C:\ProgramData\ssh\sshd_config 安全配置
# 禁用密码认证(仅使用密钥)
PasswordAuthentication no
ChallengeResponseAuthentication no
PubkeyAuthentication yes

# 限制用户
AllowUsers developer admin

# 更改默认端口
Port 2222

# 限制登录尝试
MaxAuthTries 3
MaxSessions 5

# 禁用空密码
PermitEmptyPasswords no

# 设置登录超时
LoginGraceTime 60

防火墙高级配置

1
2
3
4
5
# 创建更严格的防火墙规则
New-NetFirewallRule -DisplayName "SSH-Specific-IP" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow -RemoteAddress "192.168.1.0/24"

# 启用日志记录
Set-NetFirewallProfile -Profile Domain,Public,Private -LogAllowed True -LogBlocked True -LogFileName "C:\Windows\System32\LogFiles\Firewall\pfirewall.log"

3. 故障排除

常见问题及解决方案

连接被拒绝
1
2
3
4
5
6
7
8
9
10
11
12
# 检查问题的步骤
1. 检查服务器 SSH 服务状态
Get-Service sshd

2. 检查防火墙规则
Get-NetFirewallRule -DisplayName "*SSH*"

3. 检查端口监听
netstat -an | findstr :22

4. 查看 SSH 服务日志
Get-WinEvent -LogName "OpenSSH/Operational" | Select-Object -First 10
认证失败
1
2
3
4
5
# 调试认证问题
ssh -vvv developer@server-ip

# 检查密钥权限
ls -la ~/.ssh/
性能问题
1
2
3
4
5
6
7
8
# 检查网络延迟
ping server-ip

# 测试 SSH 连接速度
ssh -o Compression=no developer@server-ip "dd if=/dev/zero bs=1M count=100" | pv > /dev/null

# 启用压缩测试
ssh -o Compression=yes developer@server-ip "dd if=/dev/zero bs=1M count=100" | pv > /dev/null

日志分析

1
2
3
4
5
6
7
8
9
# 查看 SSH 服务日志
Get-WinEvent -LogName "OpenSSH/Operational" -MaxEvents 50 |
Select-Object TimeCreated, Id, LevelDisplayName, Message |
Format-Table -Wrap

# 查看特定错误
Get-WinEvent -LogName "OpenSSH/Operational" |
Where-Object {$_.LevelDisplayName -eq "Error"} |
Select-Object TimeCreated, Message

4. 自动化脚本

服务器配置脚本

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
32
33
34
# setup-ssh-server.ps1
param(
[string]$Username = "developer",
[string]$Password = "StrongPassword123!",
[int]$SSHPort = 22
)

Write-Host "Setting up SSH server on Windows..."

# 安装 OpenSSH 服务器
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

# 启动并设置自动启动
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

# 创建用户
net user $Username $Password /add
net localgroup administrators $Username /add

# 配置防火墙
New-NetFirewallRule -DisplayName "OpenSSH-Server-In-TCP" -Direction Inbound -Protocol TCP -LocalPort $SSHPort -Action Allow

# 创建 SSH 目录
$userHome = "C:\Users\$Username"
$sshDir = "$userHome\.ssh"
New-Item -ItemType Directory -Path $sshDir -Force

# 设置权限
icacls $sshDir /inheritance:r
icacls $sshDir /grant:r "$Username:F"

Write-Host "SSH server setup completed!"
Write-Host "Connect using: ssh $Username@$env:COMPUTERNAME"

本地连接测试脚本

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
32
33
34
35
36
37
38
39
#!/bin/bash
# test-ssh-connection.sh

SERVER_IP="192.168.1.100"
USERNAME="developer"
SSH_KEY="~/.ssh/id_ed25519"

echo "Testing SSH connection to $USERNAME@$SERVER_IP"

# 测试网络连通性
echo "1. Testing network connectivity..."
if ping -c 4 $SERVER_IP; then
echo "✓ Network connectivity OK"
else
echo "✗ Network connectivity failed"
exit 1
fi

# 测试 SSH 端口
echo "2. Testing SSH port..."
if nc -zv $SERVER_IP 22; then
echo "✓ SSH port accessible"
else
echo "✗ SSH port not accessible"
exit 1
fi

# 测试 SSH 认证
echo "3. Testing SSH authentication..."
if ssh -i $SSH_KEY -o ConnectTimeout=10 -o BatchMode=yes $USERNAME@$SERVER_IP "echo 'SSH connection successful'"; then
echo "✓ SSH authentication successful"
else
echo "✗ SSH authentication failed"
echo "Trying with verbose output..."
ssh -vvv -i $SSH_KEY $USERNAME@$SERVER_IP
exit 1
fi

echo "All tests passed! Ready for VS Code remote development."

八、最佳实践总结

1. 安全最佳实践

  • 使用 SSH 密钥认证:禁用密码认证,仅使用 SSH 密钥
  • 更改默认端口:避免使用标准的 22 端口
  • 限制访问来源:配置防火墙仅允许特定 IP 访问
  • 定期更新系统:保持 Windows 和 OpenSSH 服务器为最新版本
  • 使用强密码:如果必须使用密码,确保使用复杂密码
  • 启用日志记录:监控 SSH 访问日志,及时发现异常

2. 性能最佳实践

  • 使用 SSH 连接复用:通过 ControlMaster 减少连接开销
  • 启用压缩:对于低带宽连接,启用 SSH 压缩
  • 优化 VS Code 设置:排除不必要的文件监控和搜索
  • 使用本地 Git:在服务器端配置 Git,避免频繁的文件传输
  • 合理配置保活参数:防止连接超时断开

3. 开发最佳实践

  • 项目组织:在服务器上创建专门的项目目录结构
  • 环境管理:为不同项目使用独立的虚拟环境
  • 版本控制:合理使用 Git 分支和提交规范
  • 备份策略:定期备份重要的开发数据
  • 团队协作:统一团队的开发环境配置

4. 监控和维护

  • 定期检查服务状态:确保 SSH 服务正常运行
  • 监控系统资源:关注 CPU、内存和磁盘使用情况
  • 日志分析:定期分析 SSH 和应用日志
  • 安全扫描:定期进行安全漏洞扫描
  • 性能调优:根据使用情况优化系统和网络配置

九、常见应用场景

1. 团队协作开发

  • 共享开发环境:团队成员使用统一的服务器环境
  • 代码审查:通过远程访问进行实时代码审查
  • 环境一致性:避免”在我机器上可以运行”的问题

2. 云开发

  • 利用云服务器资源:使用强大的云服务器进行开发
  • 跨地域协作:团队成员在不同地理位置访问同一开发环境
  • 成本控制:按需使用云资源,降低开发成本

3. 学习和实验

  • 隔离环境:在独立环境中学习新技术
  • 实验项目:快速搭建实验环境
  • 技术演示:远程演示技术方案

通过本教程,你应该能够成功配置 VS Code 通过 OpenSSH 远程连接 Windows 服务器进行开发。这种开发模式结合了本地 IDE 的便利性和远程服务器的强大计算能力,是现代软件开发的重要趋势。

记住定期更新和维护你的开发环境,确保安全性和性能。如有任何问题,可以参考官方文档或寻求社区支持。

最后更新: 2025年08月23日 02:16

原始链接: https://goodgan.github.io/2025/08/22/vscode-remote-windows-development/

× 请我吃糖~
打赏二维码