从VSCODE到Google Cloud VM的远程SSH通过IAP隧道
我将以下命令用于SSH到GCP VM。如何配置VSCODE使用这些设置?
gcloud beta compute ssh --zone "asia-south1-a" "jump-box" --tunnel-through-iap --project "Project Name"
(编辑注意:注意 - 尤其是Tunnel-through-iap
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下提供了一个教程:
。
简而言之,将
添加到您的正常
gcloud Compute ssh
命令中,以查看GCLOUD在引擎盖下的工作。这些选项需要在您的VSCODE SSH配置文件中设置。使用
-Tunnel-through-iap
时,您将看到一些proxyCommand
调用gcloud.py,hostname
和hostKeyalias
将设置为实例ID(看起来像Compute.123456789123456789
),以及其他一些。An archive of this page is available: https://web.archive.org/web/20220520113631/https://medium.com/@albert.brand/remote-to- a-vm over-iap-tunnel-with-vscode-f9fb54676153
The following provides a tutorial:
https://medium.com/@albert.brand/remote-to-a-vm-over-an-iap-tunnel-with-vscode-f9fb54676153 .
Briefly, add
--dry-run
to your normalgcloud compute ssh
command to see what gcloud is doing under the hood. Those options need to get set in your VSCode SSH config file.When using
--tunnel-through-iap
, you'll see a fewProxyCommand
that invokes gcloud.py, andHostName
andHostKeyAlias
set to the instance ID (looks likecompute.123456789123456789
), as well as a few others.An archive of this page is available: https://web.archive.org/web/20220520113631/https://medium.com/@albert.brand/remote-to-a-vm-over-an-iap-tunnel-with-vscode-f9fb54676153
给定标题,我猜您要做的是使用
计算
命令 https://code.visualstudio.com/docs/remote/ssh我找不到直接使用它的方法
= “ 项目中所有实例的SSH配置。
然后,我能够将我感兴趣的主机添加到我的VSCODE SSH Config(
〜/.ssh/config
默认情况下)。这是这样的,但是我已经编辑了与我的项目相关的位。您也可以省略
-Dry-Run
以自动添加条目。Given the title I'm guessing what you're trying to do is use a
compute ssh
command for https://code.visualstudio.com/docs/remote/sshI couldn't find a way to use this directly, but a workaround that I found is to run:
This will produce an SSH configuration for all the instances in your project.
I was then able to add the hosts I was interested to my vscode ssh config (
~/.ssh/config
by default). It'll be something like this, but I've redacted the bits related to my project.You could also omit the
--dry-run
to automatically add the entries.如果您在徒劳地寻找Windows解决方案时检查了这个问题,并且您不知道为什么VS代码中的OpenSSH会给您提供奥秘错误,例如
createProcessw失败
尝试以下内容。所有问题的短篇小说是Windows上Vscode中的Openssh在试图解析字符串的重大问题上。如下更详细的故障排除步骤。
如果使用默认设置在Windows上安装了GCLOUD工具,则它们可能已安装到
c:\ ... \ AppData \ Google Cloud \ ...
中。安装中的该空间导致createProcessw
失败。转到设置 - >安装应用程序 - > Google Cloud SDK并卸载它。将其重新安装在c:\ gcp
或其他没有空间的其他短文件夹中。安装并重新启动终端并再次启动
gcloud auth登录
,您可以运行此操作将吐出类似的东西
,以创建一个字符串,VSCODE实际上可以与OpenSSH的字符串解析一起使用。将看起来像这样的东西转动:
很可能没有所有转换就可以到达那里,但是根据我的经验,vscode上的openssh会给您带来无用的错误,例如
主机名不能包含字符“ 因为字符串解析逻辑认为最终“是。将字符串转换为该形状使其可以与Vscode中的Openssh一起使用。也就是说,连接字符串可以在终端中工作,但由于未知VSCODE特定原因,VSCODE仍将失败。
在PowerShell中测试您的字符串,它应通过IAP连接到VM。
现在,转到VSCODE连接到远程主机,然后“添加新的SSH主机”,然后将更改的字符串粘贴到其中,以将其添加到SSH配置中。只是想使用
即使是“连接到主机”中的那个字符串也会失败,但是“添加新的SSH主机”最终将正确解析。
然后再次从远程SSH按钮通过连接到主机,这次选择刚刚添加的“ Compute.numbers”条目。 VSCODE应最终连接。
If you are checking this question in looking in vain for a Windows solution and you don't know why OpenSSH in VS code is giving you arcane errors like
CreateProcessW failed
try the following.The short story for all of the problems is just that OpenSSH in VSCode on Windows has significant issues attempting to parse strings. More detailed troubleshooting steps follow.
If you have installed gcloud tools on Windows using default settings, then they have likely installed into
C:\...\AppData\Google Cloud\...
. That space in the install causes theCreateProcessW
failure. Go to settings -> Installed Apps -> Google Cloud SDK and uninstall it. Reinstall it inC:\gcp
or some other short folder without a space.After you've installed and restarted your terminal and ran
gcloud auth login
again, you can runThis will spit out something like
Now to create a string that VSCode can actually use with OpenSSH's string parsing. Turn that int something that looks like this:
It may very well be possible to get there without all of the transformations, but in my experience, OpenSSH on VSCode will give you unhelpful errors like
host name cannot contain the character "
because the string parse logic believes the final " is somehow part of the [email protected]. Transforming the string into that shape allows it to work with OpenSSH in VSCode. Namely the connection string can work in terminal, but will still fail in VSCode for unknown VSCode specific reasons.Test your string in powershell, it should connect to the VM through IAP.
Now go to VSCode connect to remote host and then "Add New SSH Host" then paste your changed string into that to add it to the ssh config. Just trying to use
even that string in the "Connect to Host" will fail, but "Add New SSH Host" will finally parse it correctly.
Then go through the Connect to Host from the Remote-SSH button again and this time select the "compute.numbers" entry that was just added. VSCode should connect finally.