Getting Started with Cloud Shell and gcloud

Getting Started with Cloud Shell and gcloud Qwiklab

  • Post comments:5 Comments
  • Reading time:10 mins read

Getting Started with Cloud Shell and gcloud | Overview

Cloud Shell provides you with command-line access to computing resources hosted on Google Cloud. Cloud Shell is a Debian-based virtual machine with a persistent 5-GB home directory, which makes it easy for you to manage your Google Cloud projects and resources. The gcloud command-line tool and other utilities you need are pre-installed in Cloud Shell, which allows you to get up and running quickly.

In this hands-on lab, you learn how to connect to computing resources hosted on Google Cloud via Cloud Shell with the gcloud tool.

You are encouraged to type the commands themselves, which reinforces the core concepts. Many labs will include a code block that contains the required commands. You can easily copy and paste the commands from the code block into the appropriate places during the lab.

What you’ll do

  • Practice using gcloud commands.
  • Connect to compute services hosted on Google Cloud.


Familiarity with standard Linux text editors such as vimemacs, or nano.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

It takes a few moments to provision and connects to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Getting Started with Cloud Shell and gcloud Qwiklab

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab completion.

You can list the active account name with this command:

gcloud auth list


Credentialed accounts: 
- <myaccount>@<mydomain>.com (active)

(Example output)

Credentialed accounts: 
- [email protected]

You can list the project ID with this command:

gcloud config list project


project = <project_ID>

(Example output)

project = qwiklabs-gcp-44776a13dea667a6

For full documentation of gcloud see the gcloud command-line tool overview.

After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud tool or other tools available on the virtual machine instance. Later in the lab, you will use your $HOME directory, which is used in persistent disk storage to store files across projects and between Cloud Shell sessions. Your $HOME the directory is private to you and cannot be accessed by other users.


Task 1: Configure your environment

In this section, you’ll learn about aspects of the development environment that you can adjust.

Understanding regions and zones

Certain Google Compute Engine resources live in regions or zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region denotes a region in the Central United States that has zones us-central1-aus-central1-bus-central1-c, and us-central1-f. The following image shows zones in their respective regions:

Getting Started with Cloud Shell and gcloud Qwiklab

Resources that live in a zone are referred to as zonal resources. Virtual machine instances and persistent disks live in a zone. If you want to attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP address. Learn more about regions and zones and see a complete list in Regions and Zones documentation.

To see what your default region and zone settings are, run the following commands:

gcloud config get-value compute/zone
gcloud config get-value compute/region

If the google-compute-default-region or google-compute-default-zone responses are (unset), which means no default zone or region is set.

Identify your default region and zone

  1. Copy your project ID to your clipboard or text editor. The project ID is listed in 2 places:
  • In the Google Cloud Console, on the Dashboard, under Project info. (Click Navigation menu (Navigation menu), and then click Home > Dashboard.)
  • On the Qwiklabs tab near your username and password.
  1. In Cloud Shell, run the following gcloud command, replacing <your_project_ID> with the project ID you copied:
gcloud compute project-info describe --project <your_project_ID>

Find the default zone and region metadata values in the output. You’ll use the zone (google-compute-default-zone) from the output later in this lab.If the google-compute-default-region and google-compute-default-zone keys and values are missing from the output, no default zone or region is set.

Set environment variables

Environment variables define your environment and help save time when you write scripts that contain APIs or executables.

  1. Create an environment variable to store your Project ID, replacing <your_project_ID> with the value for name from the gcloud compute project-info describe command you ran earlier:
export PROJECT_ID=<your_project_ID>
  1. Create an environment variable to store your Zone, replacing <your_zone> with the value for zone from the gcloud compute project-info describe command you ran earlier:
export ZONE=<your_zone>
  1. To verify that your variables were set properly, run the following commands:
echo $ZONE

If the variables were set correctly, the echo commands will output your Project ID and Zone.

Create a virtual machine with the gcloud tool

