在 Windows 上使用 VSCode SSH 连接 Linux 服务器的完整指南
前言
本文详细介绍如何在 Windows 系统上使用 Visual Studio Code(VSCode)通过 SSH 安全地连接 Linux 服务器。无需每次输入密码,享受本地开发般的流畅体验。
一、环境准备
1.1 软件安装
安装 VSCode
- 访问 VSCode 官网 下载安装
- 安装完成后打开 VSCode
安装必备扩展
在 VSCode 扩展市场搜索并安装:
- Remote - SSH(Microsoft 官方扩展)
- Remote - SSH: Editing Configuration Files
安装 SSH 客户端(二选一)
选项A:Windows 内置 OpenSSH(Windows 10/11)
1 2 3 4 5
| Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
|
选项B:Git for Windows(推荐)
- 访问 Git for Windows 官网 下载
- 安装时注意勾选:
- ✅ Git Bash Here
- ✅ Git GUI Here
- ✅ Use the OpenSSH(重要!)
1.2 验证 SSH 客户端
二、SSH 密钥认证配置
2.1 生成 SSH 密钥对
在 Windows 上生成密钥
1 2 3 4 5
| ssh-keygen -t ed25519 -C "your_email@example.com"
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
| cat ~/.ssh/id_ed25519.pub
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
| 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 首次连接步骤
打开远程连接界面
- 点击 VSCode 左下角绿色远程连接按钮
- 或按
F1 打开命令面板
配置 SSH 主机
1 2 3
| 选择:Remote-SSH: Connect to Host... 然后选择:Configure SSH Hosts... 选择配置文件:C:\Users\你的用户名\.ssh\config
|
选择要连接的主机
- 从列表中选择你在 config 文件中配置的别名(如
myserver)
选择平台类型
1 2 3 4
| Select the platform of the remote host ▸ Linux Windows macOS
|
选择 Linux
等待连接
- 首次连接会安装 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
| Set-Service ssh-agent -StartupType Automatic Start-Service ssh-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
| 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 内置文件管理器
连接成功后,可以使用:
- 资源管理器:直接拖放文件
- 终端:使用
scp 或 rsync 命令
- 扩展:安装 SFTP 扩展获得更强大功能
八、故障排除指南
8.1 连接测试与诊断
基础连接测试
1 2 3 4 5 6 7 8
| Test-NetConnection server_ip -Port 22
ssh -T myserver
ssh -vvv myserver
|
检查各环节
1 2 3 4 5 6 7 8
| Test-Path ~/.ssh/id_ed25519
ssh -G myserver
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
cat ~/.ssh/authorized_keys
|
错误2:Connection timed out
1 2 3 4 5 6 7 8
| Test-NetConnection server_ip -Port 22
ssh username@server_ip "sudo systemctl status sshd"
New-NetFirewallRule -DisplayName "Allow SSH" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow
|
错误3:Bad owner or permissions
1 2 3
| icacls "$env:USERPROFILE\.ssh\id_ed25519" /reset icacls "$env:USERPROFILE\.ssh\id_ed25519" /inheritance:r /grant:r "$env:USERNAME:R"
|
8.3 查看 VSCode 日志
- 命令面板:
Remote-SSH: Open SSH Log
- 输出面板:选择 “Remote - SSH”
- 查看详细错误信息
九、安全最佳实践
9.1 服务器安全加固
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| adduser yourusername usermod -aG sudo yourusername
sudo nano /etc/ssh/sshd_config
sudo systemctl restart sshd
|
9.2 客户端安全建议
- 密钥加密:生成密钥时设置强密码
- 定期轮换:每 6-12 个月更换密钥
- 密钥分类:不同服务器使用不同密钥
- 备份密钥:安全备份
.ssh 文件夹
- 禁用密码登录:服务器配置
PasswordAuthentication no
十、实用技巧与优化
10.1 快捷连接方式
1 2 3 4
|
@echo off start "" "C:\Users\你的用户名\AppData\Local\Programs\Microsoft VS Code\Code.exe" --remote ssh-remote+myserver
|
10.2 同步设置与扩展
- 安装 “Settings Sync” 扩展
- 登录 GitHub/Gitee 账号
- 同步:扩展、设置、快捷键
10.3 自定义连接命令
1 2 3 4 5 6 7 8
| { "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! 🚀