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)
设置完成后可以在远程linux中确认下:
which code
echo "$VSCODE_IPC_HOOK_CLI"
4. 解释
第一行:把 code 命令加入 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"
它会:
- 在以下目录查找 VS Code Server:
~/.vscode-server/cli/servers - 只寻找名为
Stable-*的稳定版目录,并排除*.staging临时目录。 - 根据修改时间倒序排列:
sort -nr - 选择最新的一个版本:
head -1 - 取出该版本中
remote-cli的路径,添加到当前 shell 的PATH前面。
第二行:指定 VS Code 的 IPC 通信 Socket
export 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 分步设置
分步设置patch
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"
分步设置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"
6.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"