gcloud compute ssh 拒绝连接(return 代码 255)

gcloud compute ssh refuses connection (return code 255)

我无法通过 ssh 访问由 Google 云命令行工具 (gcloud) 创建的 vm 实例。

症状:

sudo gcloud compute ssh myuser@ubuntu
ssh: connect to host 104.155.16.104 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

我做了什么:

1: 确认防火墙在端口 22 上打开

gcloud compute firewall-rules list

返回

NAME                    NETWORK  SRC_RANGES    RULES                         SRC_TAGS  TARGET_TAGS
allow-rstudio           default  0.0.0.0/0     tcp:8787                                allow-rstudio
default-allow-http      default  0.0.0.0/0     tcp:80                                  http-server
default-allow-https     default  0.0.0.0/0     tcp:443                                 
https-server
default-allow-icmp      default  0.0.0.0/0     icmp
default-allow-internal  default  10.128.0.0/9  tcp:0-65535,udp:0-65535,icmp
default-allow-rdp       default  0.0.0.0/0     tcp:3389
default-allow-ssh       default  0.0.0.0/0     tcp:22

2:更新public键

ssh-keygen -t rsa -f ~/.ssh/google_compute_engine -C myuser

3:使用新的 public 密钥更新元数据

sudo gcloud compute ssh myuser@ubuntu
Updating project ssh metadata...
Updating project ssh metadata...done.                                                                                                                                     
Waiting for SSH key to propagate.

然后,还是一样的错误信息:

ssh: connect to host 35.187.38.82 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

我应该补充一下,直到今天我都可以访问ssh,当然,我之前已经进行了身份验证 gcloud 身份验证登录

来自 Google 云 Web 界面的 SSH 有效!那里有什么不同? 将不胜感激!

经过长时间的搜索,我终于找到了这个棘手问题的根本原因。我希望这能帮助一些处于绝望中的人...

您的 ssh 连接被拒绝的原因可能是不小心删除了外部 ip 请求的内部路由。您可以通过以下方式查看:

gcloud compute routes list

如果这不是 return 包含以下条目的列表:

default-internet       default     0.0.0.0/0       default-internet-gateway 1000

那么您必须通过以下方式重新创建此条目:

gcloud compute routes create default-internet \
--destination-range 0.0.0.0/0 \
--next-hop-gateway default-internet-gateway

就我而言,在升级 GCP 实例后(只是添加了更多处理器和内存)。

我的圈子CI部署开始投掷:

Authentication failed.

Exited with code 255

经过几个小时试图找出问题所在,我发现 /etc/ssh/sshd_config 的内容毫无理由地被清空了。 解决我的问题的方法是重新创建此文件并重新启动 ssh 服务。

注意:PasswordAuthentication 应设置为:

   PasswordAuthentication no

对于偶然发现这个的可怜的、受苦的灵魂。

以下对我来说是一致的:

On your machine in gcloud CLI run gcloud init and go through the prompts.

结束。我希望这对你有帮助,亲爱的,互联网 fellow-sufferer.

我的情况是,我是 运行 实例上的一个 nohup 进程,突然间进程停止工作。

我花了很多时间排查,发现是实例本身挂了。我们错过了这样的小事情,在调试更大的问题时被发现

检查您是否能够 ping 您的实例。如果不是,请重新启动它并ssh,它将正常工作。这是解决方案之一。