远程linux终端中支持vscode的code命令

发布时间:2026/8/12 18:02:24
远程linux终端中支持vscode的code命令 1. 背景利用win10上的vscode ssh进行远程linux连接时在win 10上vscode的终端中使用code命令可以打开新的vscode窗口直接连接到远程linux。但是在远程linux无法直接使用code命令来打开win10上vscode。我们想实现在远程linxu上也可以通过code命令直接打开win10上的vscode。2. 原因主要原因是远程的linux中没有对应的path和IPC hook。下一步可以手动增加一个path和IPC hook。3. 操作命令3.1 前提条件win10上已经打开了一个vscode窗口并且已经建立了远程ssh连接到linux。3.2 具体操作# 临时给mobaxterm的Vscode设置path和ipc支持vscode的code命令在远程的linux中执行# 第1步设置path export PATH$(find $HOME/.vscode-server/cli/servers -mindepth 1 -maxdepth 1 -type d -name Stable-* ! -name *.staging -printf %T %p\n | sort -nr | head -1 | cut -d -f2-)/server/bin/remote-cli:$PATH # 第2步设置ipc export VSCODE_IPC_HOOK_CLI$(ss -xlpnH | grep -oE /run/user/$(id -u)/vscode-ipc-[^[:space:]]\.sock | head -1) # 第3步查看设置的结果 which code echo $VSCODE_IPC_HOOK_CLI4. 解释第一行把code命令加入 PATHexport PATH$(find $HOME/.vscode-server/cli/servers \ -mindepth 1 -maxdepth 1 -type d \ -name Stable-* ! -name *.staging \ -printf %T %p\n | sort -nr | head -1 | cut -d -f2-)/server/bin/remote-cli:$PATH它会在以下目录查找 VS Code Server~/.vscode-server/cli/servers​只寻找名为Stable-*的稳定版目录并排除*.staging临时目录。根据修改时间倒序排列sort -nr​选择最新的一个版本head -1​取出该版本中remote-cli的路径添加到当前 shell 的PATH前面。第二行指定 VS Code 的 IPC 通信 Socketexport VSCODE_IPC_HOOK_CLI$( ss -xlpnH | grep -oE /run/user/$(id -u)/vscode-ipc-[^[:space:]]\.sock | head -1 )它会使用ss -xlpnH查找本机正在监听的 Unix Socket。筛选当前用户的 VS Code IPC Socket例如/run/user/1000/vscode-ipc-xxxxxxxx.sock​取找到的第一个 Socket。5. 限制条件export只对当前终端及其子进程有效退出 MobaXterm 会话后失效。前提是已经通过 VS Code Remote SSH 连接过这台服务器并且 VS Code Server 仍在运行。6. 其他命令6.1 分步设置# 分步设置patchfind $HOME/.vscode-server/cli/servers -mindepth 1 -maxdepth 1 -type d -name Stable-* ! -name *.staging -printf %T %p\n find $HOME/.vscode-server/cli/servers -mindepth 1 -maxdepth 1 -type d -name Stable-* ! -name *.staging -printf %T %p\n | sort -nr | head -1 | cut -d -f2- # 分步设置ipc ss -xlpn | grep /run/user/$(id -u)/vscode-ipc # 设置连接 ipc$(ss -xlpnH | grep -oE /run/user/$(id -u)/vscode-ipc-[^[:space:]]\.sock | head -1) echo $ipc export VSCODE_IPC_HOOK_CLI$ipc# 分步设置ipcss -xlpn | grep /run/user/$(id -u)/vscode-ipc # 设置连接 ipc$(ss -xlpnH | grep -oE /run/user/$(id -u)/vscode-ipc-[^[:space:]]\.sock | head -1) echo $ipc export VSCODE_IPC_HOOK_CLI$ipc6.2 手动导入配置在vscode中终端查询which code echo $VSCODE_IPC_HOOK_CLI # 输出 /home/d12345/.vscode-server/cli/servers/Stable-8a7abeba6e03ea3af87bfbce9a1b7e48fed567b8/server/bin/remote-cli/code /run/user/1006/vscode-ipc-41309e88-5a57-40ed-bb1a-64905ece2758.sock在mobaxterm中设置export PATH$HOME/.vscode-server/cli/servers/Stable-8a7abeba6e03ea3af87bfbce9a1b7e48fed567b8/server/bin/remote-cli:$PATH export VSCODE_IPC_HOOK_CLI/run/user/1006/vscode-ipc-41309e88-5a57-40ed-bb1a-64905ece2758.sock