问题现象
在使用Cursor的Remote-SSH功能连接远程服务器时,经常卡在"Connecting to the remote host (attempt 1)...",长时间无法连接或连接超时。即使SSH基础连接正常,Cursor也无法快速建立远程开发环境。
问题原因分析
通过排查发现,主要原因是:
Cursor需要在远程服务器下载安装
cursor-server组件(约80MB)服务器直接下载速度慢,默认30秒超时容易失败
下载进程卡住,导致连接hang住
文件路径不正确,Cursor找不到已下载的
cursor-server
解决方案
第一步:诊断当前状态
首先检查服务器上是否存在cursor-server:
测试SSH基础连接
ssh root@your-server-ip "echo OK"
查看cursor-server目录
ssh root@your-server-ip "ls -la /root/.cursor-server/"
查找cursor-server文件
ssh root@your-server-ip "find /root/.cursor-server -name 'cursor-server' -type f"
第二步:清理残留进程
ssh root@your-server-ip "
pkill -f wget
pkill -f cursor
pkill -f node
rm -rf ~/.cursor-server/*
"
第三步:手动下载并安装cursor-server
在本地Mac上执行:
下载cursor-server
cd /tmp
curl -L -o cursor-server.tar.gz https://downloads.cursor.com/production/7b9c34466f5c119e93c3e654bb80fe9306b6cc79/linux/x64/cursor-reh-linux-x64.tar.gz
上传到服务器
scp cursor-server.tar.gz root@your-server-ip:/root/.cursor-server/
在服务器上解压
ssh root@your-server-ip "
cd /root/.cursor-server
tar -xzf cursor-server.tar.gz
rm cursor-server.tar.gz
"
第四步:创建正确的符号链接
ssh root@your-server-ip "
mkdir -p /root/.cursor-server/bin
ln -sf /root/.cursor-server/vscode-reh-linux-x64/bin/cursor-server /root/.cursor-server/bin/cursor-server
验证
ls -la /root/.cursor-server/bin/
/root/.cursor-server/bin/cursor-server --version
"
第五步:配置Cursor设置
修改Cursor的settings.json(Cmd/Ctrl + Shift + P -> Preferences: Open User Settings (JSON)):
{
"remote.SSH.connectTimeout": 120,
"remote.SSH.localServerDownload": "always",
"remote.SSH.remotePlatform": {
"root@your-server-ip": "linux"
}
}
第六步:配置SSH免密登录(可选但推荐)
生成SSH密钥(如果还没有)
ssh-keygen -t rsa -b 4096
复制公钥到服务器
ssh-copy-id root@your-server-ip
配置SSH客户端
cat >> ~/.ssh/config << EOF
Host my-server
HostName your-server-ip
User root
ServerAliveInterval 60
ServerAliveCountMax 3
ConnectTimeout 30
EOF
一键修复脚本
将以下脚本保存为 fix-cursor.sh:
#!/bin/bash
SERVER="root@your-server-ip" # 请替换为你的服务器
echo "=== 1. 清理远程服务器 ==="
ssh $SERVER "
pkill -f wget
pkill -f cursor
pkill -f node
rm -rf ~/.cursor-server
mkdir -p ~/.cursor-server
"
echo "=== 2. 下载cursor-server ==="
cd /tmp
curl -L -o cursor-server.tar.gz https://downloads.cursor.com/production/7b9c34466f5c119e93c3e654bb80fe9306b6cc79/linux/x64/cursor-reh-linux-x64.tar.gz
echo "=== 3. 上传到服务器 ==="
scp cursor-server.tar.gz $SERVER:/root/.cursor-server/
echo "=== 4. 解压并配置 ==="
ssh $SERVER "
cd /root/.cursor-server
tar -xzf cursor-server.tar.gz
rm cursor-server.tar.gz
mkdir -p bin
ln -sf /root/.cursor-server/vscode-reh-linux-x64/bin/cursor-server /root/.cursor-server/bin/cursor-server
echo 'cursor-server版本:'
/root/.cursor-server/bin/cursor-server --version
"
echo "=== 5. 清理本地文件 ==="
rm cursor-server.tar.gz
echo "=== 完成!请重启Cursor ==="
使用方法:
chmod +x fix-cursor.sh
./fix-cursor.sh
验证结果
-
完全重启Cursor(Cmd+Q彻底退出)
-
重新连接远程服务器:
Cmd/Ctrl + Shift + P->Remote-SSH: Connect to Host... -
输入
root@your-server-ip(或配置的别名) -
连接应该秒级完成!
常见问题
Q: 为什么需要创建符号链接?
A: Cursor默认在/root/.cursor-server/bin/cursor-server查找文件,但实际文件在vscode-reh-linux-x64/bin/cursor-server,符号链接解决了路径不一致的问题。
Q: localServerDownload设置为always有什么用?
A: 让Cursor优先从本地下载组件然后上传到服务器,避免服务器直接下载慢的问题。
Q: 如果还是连接慢怎么办?
A: 检查服务器网络:curl -I https://downloads.cursor.com,如果下载慢,可以先用本地下载再上传的方式。
原理说明
Cursor远程连接的原理:
-
通过SSH连接到服务器
-
检查服务器是否有
cursor-server -
如果没有,从CDN下载并安装
-
启动
cursor-server进程 -
建立WebSocket隧道进行通信
本文的解决方案跳过了第三步的网络下载瓶颈,并解决了第四步的文件路径问题,从而实现快速连接。
实测效果:连接时间从原来的30秒超时甚至失败,缩短到3-5秒成功建立连接。