Google Compute Engine - alternative log in to VM instance if ssh port is disabled

蹲街弑〆低调 提交于 2019-12-11 11:54:22

问题


I logged in to my GCE VM instance (Debian) and installed ufw and enabled it.

However, I forgot to enable port 22. Is there another way to log in so that I can enable port 22 without having to destroy the instance?


回答1:


No, the only way to access to the instance is through SSH.

You can enable the port 22 using a startup script like the following one:

#!/bin/bash
/usr/sbin/ufw allow 22/tcp

Then, you can add this startup script to your instance either using the Developers Console and pasting the code in the custom metadata section or using the cloud SDK with the command:

gcloud compute instances add-metadata <INSTANCE NAME> --metadata-from-file startup-script=<PATH TO SCRIPT> --project <PROJECT ID>

This script will be executed as root every time the instance boots up or reboots so you'll need to restart your instance and you can remove the script once you have achieved your goal with:

gcloud compute instances remove-metadata <INSTANCE NAME> --keys startup-script --project <PROJECT ID>

You have more information about startup scripts in the documentation.



来源:https://stackoverflow.com/questions/28759251/google-compute-engine-alternative-log-in-to-vm-instance-if-ssh-port-is-disable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!