Linux | Cloud | DevOps | Scripting

Breaking

Wednesday 26 June 2019

Access SSH in Google Cloud Platform (GCP)


Our purpose is to access 'appserver' via SSH from 'testserver'. For this, we need to share private and public key between both of these instances. But in GCP, there is no need to share keys between instances. We just need to copy public key in the metadata of GCP then we can access instances via SSH. But make sure instances must ping each other.

Steps we need to follow:

  1. Create two Linux instances named testserver and appserver
  2. Login to appserver and create key-pair
  3. Copy public key into the metadata of GCP
  4. Login to testserver and ssh to appserver

Step 1: Create two instances:

- Create an instance named 'testserver':

GCP ➔ Compute Engine ➔ VM Instances ➔ Create Instance ➔ Name: testserver ➔ Region: asia-south1 (Mumbai) ➔ Zone: asia-south1-a ➔ Machine type: n1-standard ➔ CPU Platform: Automatic ➔ Boot Disk: CentOS 7 (from here we can select the operating system) ➔ Boot Disk Type: Standard persistent disk ➔ Size: 10 GB ➔ Select ➔ Service Account: Compute Engine default service account ➔ Access Scope: [*] Allow default access ➔ Firewall: [*] Allow HTTP traffic ➔ [*] Allow HTTPS traffic ➔ Create.

- Create another instance named 'appserver':

GCP ➔ Compute Engine ➔ VM Instances ➔ Create Instance ➔ Name: appserver ➔ Region: asia-south1 (Mumbai) ➔ Zone: asia-south1-b ➔ Machine type: n1-standard ➔ CPU Platform: Automatic ➔ Boot Disk: CentOS 7 (from here we can select the operating system) ➔ Boot Disk Type: Standard persistent disk ➔ Size: 10 GB ➔ Select ➔ Service Account: Compute Engine default service account ➔ Access Scope: [*] Allow default access ➔ Firewall: [*] Allow HTTP traffic ➔ [*] Allow HTTPS traffic ➔ Create.

Step 2: Login to appserver and create keys:

GCP ➔ Compute Engine ➔ VM Instances ➔ Select instance ➔ Click on the SSH in front of the appserver.

Click on the SSH in front of the instance name
Fig: New instance created
This will open a console.

Use command 'ssh-keygen' to create public and private keys. As we use this command, public and private keys creates in .ssh directory in home directory of user. Like, I am logged in by user divakar_rastogi. So, .ssh directory will be created in /home/divakar_rastogi directory. 

$ cd .ssh

$ pwd
/home/divakar_rastogi/.ssh

$ ls –la

List .ssh directory before creating key-pair
Fig: List .ssh directory before creating key-pair

$ ssh-keygen
Enter file in which to save the key (/home/divakar_rastogi/.ssh/id_rsa): <hit enter>
Enter passphrase (empty for no passphrase): <hit enter>
Enter same passphrase again: <hit enter>

$ ls –la

List .ssh directory after the creation of key-pair
Fig: List .ssh directory after the creation of key-pair
Now, we can see id_rsa and id_rsa.pub has been created. Here, id_rsa is a private key and id_rsa.pub is a public key.

Step 3: Copy public key into the metadata of GCP:

$ cat id_rsa.pub

Copy the content of public key. Now, navigate to GCP

GCP ➔ Compute Engine ➔ Metadata ➔ SSH Keys ➔ Edit ➔ + Add Item ➔ Paste entire key data into provided text box ➔ Save.

Step 4: Login to testserver and ssh to appserver:

GCP ➔ Compute Engine ➔ VM Instances ➔ Select instance ➔ click on the SSH in front of testserver, this will open its console. Now, use commands:

$ ssh <IP_of_appserver>

Enjoy!




2 comments:

  1. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! Google Cloud

    ReplyDelete

Pages