How to create a GCP VM instance and perform login using SSH

From PUBLIC-WIKI
Jump to navigation Jump to search

Login to the management console

  • Login to the GCP console using privileged account:
https://console.cloud.google.com/
  • From the upper pane, select the relevant project

Adding account permissions

  • From the upper left pane, click on “IAM & Admin” -> IAM
  • Click on Add -> inside “Members”, specify either GMAIL or G Suite email address -> Specify the following roles:
  • Compute Engine / Compute Image User
  • Compute Engine / Compute Instance Admin (v1)
  • Compute Engine / Compute OS Login
  • Compute Engine / Compute Viewer
  • Service Accounts / Service Account User
  • Click on Add

Generating SSH keys for specific accounts (from Windows machine)

  • Download puttygen.exe from:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  • Run the puttygen.exe
  • Click Generate and follow the on-screen instructions to generate a new key.
Note: Make sure you create keys with at least 2048 bits
  • In the Key comment section, replace the existing text with the username who will use this key to login to the VM instance.
  • Click Save private key to write your private key to a file with a .ppk extension.
  • Click Save public key to write your public key to a file for later use.

Generating SSH keys for specific accounts (from Linux machine)

  • Login to the Linux machine console.
  • Run the following command to generate the SSH key pair:
ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
Note 1: Replace KEY_FILENAME with the actual private key file name
Note 2: Replace USERNAME with the user who will use this SSH key to login
  • Change the permissions of the SSH private key using the command below:
chmod 400 ~/.ssh/[KEY_FILENAME]
Note: Replace KEY_FILENAME with the actual private key file name

Configure Metadata settings for the VM instances

  • From the upper left pane, click on “Compute Engine”
  • From the left pane, click on “Metadata”
  • From the main pane, under “Metadata” click on Edit -> add new key:
  • Key: enable-oslogin
  • Value: false
  • Click on Save
  • Open the previously created public key (usually it has no file extension) using a text editor and copy its entire content into memory.
  • From the main pane, under “SSH Keys” -> click on Edit -> click on Add item -> paste the content of the public key into the free text field labeled “Enter entire key data” -> click on Save

Creating a new VM instance

  • From the left pane, under “Compute Engine”, click on “VM instances”
  • From main pane, click on “Create Instance”:
  • Name: Specify machine hostname
  • Zone: Select a zone close to your location
  • Machine type: Select appropriate machine type according to your needs.
For more information, see:
https://cloud.google.com/compute/docs/machine-types
  • Boot disk: Choose either relevant “OS image” or pre-installed “Application image”
  • Click on Create

Login to a VM instance (from Windows machine)

  • Download Putty from:
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
  • Run putty.exe
  • From the left pane, under “Connection” -> expand SSH -> click on “Auth” -> from the main pane, under “Authentication parameters”, click on “Browse” -> locate the SSH private key generated by puttygen.exe
  • From the left pane, click on “Session” -> from the main pane, under “Host Name (or IP address)” specify the following:
USERNAME@IP_Address
Note 1: Replace USERNAME with the user who will use this SSH key to login
Note 2: Replace IP_Address with the VM instance external IP
  • Under “Saved Sessions”, specify a name for this newly created connection.
  • Click on Save
  • Click on Open

Login to a VM instance (from Linux machine)

  • Login to the Linux machine console.
  • Run the following command:
ssh USERNAME@IP_Address -i ~/.ssh/[KEY_FILENAME]
Note 1: Replace USERNAME with the user who will use this SSH key to login
Note 2: Replace IP_Address with the VM instance external IP
Note 3: Replace KEY_FILENAME with the actual private key file name