VS Code - 通过 SSH 隧道打开文件夹和编辑文件

Posted

技术标签:

【中文标题】VS Code - 通过 SSH 隧道打开文件夹和编辑文件【英文标题】:VS Code - Openfolders and edit files through SSH tunnel 【发布时间】:2020-12-03 16:52:28 【问题描述】:

我通常使用 VS Code 的 Remote-SSH 扩展来连接到我工作的 AWS 实例。他们的文档中有一个很好的部分关于如何做到这一点here。我讨厌单独使用终端,因为我喜欢调试器以及打开文件夹和文件进行查看的能力。直到最近,我一直从我的 MacBook Pro (A) SSH 到运行 Ubuntu 18.04.4 LTS (B) 的 AWS 实例,没有任何问题。但是,现在我需要处理一个新实例,也是 Ubuntu 18.04.4 LTS,(C),我只能从 B SSH 进入,而不能从 A。

This part of the documentation 表示我可以从A SSH 到B,然后转发一个端口来创建一个SSH 隧道。但是,这只允许我在 C 中打开一个终端。但是,这不允许我在资源管理器中像我想要的那样打开文件夹和文件。

我已经按照秦升的指示here 设置了一个 SSH 隧道并使用 Remote-SSH 扩展的“ProxyJump”功能我的配置文件如下所示:

Host B
  HostName B.mydomain.com
  IdentityFile /Users/MyID/.ssh/B_private_key
  User me

Host C
  HostName C
  IdentityFile /Users/MyID/.ssh/C_private_key
  ProxyJump B
  User me

当我在 VS Code 上使用终端运行 ssh C 时,这非常有效。无需输入任何密码,我就会被带到 C 并可以执行我的工作。但是,当我尝试使用“远程 SSH:连接到主机”功能通过 B 使用 ProxyJump 通过 SSH 连接到 C 并随后在那里打开文件夹/查看文件时,我收到消息“无法建立与 'C' 的连接” ”。堆栈跟踪如下所示:

[13:59:48.537] Log Level: 2

[13:59:48.539] remote-ssh@0.51.0
[13:59:48.539] darwin x64
[13:59:48.541] SSH Resolver called for "ssh-remote+C", attempt 1
[13:59:48.541] SSH Resolver called for host: C
[13:59:48.541] Setting up SSH remote "C"
[13:59:48.546] Acquiring local install lock: /var/folders/w2/0w6jys8d34x940rj7f5r20vnh83ht7/T/vscode-remote-ssh-C-install.lock
[13:59:48.590] Looking for existing server data file at /Users/MyID/Library/Application Support/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-v1-91899dcef7b8110878ea59626991a18c8a6a1b3e-0.51.0/data.json
[13:59:48.593] Using commit id "91899dcef7b8110878ea59626991a18c8a6a1b3e" and quality "stable" for server
[13:59:48.595] Install and start server if needed
[13:59:48.599] Checking ssh with "ssh -V"
[13:59:48.629] > OpenSSH_8.1p1, LibreSSL 2.7.3
[13:59:48.632] askpass server listening on /var/folders/w2/0w6jys8d34x940rj7f5r20vnh83ht7/T/vscode-ssh-askpass-c4eabfddec4ae91c1e423a3167dd184d8553da26.sock
[13:59:48.633] Spawning local server with "ipcHandlePath":"/var/folders/w2/0w6jys8d34x940rj7f5r20vnh83ht7/T/vscode-ssh-askpass-b00842fb08dbbff49678439b1335f105364fd9d5.sock","sshCommand":"ssh","sshArgs":["-v","-T","-D","52911","-o","ConnectTimeout=15","C"],"dataFilePath":"/Users/MyID/Library/Application Support/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-v1-91899dcef7b8110878ea59626991a18c8a6a1b3e-0.51.0/data.json"
[13:59:48.633] Local server env: "DISPLAY":"1","ELECTRON_RUN_AS_NODE":"1","SSH_ASKPASS":"/Users/MyID/.vscode/extensions/ms-vscode-remote.remote-ssh-0.51.0/out/local-server/askpass.sh","VSCODE_SSH_ASKPASS_NODE":"/Users/MyID/Desktop/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer)","VSCODE_SSH_ASKPASS_MAIN":"/Users/MyID/.vscode/extensions/ms-vscode-remote.remote-ssh-0.51.0/out/askpass-main.js","VSCODE_SSH_ASKPASS_HANDLE":"/var/folders/w2/0w6jys8d34x940rj7f5r20vnh83ht7/T/vscode-ssh-askpass-c4eabfddec4ae91c1e423a3167dd184d8553da26.sock"
[13:59:48.635] Spawned 29414
[13:59:48.734] > local-server> Spawned ssh: 29415
[13:59:48.738] stderr> OpenSSH_8.1p1, LibreSSL 2.7.3
[13:59:48.740] stderr> /bin/false: No such file or directory
[13:59:48.740] stderr> kex_exchange_identification: Connection closed by remote host
[13:59:48.741] > local-server> ssh child died, shutting down
[13:59:48.745] Local server exit: 0
[13:59:48.745] Received install output: OpenSSH_8.1p1, LibreSSL 2.7.3
/bin/false: No such file or directory
kex_exchange_identification: Connection closed by remote host

[13:59:48.746] Stopped parsing output early. Remaining text: OpenSSH_8.1p1, LibreSSL 2.7.3/bin/false: No such file or directorykex_exchange_identification: Connection closed by remote host
[13:59:48.746] Failed to parse remote port from server output
[13:59:48.747] Resolver error: 
[13:59:48.750] ------

我似乎很清楚,主要问题是试图访问 /bin/false 作为解释器。我认为这是我的 MacBook Pro (A) 的问题。我确实注意到在 A 上运行 echo $SHELL 时它返回 /bin/false,但我运行 export SHELL=/bin/bash 来更改它并按照 this post 中的建议将 VS Code 的 MacOS 集成 shell 设置为 /bin/bash .我也在 B 上检查了echo $SHELL,它也同样显示了/bin/bash

我的问题是:

    我想要完成的事情可能吗? 如果是,我做错了什么?/我可以检查什么来帮助确保我可以使用我想要的 VS Code 功能?

【问题讨论】:

【参考方案1】:

我想通了。我的/etc/passwd 文件中有一堆/bin/false 引用,所以我将它们更改为/bin/bash,现在它就像一个魅力。 :)

【讨论】:

以上是关于VS Code - 通过 SSH 隧道打开文件夹和编辑文件的主要内容,如果未能解决你的问题,请参考以下文章

通过vscode的SSH隧道打开一个Matplotlib图

使用Windows7上的VS Code打开远程机Ubuntu上的文件操作步骤

通过 SSH SOCKS 隧道将流量转发到特定 IP

用于 sftp 或 GUI 的 ssh 隧道

无法从我的 linux shell 打开 ssh 隧道(EC2 暴露 RDS db)

Jupyter notebook SSH 隧道错误“无法打开签名数据库”