Logging into Google Cloud Platform (GCP) from the command line involves using the gcloud command-line tool. Here are the steps to do this:
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:
Windows
macOS
sh
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
Linux
sh
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
./google-cloud-sdk/install.sh
exec -l $SHELL
Note: Replace the version number in the URLs with the latest version if necessary.
After installing the SDK, initialize it using the following command:
sh
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.
To log in to your Google Cloud account, use the following command:
sh
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.
If you have multiple projects in GCP, you can set a default project for your gcloud commands:
sh
gcloud config set project PROJECT_ID
Replace PROJECT_ID with the ID of your Google Cloud project.
You can verify that you are logged in and see the current configuration settings by running:
sh
gcloud config list
This will display the currently active account and project, among other settings.
List Projects: To list all available projects:
sh
gcloud projects list
Change Account: If you need to log in with a different account:
sh
gcloud auth login --no-launch-browser
Activate Service Account: For automation purposes, you might want to use a service account:
sh
gcloud auth activate-service-account --key-file=path/to/key-file.json