使用 Cloud Shell 运行 gcloud 命令

了解如何在 Cloud Shell 中运行 gcloud CLI 命令。


如需遵循有关此任务的分步指导,请直接在 Cloud Shell Editor 中点击操作演示

操作演示


准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

设置 gcloud 属性

  1. 启动 Cloud Shell Editor
  2. 通过设置核心属性,可防止在向终端输出消息时使用颜色:

    gcloud config set disable_color true
    
  3. 取消设置 disable_color 属性:

    gcloud config unset disable_color
    
  4. 如果您已启用 Compute Engine API,请设置您的首选 Compute Engine 区域。将 REGION 替换为区域名称,例如 us-east1

    gcloud config set compute/region REGION
    

    或者,您也可以设置首选区域。 将 ZONE 替换为可用区名称,例如 us-east1-b

    gcloud config set compute/zone ZONE
    

    如需了解 Compute Engine 区域和可用区,请参阅区域和可用区

  5. 显示可用属性的列表:

    gcloud topic configurations
    

运行核心命令

  1. 列出其凭据存储在本地系统上的账号:

    gcloud auth list
    

    gcloud CLI 会显示存储了凭据的账号的列表,其中 ACCOUNT_EMAIL 表示一个或多个有效账号的列表:

    Credentialed Accounts
    ACTIVE  ACCOUNT
    \*      ACCOUNT_EMAIL
    
  2. 列出活跃 gcloud CLI 配置中的属性:

    gcloud config list
    

    gcloud CLI 会显示类似于以下内容的属性列表,其中 ACCOUNT_EMAILPROJECT_ID 会显示您的电子邮件地址和项目 ID:

    [core]
    account = ACCOUNT_EMAIL
    disable_usage_reporting = False
    project = PROJECT_ID
    
  3. 查看有关您的 gcloud CLI 安装和活跃配置的信息:

    gcloud info
    

查看有关 gcloud 命令的帮助

  1. 查看有关 gcloud compute instances create 命令的帮助:

    gcloud help compute instances create
    
  2. 查看 gcloud topic 下的可用命令:

    gcloud help topic
    
  3. 查看有关过滤的信息:

    gcloud topic filters
    
  4. 详细了解 gcloud 命令结构:

    gcloud topic command-conventions
    

后续步骤