Google compute - SSH - port 22 blocked by ufw

Solution 1:

By Default, all ports are blocked other than port 22 to allow you to ssh to the VM instance. You also have the option to open port 80 and/or 443. You can see which ports are open by default by checking the Firewall rules within the Cloud Console. I am mentioning this to inform you that you do not need to install ufw in the future.

You will have to provide a startup script to the VM instance to enable SSH. You can just create a simple bash script with either command listed below.

$ufw allow ssh


ufw allow 22

You do not have to use the sudo prefix since the startup script runs as root already.

Solution 2:

Just complimenting the accepted answer.

Some are asking how you can include a start-up script on a VM that you can't even SSH into. You can accomplish this by:

  • Navigate to the GCP Console under your VM instance
  • Stop your VM Instance
  • Click Edit
  • Under custom meta-data, add the key startup-script and the value:

     #! /bin/bash
     sudo ufw allow 22

    Hope this clears some of the confusion :)