How to run remote commands on Google Cloud Platform(GCP)

Running remote commands on Google Cloud Platform (GCP) allows you to execute commands on virtual machines (VMs) without having to log in to each machine individually. This can save you a lot of time and effort when managing multiple VMs on GCP.

There are two main ways to run remote commands on GCP:

  • gcloud command-line tool: The gcloud compute execute command allows you to execute commands on a VM by specifying its name or IP address.

  • Google Cloud Platform Console: The GCP Console provides a graphical interface for running remote commands on VMs.

Using the gcloud

Here's a step-by-step guide to using gcloud for running remote commands on GCP:

  1. Install Google Cloud SDK: Ensure you have the Google Cloud SDK installed on your local machine. You can follow this Answer for installation purpose.

  2. Set up authentication: Make sure you have authenticated your gcloud command-line tool with the appropriate GCP project and service account. You can do this by running the following command and following the prompts to log in:

gcloud auth login
Command to setup GCP authentication
Output of gcloud auth login
Output of gcloud auth login
  1. Create an instance: Ensure you have a running VM instance on GCP where you want to execute the remote commands. You can follow this Answer to create a VM instance.

  2. Get instance information: You'll need the instance name and zone to connect via SSH. You can get this information by running:

gcloud compute instances list
Comnand to list instance information
Output of gcloud compute instances list
Output of gcloud compute instances list

  1. SSH into the VM: To SSH into a specific VM instance, use the following command:

gcloud compute ssh [INSTANCE_NAME] --zone=[ZONE] --project=[PROJECT_ID]
Command to SSH into VM instance

Replace the placeholders:

  • [INSTANCE_NAME]: The name of the VM instance you want to connect to.

  • [ZONE]: The zone where the VM instance is located.

  • [PROJECT_ID]: The ID of the GCP project that contains the VM instance.

Output of gcloud compute ssh
Output of gcloud compute ssh
  1. Running remote commands: Once you're connected to the VM via SSH, you can run any commands as if you were logged into the remote machine. For example, you can run:

ls
Sample command to list contents of home directory
Output of running ls command
Output of running ls command
  1. Exit the SSH session: To exit the SSH session and return to your local machine's command prompt, simply type:

exit
Command to exit SSH session
Output of running exit command
Output of running exit command

Using Google Cloud Platform Console:

To run remote commands on a Google Cloud Platform (GCP) instance using the GCP Console, you can use the built-in SSH feature. Here's a step-by-step procedure:

  1. Open Google Cloud Console: Open your web browser and navigate to the Google Cloud Console.

Google Cloud Console
Google Cloud Console
  1. Select your project: From the top navigation bar, select the GCP project where your VM instance is located. You can click on the project name, and a dropdown list will appear. Choose the appropriate project.

GCP projects
GCP projects
  1. Navigate to Compute Engine: In the left sidebar, under the "Compute" section, click on "Compute Engine" to access the Compute Engine dashboard.

Compute Engine
Compute Engine
  1. Locate your VM instance: In the Compute Engine dashboard, you will see a list of all your VM instances in the project. Locate the VM instance on which you want to run remote commands.

VM instances
VM instances
  1. Start SSH session: Click on the "SSH" button next to the VM instance you wish to connect to. This will open an SSH session in the browser.

SSH session in browser
SSH session in browser
  1. Enter remote commands: Once the SSH session is open, a terminal-like interface will appear in the browser. You can now enter and run commands as if you were using a local terminal on the VM instance.

Entering remote command to display contents of home directory
Entering remote command to display contents of home directory

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved