Cloud Shell & the gcloud CLI
By the end of this lesson you will be able to open Cloud Shell, run gcloud commands, switch between projects with named configurations, enable APIs, and print exactly the output you need with --format and --filter. These skills are used in every later lesson.
Cloud Shell: a terminal in your browser
Cloud Shell is a free Linux terminal that opens inside the console. Click the terminal icon at the top right of the console. After a few seconds a panel opens at the bottom with a prompt like this:
priya@cloudshell:~ (jaipur-coaching-dev)$The part in brackets is your current project. Behind the panel is a small temporary virtual machine that Google runs for you. It comes ready with:
- the gcloud CLI, already signed in as you;
- tools such as Git, Docker, Python, pip, npm, Vim and Nano;
- a 5 GB persistent home directory (
$HOME) that survives between sessions; - Cloud Shell Editor, a browser code editor opened with the "Open Editor" button.
Remember three limits. Files outside $HOME disappear when the session ends. A session ends after about an hour of inactivity. If you do not open Cloud Shell for 120 days, Google deletes your home directory (after an email warning). There is also a weekly usage limit, described on the Cloud Shell limits page.
Cloud Shell is like a cyber café computer that already has all your software installed and keeps one locker ($HOME) for you. Do not leave valuables on the desk.
Installing gcloud on your own computer
For daily work you may prefer gcloud on your laptop. Download the installer for Windows, macOS or Linux from the official install page, then run:
gcloud initgcloud init opens a browser to sign in, then asks you to choose a project and, optionally, a default region. Three sign-in commands are worth knowing:
| Command | What it does |
|---|---|
gcloud auth login | Signs you in for gcloud commands |
gcloud auth application-default login | Gives your local code (Python, Terraform) credentials for testing |
gcloud auth list | Shows which accounts are signed in |
gcloud auth list Credentialed Accounts
ACTIVE ACCOUNT
* priya@example.com
To set the active account, run:
$ gcloud config set account `ACCOUNT`The star marks the active account.
How a gcloud command is built
Every command follows the same shape, like a postal address that goes from big to small:
gcloud compute instances create web-1 --zone=asia-south1-a
(group) (group) (verb) (name) (flag)Groups match products: compute, run, storage, sql, iam, projects and so on. Some features first appear in gcloud beta or gcloud alpha. Prefer the normal commands for anything important. When you are lost, add --help to any command.
Step by step: properties and configurations
gcloud keeps settings called properties, such as your project and default region.
Step 1. Set the project and region:
gcloud config set project jaipur-coaching-dev
gcloud config set compute/region asia-south1Updated property [core/project]. Updated property [compute/region].
Step 2. Check what is set:
gcloud config list[compute] region = asia-south1 [core] account = priya@example.com disable_usage_reporting = True project = jaipur-coaching-dev Your active configuration is: [default]
Step 3. Create a second named configuration for a college lab project. A configuration is a saved set of properties, like having two SIM cards in one phone and switching between them.
gcloud config configurations create college
gcloud config set project college-lab-2026
gcloud config configurations listCreated [college]. Activated [college]. Updated property [core/project]. NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION college True priya@example.com college-lab-2026 default False priya@example.com jaipur-coaching-dev asia-south1
Step 4. Switch back whenever you like with gcloud config configurations activate default. This prevents the classic mistake of running a command in the wrong project.
Enabling APIs
Most products must be enabled in a project before you use them. Enable several at once:
gcloud services enable compute.googleapis.com run.googleapis.com
gcloud services list --enabled --filter="name:run.googleapis.com"Operation "operations/acf.p2-987654321098-4c1f3d2e-7a8b-4f60-9e21-0d5c6b7a8f90" finished successfully. NAME TITLE run.googleapis.com Cloud Run Admin API
If you forget, gcloud usually offers to enable the API for you, or shows an error that names the API to enable.
Shaping output with --format and --filter
gcloud output is a table by default. Two flags make it far more useful:
--filterkeeps only matching rows, like a filter on a spreadsheet column.--formatchanges the layout:table(...),json,yaml,csv(...)orvalue(...).
gcloud compute instances list \
--filter="status=RUNNING AND zone:asia-south1" \
--format="table(name, zone.basename(), status)"NAME ZONE STATUS web-1 asia-south1-a RUNNING web-2 asia-south1-b RUNNING
The basename() part shortens a long zone URL to just asia-south1-a. Use value(...) when you want to put a result into a shell variable:
PROJECT_NUMBER=$(gcloud projects describe jaipur-coaching-dev --format="value(projectNumber)")
echo "$PROJECT_NUMBER"987654321098
Common mistakes
- Running commands in the wrong project. Check
gcloud config listor the Cloud Shell prompt first. Named configurations help. - Saving work outside
$HOMEin Cloud Shell. It is lost when the session ends. - Mixing up
gcloud auth loginandgcloud auth application-default login. The first is for gcloud itself; the second is for code and tools on your machine. - Parsing table output in scripts. Use
--format="value(...)"orjsoninstead; tables can change.
Key points
- Cloud Shell is a browser terminal with gcloud ready, a 5 GB persistent
$HOME, and temporary everything else. - Install gcloud locally from the official page and start with
gcloud init. - Commands follow
gcloud GROUP ... VERB NAME --flags; add--helpwhen unsure. - Properties and named configurations store your project, account and region.
--filterpicks rows and--formatcontrols the layout; usevalue(...)in scripts.
Exercise
Kabir works on two projects: kabir-shop-dev and kabir-shop-prod. Write commands to create two named configurations, dev and prod, each set to the right project and to region asia-south1. Then write a command that lists only RUNNING VM names in the active project, one per line.
Show solution
gcloud config configurations create dev
gcloud config set project kabir-shop-dev
gcloud config set compute/region asia-south1
gcloud config configurations create prod
gcloud config set project kabir-shop-prod
gcloud config set compute/region asia-south1
gcloud compute instances list --filter="status=RUNNING" --format="value(name)"Creating a configuration also activates it, so the set commands after each one apply to that configuration. value(name) prints only the names, one per line.
Quick Quiz
1. Which files survive after a Cloud Shell session ends?
Only the 5 GB persistent $HOME survives; the rest of the VM is temporary.
2. What does --format="value(name)" do?
value(...) prints raw field values, which is ideal for scripts.
3. Why use named gcloud configurations?
A configuration saves account, project and region together, so you switch all of them with one command.
Learn Google Cloud with a mentor
Free lessons get you started. Our Cloud & DevOps training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
