首页 > 解决方案 > GCP Compute-engine ssh 仅在文件内部失败

问题描述

我正在尝试编写启动 VM、运行命令然后停止它的脚本。

代码是(为简洁起见,省略了项目和区域):

gcloud compute instances start instance-1
gcloud compute ssh user@instance-1 --command 'echo check-ssh' -- -vvv
gcloud compute instances stop instance-1

当我在终端中运行这些步骤时,一切都很顺利,但是当我通过文件运行它时,ssh 命令失败,我得到:

OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 34.68.81.200 is address
debug2: ssh_connect_direct
debug1: Connecting to 34.68.81.200 [34.68.81.200] port 22.
debug1: connect to address 34.68.81.200 port 22: Connection refused
ssh: connect to host 34.68.81.200 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

实例操作系统是 ubuntu 20.4。

可能与此问题有关的东西,当我尝试通过终端连接到 root 用户时,我得到了同样的错误。

有人知道有什么区别吗?我该如何解决?

标签: sshgoogle-compute-enginegcloud

解决方案


当您请求 GCP 计算实例的 gcloud 计算实例启动时,您实际上是在请求它启动,而不是它实际上已经启动。如果您随后转身执行 SSH 请求,则 VM 可能尚未达到准备接收 SSH 请求的内部状态。尝试在您的 VM 启动和 SSH 访问之间设置一个睡眠延迟(比如...... 5 分钟)......看看这是否会改变故事。

也可以看看:


推荐阅读