Google Compute Engine - alternative log in to VM i

2019-07-27 06:58发布

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条回答
唯我独甜
2楼-- · 2019-07-27 08:03

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.

查看更多
登录 后发表回答