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)
打开 Windows 设置
进入应用和功能
点击”应用” → “应用和功能”
点击”可选功能”
安装 OpenSSH 服务器
点击”添加功能”
搜索”OpenSSH”
选择”OpenSSH 服务器”并点击”安装”
方法二:通过 PowerShell(管理员权限) 1 2 3 4 5 6 7 8 Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0 .1.0 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 Start-Service sshdSet-Service -Name sshd -StartupType 'Automatic' Get-Service sshd
验证安装 1 2 3 4 5 netstat -an | findstr :22 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 Port 22 ListenAddress 0.0.0.0 PasswordAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PermitRootLogin yes MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 X11Forwarding no LogLevel INFO
重启 SSH 服务使配置生效 1 2 3 4 5 6 Restart-Service sshdStop-Service sshdStart-Service sshd
4. 防火墙配置 Windows Defender 防火墙设置 1 2 3 4 5 6 7 8 New-NetFirewallRule -DisplayName "OpenSSH-Server-In-TCP" -Direction Inbound -Protocol TCP -LocalPort 22 -Action AllowGet-NetFirewallRule -DisplayName "*SSH*"
图形界面配置防火墙
打开防火墙设置
控制面板 → 系统和安全 → Windows Defender 防火墙
点击”高级设置”
创建入站规则
右键点击”入站规则” → “新建规则”
选择”端口” → 下一步
选择”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 ssh-keygen -t rsa -b 4096 -C "your-email@example.com" 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 New-Item -ItemType Directory -Path "C:\Users\developer\.ssh" -Force 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 ~/.sshcat ~/temp_key.pub >> ~/.ssh/authorized_keysrm ~/temp_key.pubchmod 700 ~/.sshchmod 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 插件
打开 VS Code
进入扩展市场
点击左侧活动栏的扩展图标
或使用快捷键 Ctrl + Shift + X
搜索并安装插件
搜索”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 配置文件
通过命令面板
按 Ctrl + Shift + P
输入”Remote-SSH: Open Configuration File”
选择用户配置文件(通常是 ~/.ssh/config)
直接编辑文件
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 Host windows-dev-server HostName 192.168.1.100 User developer Port 22 IdentityFile ~/.ssh/id_ed25519 ForwardAgent yes 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. 建立远程连接 方法一:通过命令面板
打开命令面板
连接到主机
输入”Remote-SSH: Connect to Host”
选择配置的主机名称(如 windows-dev-server)
选择平台
首次连接会询问远程平台类型
选择”Windows”
方法二:通过远程资源管理器
打开远程资源管理器
添加新连接
点击 SSH TARGETS 旁的”+”图标
输入 SSH 连接字符串:ssh developer@your-server-ip
连接到主机
右键点击配置的主机
选择”Connect to Host in Current Window”或”Connect to Host in New Window”
方法三:通过状态栏
点击状态栏左下角
选择连接选项
在弹出菜单中选择”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
验证连接
检查状态栏
左下角显示”SSH: windows-dev-server”
打开终端
按 Ctrl + 或通过菜单”Terminal → New Terminal”
终端应显示远程 Windows 系统信息
查看文件系统
五、远程开发环境配置 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 # 图标主题
安装方式
通过扩展市场
点击左侧的扩展图标
搜索并安装所需扩展
注意:需要在”Install in SSH: windows-dev-server”选项
批量安装脚本
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 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 { "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 { "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 { "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-appcd ~/projects/python-web-apppython -m venv venv .\venv\Scripts\Activate.ps1 venv\Scripts\activate.bat 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 from flask import Flask, jsonifyimport requestsapp = Flask(__name__) @app.route('/' ) def hello (): return jsonify({"message" : "Hello from remote Windows server!" }) @app.route('/api/data' ) def get_data (): 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 dotnet new webapi -n WebApiProject cd WebApiProjectdotnet run
2. 开发工作流 代码编辑和调试
文件编辑
直接在 VS Code 中编辑远程文件
支持智能补全、语法高亮等所有本地功能
断点调试
设置断点:点击行号左侧
启动调试:按 F5 或点击调试按钮
调试控制台显示远程执行结果
集成终端
终端直接连接到远程服务器
支持 PowerShell、Command Prompt 等
版本控制 1 2 3 4 5 6 7 8 9 10 11 12 13 git init code .gitignore git add . git commit -m "Initial commit" git remote add origin https://github.com/username/project.git git push -u origin main
端口转发
3. 项目部署 构建和测试 1 2 3 4 5 6 7 8 9 10 11 12 pip install -r requirements.txt python -m pytest tests/ dotnet build dotnet test 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 param ( [string ]$Environment = "development" ) Write-Host "Deploying to $Environment environment..." Stop-Process -Name "python" -Force -ErrorAction SilentlyContinuegit pull origin main pip install -r requirements.txt if ($Environment -eq "production" ) { 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 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 eval $(ssh-agent -s)ssh-add ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519chmod 644 ~/.ssh/id_ed25519.pubchmod 700 ~/.ssh
服务器安全配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 -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 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 param ( [string ]$Username = "developer" , [string ]$Password = "StrongPassword123!" , [int ]$SSHPort = 22 ) Write-Host "Setting up SSH server on Windows..." Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0 .1.0 Start-Service sshdSet-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$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 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 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 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 的便利性和远程服务器的强大计算能力,是现代软件开发的重要趋势。
记住定期更新和维护你的开发环境,确保安全性和性能。如有任何问题,可以参考官方文档或寻求社区支持。