Configuring a remote connection on a Linux instance hosted on Google Cloud Platform
Tags: cloud, beginners
Published: 2019-10-11 14:37:13

---
title: Configuring a remote connection on a Linux instance hosted on Google Cloud Platform
published: true
date: 2019-10-09 11:23:42 UTC
tags: cloud,beginners
canonical_url: https://educosta.dev/blog/configuring-a-remote-connection-on-a-linux-instance-hosted-on-google-cloud-platform/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/z3nr5o8xohm6a718xjc0.jpg
---


In this tutorial, we will configure a Linux Virtual Machine that is hosted on <g id="187" data-gr-id="187">Google</g> Cloud Platform (GCP) to accept remote connection through SSH using a public/private key. If you do not have a GCP [Learn here how to create a Google Cloud Platform Account](https://dev.to/educostadev/learn-how-to-create-a-google-cloud-platform-account-4g7e). For Windows users I recommend two steps before start:

1. Install the [ConEmu](https://conemu.github.io/). It is a Windows terminal with a lot of enhancement.
2. Install [Hosts File Editor](https://hostsfileeditor.com/). It is a Editor for the Windows Host File. 

## Starting the VM Instance and configuring the Hostname

- Go to the Google Cloud Platform (GCP) dashboard and start the VM Instance. (See this tutorial on how to create a VM Instance).
- Copy the _External IP_ and check if you can reach the remote machine with a ping command. **Warning** : This IP address can change when restarting the VM instance. 

```
\>ping 34.82.27.184
```

- Open the <g id="12" data-gr-id="12">software </g>`Host File Editor`<g id="12" data-gr-id="12"> and</g> configure the Hostname for this IP address. It this example I gave the <g id="13" data-gr-id="13">name </g>`becloudnativedev`<g id="13" data-gr-id="13">.</g> _When the external UP address changes after a restart, update the host file with the new value_. The software will edit the Windows host file, it is very similar to the Linux _/etc/hosts_ file. 
- Perform a `ping` command to check whether you can reach re remote machine with the <g id="8" data-gr-id="8">host name</g> chosen.

```
\> ping becloudnativedev Pinging becloudnativedev [34.82.27.184] with 32 bytes of data: Reply from 34.82.27.184: bytes=32 time=188ms TTL=52 Reply from 34.82.27.184: bytes=32 time=186ms TTL=52 Reply from 34.82.27.184: bytes=32 time=185ms TTL=52 Reply from 34.82.27.184: bytes=32 time=189ms TTL=52 Ping statistics for 34.82.27.184: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 185ms, Maximum = 189ms, Average = 187ms
```

## Generating the Public/Private key to remove connection

Generate the public and private key files with the command ssh-keygen.

```
ssh-keygen -t rsa -b 4096 -f %USERPROFILE%/.ssh/becloudnativedev.key
```

- When asked for the passphrase, keep blank and just type enter to confirm. The output will be similar to the following. 

```
Generating public/private rsa key pair. Created directory 'C:\Users\eduardo/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in C:\Users\eduardo/.ssh/becloudnativedev.key. Your public key has been saved in C:\Users\eduardo/.ssh/becloudnativedev.key.pub. The key fingerprint is: SHA256:TsRNK0F7ybvCOC0mnzSpkBWDyiuvMVfu8i5mk5Yhs6E eduardo@notebook The key's randomart image is: +---[RSA 4096]----+ | .o . | | . . \* o | | . o = \* | |.. o . o . | |.. o S . | |+ o= B . | |=\*++o O = . | |E=Xo \* = . | |.=.\*= o | +----[SHA256]-----+
```

- Go to the `%USERPROFILE%/.ssh` folder and check the created files `becloudnativedev.key` and `becloudnativedev.key.pub` 

```
C:\Users\eduardo\.ssh λ ls -la total 45 drwxr-xr-x 1 eduardo 197121 0 Sep 19 00:01 ./ drwxr-xr-x 1 eduardo 197121 0 Sep 18 23:46 ../ -rw-r--r-- 1 eduardo 197121 3243 Sep 19 21:32 becloudnativedev.key -rw-r--r-- 1 eduardo 197121 743 Sep 19 21:32 becloudnativedev.key.pub -rw-r--r-- 1 eduardo 197121 175 Sep 19 00:01 known\_hosts
```

### Generating a Public/Private Key using using PuttyGen

With the following steps, you can generate the keys using the software _PuttyGen_. _You do not need to follow these steps if you followed the previous one._

- Donwload and open PuttyGen
- Click on `Generate` button and keep moving the mouse while the progress bar fill out.
- Click on `Conversions -> Export OpenSSH key` and save this private key into a file named `becloudnativedev.key`. 
- Copy the text of the public key from the PuttyGen and save into a file `becloudnativedev.key.pub` .
- When try to test the _SSH_ command, If you get a _Permission denied_ error due a _Bad permission_ you will need restrict the permission of the `becloudnativedev.key` file . Check [this thread](https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open) on stackoverflow.

### Generating a Public/Private Key on Linux

If you are on Linux you can generate the keys with the following steps. _You do not need to follow these steps if you followed the previous one._

- Go to .ssh folder `cd ~/.ssh`
- Generate a public and private <g id="9" data-gr-id="9">key </g>`ssh-keygen -t rsa -f ~/.ssh/becloudnativedev.key`<g id="9" data-gr-id="9">.</g> 
- Two files will be created at ~/.ssh. `becloudnativedev.key` and `becloudnativedev.key.pub`
- Change the permissions of the private key `chmod 600 becloudnativedev.key`

## Configuration the VM Instance to accept remote connections

Lets configure the VM instance to allow SSH connections using our keys.

- Go to VM Instances on GCP and click on the instance you created 
- Click <g id="11" data-gr-id="11">on </g>`Edit`<g id="11" data-gr-id="11"> <g id="8" data-gr-id="8">button</g></g> and scroll down <g id="12" data-gr-id="12">to </g>`SSH Key`<g id="12" data-gr-id="12"> <g id="9" data-gr-id="9">section</g></g>. 
- In the session Click on `Show and edit` and paste into the text box the content of the file `becloudnativedev.key.pub` 
- Scroll down and click <g id="14" data-gr-id="14">on </g>`Save`<g id="14" data-gr-id="14"> <g id="13" data-gr-id="13">button</g></g>. 

## Testing the remote connection

Now it´s time to confirm that everything works and connect on our remote machine.

- Open the console go to the `%USERPROFILE%/.ssh` directory and connect remotely to the VM Instance using the command `ssh -i becloudnativedev.key eduardo@becloudnativedev`

```
λ ssh -i becloudnativedev.key eduardo@becloudnative key\_load\_public: invalid format The authenticity of host 'becloudnative (35.199.170.231)' can't be established. ED25519 key fingerprint is SHA256:Zlbz3z6tObcWd3UQyCf+1giF4aJeZ11StwY4vMrPMmQ. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'becloudnative' (ED25519) to the list of known hosts. educostabra@cloud-native ~ $
```

It´s done! The remote machine accepted the SSH connection and used the private key to authenticate without the need to ask you password. Keep you private key safe and do not forget of shutdown your VM Instance in case you will not use.

Watch the following video and see the previous steps being executed on practice:

{% youtube g6XJ5uM43C0 %}

The post [Configuring a remote connection on a Linux instance hosted on Google Cloud Platform](https://educosta.dev/blog/configuring-a-remote-connection-on-a-linux-instance-hosted-on-google-cloud-platform/) appeared first on [Be Cloud Native](https://educosta.dev). If you have any doubts or questions feel free to drop a comment here or reach me out on Twitter [@educostadev](http://twitter.com/educostadev).