Use the gcloud tool to create a new virtual machine (VM) instance.

  1. To create your VM, run the following command:
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone $ZONE


Getting Started with Cloud Shell and gcloud Qwiklab

Command details

  • gcloud compute allows you to manage your Compute Engine resources in a format that’s simpler than the Compute Engine API.
  • instances create creates a new instance.
  • gcelab2 is the name of the VM.
  • The --machine-type flag specifies the machine type as n1-standard-2.
  • The --zone flag specifies where the VM is created.
  • If you omit the --zone flag, the gcloud tool can infer your desired zone based on your default properties. Other required instance settings, such as machine type and image, are set to default values if not specified in the create command.
  • To open help for the create command, run the following command:
gcloud compute instances create --help

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

Explore gcloud commands

The gcloud tool offers simple usage guidelines that are available by adding the -h flag (for help) onto the end of any gcloud command.

  1. Run the following command:
gcloud -h

You can access more verbose help by appending the --help flag onto a command or running the gcloud help command.

  1. Run the following command:
gcloud config --help

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

  1. Run the following command:
gcloud help config

The results of the gcloud config --help and gcloud help config commands are equivalent. Both return long, detailed help. Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

gcloud Global Flags govern the behavior of commands on a per-invocation level. Flags override any values set in SDK properties.

  1. View the list of configurations in your environment:
gcloud config list
  1. To see all properties and their settings:
gcloud config list --all
  1. List your components:
gcloud components list

This command displays the gcloud components that are ready for you to use in this lab.

Task 2: Install a new component

Next, you’ll install a gcloud component that makes working in the gcloud tool easier.

Auto-complete mode

gcloud interactive has auto prompting for commands and flags and displays inline help snippets in the lower section of the pane as the command is typed.

You can use dropdown menus to auto-complete static information, such as command and sub-command names, flag names, and enumerated flag values.

  1. Install the beta components:
sudo apt-get install google-cloud-sdk
  1. Enable the gcloud interactive mode:
gcloud beta interactive

When using the interactive mode, press TAB to complete the file path and resource arguments. If a dropdown menu appears, press TAB to move through the list, and press the spacebar to select your choice.

  1. To try this feature, start typing the following command, and use auto-complete to replace <your_vm> with an existing VM in your project:
gcloud compute instances describe <your_vm>

A list of commands is displayed below the Cloud Shell pane. Pressing F2 toggles the active help section to ON or OFF.

  1. To exit from the interactive mode, run the following command:

Task 3: Connect to your VM instance with SSH

gcloud compute makes connecting to your instances easy. The gcloud compute ssh command provides a wrapper around SSH, which takes care of authentication and the mapping of instance names to IP addresses.

  1. To connect to your VM with SSH, run the following command:
gcloud compute ssh gcelab2 --zone $ZONE


WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: [/usr/bin/ssh-keygen] will be executed to generate a key.
This tool needs to create the directory
[/home/gcpstaging306_student/.ssh] before being able to generate SSH Keys.
Do you want to continue? (Y/n)
  1. To continue, type Y.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase)
  1. To leave the passphrase empty, press ENTER twice.
  2. You don’t need to do anything here, so to disconnect from SSH and exit the remote shell, run the following command:

You should be back at your project’s command prompt.

Task 4: Use the Home directory

Now try out your Home directory. The contents of your Cloud Shell home directory persist across projects between all Cloud Shell sessions, even after the virtual machine is terminated and restarted.

  1. Change your current working directory:
cd $HOME
  1. Open your .bashrc configuration file by using the vi text editor:
vi ./.bashrc

The editor opens and displays the contents of the file.

  1. To exit the editor, press ESC, then type :wq, and then press Enter.

And you are done with Getting Started with Cloud Shell and gcloud Qwiklab!

Complete More Qwiklabs:

Creating a Virtual Machine Qwiklab

Kubernetes Engine Qwik Start

Get Google Cloud Stickers: Get it Now

This Post Has 5 Comments

Leave a Reply