When working with linux VM you need to connect to them over SSH. In GCP be default login using password authentication is turned off, which i believe is always right approach to keep access to your VMs secure.
In GCP you can SSH into your VMs using two methods
1- using GCP provided browser supported SSH. To access that simply login to your GCP console > VM Instances.
This is simplest and most easiest way to SSH into your VMs. But there are 2 challenges with this –
a) it is slow to connect
b) at times it will give you error and will not connect.
2- Second method is using putty (in windows) or terminal (in mac). In this you generate private-public key pair on machine from where you wish to SSH into VM. After generation you keep private key on your machine and copy/paste the public key in your VM GCP.
Please note, if you have multiple VMs, you need not generate multiple keys to access them. You can access all of them with same private key, all you need to do is paste public key to all VMs you can access from your machine.
STEP1: Setup firewall
Ensure that in Firewall, ssh port 22 is open and if you are on static IP you can allow traffic only from that else just keep IP as 0.0.0.0
Now that you have checked firewall, to be double sure that we are able to SSH into VM, open putty, enter VM external IP and click Open. If you see Login as prompt, it means we are good.
STEP 2: Generate keys
For this open puttygen, and click generate. It will ask you to move your mouse in blank space to generate the key. Once generated it will display Public Key and option to save Private Key.
a) Login name — first in Key Comment we will enter the name we want to use to login to VM.
b) save private key — click save private key. (save at any location of your choice, just remember it as you will need to choose it every-time you SSH into your VM.
Step3 — Copy Public Key to VM
a) Copy the public key part of key pair you just generated.
b) Now go to GCP console, click edit for VM you want to SSH into. Please note, you need not stop the VM to perform this.
c) On edit page, scroll to bottom to ‘Security and Access’ section, there you will see an option ‘Add key’. Click on that and paste the public key you copied from previous step.
Now we are all done with keys setup — we have private key on your machine and public key on VM.
Step4 — SSH using putty
Open putty on your windows machine,
a) Enter the external IP of your VM
b) Click SSH > Auth. Here browse the private key you have saved earlier.
c) Click on Session (first option) and click Open
d) You will again see the login prompt. Enter the name (name you entered in key comment in step 2) and press enter.
Voila!! Now you can SSH into your VM on GCP from your windows machine.
Please see, you can follow same steps for Mac or ubuntu. Both them have inbuilt application for key generator (ssh-keygen).
(Written by Harish Sharma)