Logging into Google Cloud Platform (GCP) from the command line involves using the gcloud command-line tool. Here are the steps to do this:

Step 1: Install the Google Cloud SDK

If you haven’t already installed the Google Cloud SDK, you’ll need to do so first. Follow the installation instructions for your operating system:

  1. Download the Google Cloud SDK installer.
  2. Run the installer and follow the prompts.

curl https://sdk.cloud.google.com | bash
exec -l $SHELL

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-367.0.0-linux-x86_64.tar.gz
tar -xf google-cloud-sdk-367.0.0-linux-x86_64.tar.gz
exec -l $SHELL

Note: Replace the version number in the URLs with the latest version if necessary.

Step 2: Initialize the SDK

After installing the SDK, initialize it using the following command:
gcloud init
This will guide you through the setup process, including logging in to your Google account, setting the default project, and setting the default region/zone.

Step 3: Log In to GCP

To log in to your Google Cloud account, use the following command:
gcloud auth login

This command will open a web browser window prompting you to log in to your Google account. Once you’ve logged in, the gcloud tool will have the necessary credentials to access your GCP resources.

Step 4: Set the Default Project (Optional)

If you have multiple projects in GCP, you can set a default project for your gcloud commands:
gcloud config set project PROJECT_ID
Replace PROJECT_ID with the ID of your Google Cloud project.

Step 5: Verify the Configuration

You can verify that you are logged in and see the current configuration settings by running:
gcloud config list
This will display the currently active account and project, among other settings.

Additional Tips

List Projects: To list all available projects:
gcloud projects list

Change Account: If you need to log in with a different account:
gcloud auth login --no-launch-browser

Activate Service Account: For automation purposes, you might want to use a service account:
gcloud auth activate-service-account --key-file=path/to/key-file.json

Support On Demand!