GCP Command Cheatsheet - Google Cloud Platform Commands

Comprehensive GCP command cheatsheet for Google Cloud Platform. Quickly find and use essential gcloud and gsutil commands for managing projects, GKE, buckets, VMs, networking, and more.

GCP Command Cheatsheet

GCP Command Line Interface (CLI) Cheatsheet

This cheatsheet provides a quick reference for essential Google Cloud Platform (GCP) commands using the gcloud and gsutil tools. Master these commands to efficiently manage your cloud resources.

GCP Basics and Initialization

  • Check Version and Settings: gcloud version, gcloud info, gcloud components list
  • Initialize GCP SDK Profile: gcloud init (This command will guide you through setting up your project and authentication.)
  • List all available zones: gcloud compute zones list
  • Upgrade local SDK components: gcloud components update, or specify a version: gcloud components update --version 219.0.1

Cloud Storage (GCS) Bucket Operations

  • List buckets and files: gsutil ls, or for detailed listing: gsutil ls -lh gs://<bucket-name>
  • Download a file from a bucket: gsutil cp gs://<bucket-name>/<dir-path>/package-1.1.tgz .
  • Upload a file to a bucket: gsutil cp <filename> gs://<bucket-name>/<directory>/
  • View file content: gsutil cat gs://<bucket-name>/<filepath>/
  • Delete a file from a bucket: gsutil rm gs://<bucket-name>/<filepath>
  • Move or rename a file/object: gsutil mv <src-filepath> gs://<bucket-name>/<directory>/<dest-filepath>
  • Copy an entire folder recursively: gsutil cp -r ./conf gs://<bucket-name>/
  • Show disk usage for a bucket/directory: gsutil du -h gs://<bucket-name>/<directory>/
  • Create a new bucket: gsutil mb gs://<bucket-name>
  • Calculate file SHA1 checksum: gsha1sum <filename>, or shasum <filename>
  • Access gsutil help documentation: gsutil help, gsutil help cp, gsutil help options

GCP Project Management

  • List current project configuration: gcloud config list, or specifically for project: gcloud config list project
  • Show project information: gcloud compute project-info describe
  • Switch to a different project: gcloud config set project <project-id>

Google Kubernetes Engine (GKE) Operations

  • Display a list of authenticated accounts: gcloud auth list
  • Set the active account for gcloud: gcloud config set account <ACCOUNT>
  • Configure kubectl context for a GKE cluster: gcloud container clusters get-credentials <cluster-name>
  • Change the default compute region: gcloud config set compute/region us-west
  • Change the default compute zone: gcloud config set compute/zone us-west1-b
  • List all GKE container clusters: gcloud container clusters list

Identity and Access Management (IAM)

  • Authenticate using a service account key file: gcloud auth activate-service-account --key-file <key-file>
  • Display a list of credentialed accounts: gcloud auth list
  • Set the active account for gcloud: gcloud config set account <ACCOUNT>
  • Configure Docker authentication for GCP Container Registry: gcloud auth configure-docker
  • Print the access token for the active account: gcloud auth print-access-token, or refresh token: gcloud auth print-refresh-token
  • Revoke previously generated credentials: gcloud auth application-default revoke

Cloud Storage Bucket Security

  • Make all files in a bucket publicly readable: gsutil -m acl set -R -a public-read gs://<bucket-name>/
  • Configure gsutil authentication: gsutil config -a
  • Grant specific IAM roles to a user for a bucket: gsutil iam ch user:denny@gmail.com:objectCreator,objectViewer gs://<bucket-name>
  • Remove IAM roles from a user for a bucket: gsutil iam ch -d user:denny@gmail.com:objectCreator,objectViewer gs://<bucket-name>

Compute Engine Virtual Machines (VMs)

  • List all VM instances: gcloud compute instances list, or instance templates: gcloud compute instance-templates list
  • Show detailed information about a VM instance: gcloud compute instances describe "<instance-name>" --project "<project-name>" --zone "us-west2-a"
  • Stop a VM instance: gcloud compute instances stop instance-2
  • Start a VM instance: gcloud compute instances start instance-2
  • Create a new VM instance: gcloud compute instances create vm1 --image image-1 --tags test --zone "<zone>" --machine-type f1-micro
  • SSH into a VM instance: gcloud compute ssh --project "<project-name>" --zone "<zone-name>" "<instance-name>"
  • Download files from a VM instance: gcloud compute copy-files example-instance:~/REMOTE-DIR ~/LOCAL-DIR --zone us-central1-a
  • Upload files to a VM instance: gcloud compute copy-files ~/LOCAL-FILE-1 example-instance:~/REMOTE-DIR --zone us-central1-a

Compute Engine Disks and Volumes

  • List all persistent disks: gcloud compute disks list
  • List all available disk types: gcloud compute disk-types list
  • List all disk snapshots: gcloud compute snapshots list
  • Create a snapshot of a disk: gcloud compute disks snapshot <diskname> --snapshotname <name1> --zone $zone

VPC Network Management

  • List all VPC networks: gcloud compute networks list
  • Get detailed information about a network: gcloud compute networks describe <network-name> --format json
  • Create a new VPC network: gcloud compute networks create <network-name>
  • Create a subnet within a network: gcloud compute networks subnets create subnet1 --network net1 --range 10.5.4.0/24
  • Reserve a static external IP address: gcloud compute addresses create --region us-west2-a vpn-1-static-ip
  • List all reserved IP addresses: gcloud compute addresses list
  • Describe a specific IP address resource: gcloud compute addresses describe <ip-name> --region us-central1
  • List all network routes: gcloud compute routes list

Cloud DNS Operations

  • List all record-sets in a managed zone: gcloud dns record-sets list --zone my_zone
  • List the first 10 DNS records in a zone: gcloud dns record-sets list --zone my_zone --limit=10

Firewall Rule Management

  • List all firewall rules: gcloud compute firewall-rules list
  • List all forwarding rules: gcloud compute forwarding-rules list
  • Describe a specific firewall rule: gcloud compute firewall-rules describe <rule-name>
  • Create a new firewall rule: gcloud compute firewall-rules create my-rule --network default --allow tcp:9200 tcp:3306
  • Update an existing firewall rule: gcloud compute firewall-rules update default --network default --allow tcp:9200 tcp:9300

Images and Containers

  • List all available images: gcloud compute images list
  • List all GKE container clusters: gcloud container clusters list
  • Set kubectl context for a GKE cluster: gcloud container clusters get-credentials <cluster-name>

Cloud SQL Instances

  • List all Cloud SQL instances: gcloud sql instances list

Load Balancing and Services

  • List all backend services: gcloud compute backend-services list
  • List all health check endpoints: gcloud compute http-health-checks list
  • List all URL maps: gcloud compute url-maps list

For more detailed information and advanced usage, refer to the official gcloud CLI documentation and gsutil documentation.