3

I have setup my VM on Google compute engine and connect to it from my Mac using SSH via Gcloud. I have installed my Django website and after that when I run the command below:

sudo ufw enable

After this command I not able to connect to my VM instance vis SSH, I need to edit some of my files for my site but I'm not able to connect to my instance.

When I run the command below:

gcloud compute --project "GCP_Project" ssh --zone "my_zone" "Instane_Name"

I got the error below:

ssh: connect to host <my_ip> port 22: Operation timed out
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

I don't know, how can I fix this issue.

Thanks in advance!

Abdul Rehman
  • 133
  • 1
  • 1
  • 4

2 Answers2

7

When you turn UFW on, denies any incoming connection. So, you need to disable it for port 22 and then you will be SSH to your machine again. To do so, you should edit your instance and run a Startup Script.

  1. In the GCP Console, go to the VM Instances page.

  2. Select the instance and click edit.

  3. Find Custom metadata option and Click Add item and Type Startup-script as a key and Copy and past the command sudo ufw allow ssh as a value. This command will enabled port 22 for SSH.

  4. Click save

  5. Stop and start the instance. After rebooting the instance you should be able to SSH again.

Milad Tabrizi
  • 327
  • 1
  • 7
  • Reset will make your VM lose data just stopping it and starting it again worked for me. Thanks By The way – ikben Mar 03 '21 at 21:35
0
  1. Go to edit your VM.
  2. Go to section Automation
  3. Add Startup script
#!/bin/bash 

sudo ufw allow ssh
  1. Stop and Start VM.