FabianVolkers / Foundations-of-Software-Engineering

1 stars 0 forks source link

Google Cloud Platform: SSH Connection Failed #1

Open FabianVolkers opened 4 years ago

FabianVolkers commented 4 years ago

Connection Failed We are unable to connect to the VM on port 22

Screen Shot 2020-03-26 at 11 25 05
FabianVolkers commented 4 years ago

Problem

This issue appears when installing and configuring a firewall like ufw on your server without allowing ssh access through the firewall. If this happens you have locked yourself out of your server, but don't worry. You are not the first person to do so and Google has a way for you to regain access to your virtual machine. What we will do is reboot our machine and temporarily disable the firewall so we can change its configuration. Afterwards we enable the firewall again.

Solution

  1. In the google cloud platform, go to Compute Engine > VM Instances

  2. Then click on the name of the instance with the firewall issue, you should see details about the instance.

  3. Click “edit” and scroll down to “custom metadata”

  4. Here we add the key ‘startup-script’ with the value

    #! /bin/bash
    /usr/sbin/ufw disable

    image

  5. Click ‘save’ at the bottom on the page. Go back to your VM Instances overview and restart the instance.

  6. Once the machine restarted you should be able to use ssh to access it again.

  7. Now you can fix your firewall config with sudo ufw allow ssh

  8. reenable the firewall with sudo ufw enable

  9. Before you lock yourself out again it is always a good idea to check if everything is configured the way you want;. To do this, run sudo ufw status

  10. If both Nginx http and ssh (22/tcp) are allowed through the firewall, your firewall is configured properly.

  11. One important last step is to remove the startup script again after having changed the configuration successfully. Otherwise we would turn off the firewall on every restart, thereby defeating it’s purpose.