VS Code Server远程隧道连接频繁断开问题求助
VS Code Remote Tunnel连接GCP服务器主动断开问题解决方案
问题场景
本地通过VS Code Remote Tunnel连接GCP云服务器上的VS Code Server,连接维持约1小时后会被主动断开,导致未保存工作丢失,需重新发起连接,影响工作效率。
服务器断开时日志
(conda)jupyter@splintered-learning:~$ code-server (conda)jupyter@splintered-learning:~$ code-server Open this link in your browser https://insiders.vscode.dev/+ms-vscode.remote-server/********** [2023-03-14 22:49:08] info [rpc.0] [call.0] Checking /home/jupyter/.vscode-cli/server-stable/.5e805b79fcb6ba4c2d23712967df89a089da575b.log and /home/jupyter/.vscode-cli/server-stable/.5e805b79fcb6ba4c2d23712967df89a089da575b.pid for a running server... [2023-03-14 22:49:08] info [rpc.0] [call.0] Downloading VS Code server https://az764295.vo.msecnd.net/stable/5e805b79fcb6ba4c2d23712967df89a089da575b/vscode-server-linux-x64.tar.gz -> /home/jupyter/.vscode-cli/server-stable/bin/5e805b79fcb6ba4c2d23712967df89a089da575b [2023-03-14 22:49:09] info [rpc.0] [call.0] Setting up server... [2023-03-14 22:49:11] info [rpc.0] [call.0] Starting server... [2023-03-14 22:49:11] info [rpc.0] [call.0] Server started [2023-03-14 22:49:35] info [rpc.0] Forwarding port 8050 [2023-03-14 22:49:35] info [rpc.0] Forwarding port 8052 [2023-03-14 23:49:09] info [rpc.0] Disposed of connection to running server.
解决方案
1. 调整SSH会话超时参数
GCP服务器默认SSH配置可能主动断开闲置会话,修改sshd_config延长超时:
- 编辑
/etc/ssh/sshd_config,添加或修改:
(每5分钟发送心跳包,连续12次无响应才断开,允许1小时无操作)ClientAliveInterval 300 ClientAliveCountMax 12 - 重启SSH服务:
sudo systemctl restart sshd
2. 将VS Code Server配置为后台服务
用systemd管理进程,避免终端会话结束导致进程终止:
- 创建服务文件:
sudo nano /etc/systemd/system/code-server.service - 写入以下内容(替换用户名为你的GCP服务器用户名):
[Unit] Description=VS Code Server After=network.target [Service] User=jupyter ExecStart=/home/jupyter/.vscode-cli/server-stable/bin/5e805b79fcb6ba4c2d23712967df89a089da575b/bin/code-server Restart=always RestartSec=10 [Install] WantedBy=multi-user.target - 启用并启动服务:
sudo systemctl daemon-reload sudo systemctl enable code-server sudo systemctl start code-server
3. 确保本地VS Code与Server版本匹配
日志中Server版本为5e805b79fcb6ba4c2d23712967df89a089da575b:
- 本地VS Code按
Ctrl+Shift+P输入About Visual Studio Code查看版本 - 版本不匹配时,更新本地VS Code或重新安装对应版本的Server
4. 检查GCP防火墙与网络设置
- 确认GCP防火墙规则未主动切断长连接(默认无需额外开放端口,重点排查闲置连接清理规则)
- 若本地用代理或VPN,检查其连接超时限制并调整
5. 启用VS Code自动保存与工作区恢复
减少断开时的工作丢失:
- 打开VS Code设置,搜索
Auto Save,设置为afterDelay并调整延迟(如10秒) - 搜索
Window: Restore Windows,设置为all,确保重启连接后自动恢复工作窗口
内容的提问来源于stack exchange,提问作者exan
相关产品推荐
相关产品推荐

