Installing and initializing the gcloud CLI
Bash
# (Linux)
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
# (· settings)
gcloud init
# settings check
gcloud config list
gcloud auth listCreating a VM instance
Bash
# VM
gcloud compute instances create my-vm \
--project=my-project-id \
--zone=asia-northeast3-a \
--machine-type=e2-medium \
--image-family=ubuntu-2204-lts \
--image-project=ubuntu-os-cloud \
--boot-disk-size=20GB \
--boot-disk-type=pd-ssd \
--tags=web-server \
--metadata=startup-script='#!/bin/bash
apt update && apt install -y nginx'
# list
gcloud compute instances list
#
gcloud compute instances describe my-vm --zone=asia-northeast3-a
# when / /
gcloud compute instances start my-vm --zone=asia-northeast3-a
gcloud compute instances stop my-vm --zone=asia-northeast3-a
gcloud compute instances delete my-vm --zone=asia-northeast3-aConfiguring firewall rules
Bash
# HTTP / HTTPS (web-server )
gcloud compute firewall-rules create allow-web \
--allow tcp:80,tcp:443 \
--source-ranges 0.0.0.0/0 \
--target-tags web-server \
--description "Allow HTTP and HTTPS"
# IP SSH
gcloud compute firewall-rules create allow-ssh-from-office \
--allow tcp:22 \
--source-ranges 203.0.113.0/24 \
--target-tags web-server
# (VPC )
gcloud compute firewall-rules create allow-internal \
--allow all \
--source-ranges 10.0.0.0/8 \
--network default
# list
gcloud compute firewall-rules listConnecting via SSH
Bash
# gcloud SSH
gcloud compute ssh my-vm --zone=asia-northeast3-a
# external IP check
gcloud compute instances describe my-vm \
--zone=asia-northeast3-a \
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'
# SSH
gcloud compute project-info add-metadata \
--metadata ssh-keys="myuser:$(cat ~/.ssh/id_rsa.pub)"
#
gcloud compute ssh my-vm --zone=asia-northeast3-a \
-- -L 5432:localhost:5432Disk snapshots
Bash
#
gcloud compute disks snapshot my-vm \
--zone=asia-northeast3-a \
--snapshot-names my-vm-snap-$(date +%Y%m%d)
# list
gcloud compute snapshots list
#
gcloud compute disks create restored-disk \
--source-snapshot my-vm-snap-20250527 \
--zone=asia-northeast3-a
#
gcloud compute snapshots delete my-vm-snap-20250527
#
gcloud compute resource-policies create snapshot-schedule daily-backup \
--region=asia-northeast3 \
--max-retention-days=7 \
--daily-schedule \
--start-time=03:00Changing the machine type (scale-up)
Bash
# after
gcloud compute instances stop my-vm --zone=asia-northeast3-a
gcloud compute instances set-machine-type my-vm \
--zone=asia-northeast3-a \
--machine-type=e2-standard-4
gcloud compute instances start my-vm --zone=asia-northeast3-aCost-saving tips
- Preemptible instances: up to 80% cheaper; can be interrupted at any time
- Committed use discounts: up to 57% off with 1-year or 3-year commitments
- e2-series machine types are cheaper and perform better than n1
- Stop unused VMs (you only pay for disk)
Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Google Cloud documentation ↗Linux man pages ↗
Questions & answers
Ask a question about this guide. We'll review and reply.