Google Cloud Platform Logging in to GCP from command line

0 votes
I was sure it will be simple but couldn't find any documentation or resolution. I'm trying to write a script using gcloud to perform some operations in my GCP instances.

Is there anyway to login/authenticate using gcloud via command line only?

Thanks
Apr 6, 2022 in GCP by Rahul
• 3,380 points
1,014 views

1 answer to this question.

0 votes

You have a couple of options here (depending on what exactly you're trying to do).

The first option is to log in using the --no-launch-browser option. This still requires interaction from a human user, but doesn't require a browser on the machine you're using:

> gcloud auth login --no-launch-browser
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&prompt=select_account&response_type=code&client_id=32555940559.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&access_type=offline


Enter verification code: *********************************************
Saved Application Default Credentials.

You are now logged in as [user@example.com].
Your current project is [None].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

The non-interactive option involves service accounts. The linked documentation explains them better than I can, but the short version of what you need to do is as follows:

  1. Create a service account in the Google Developers Console. Make sure it has the appropriate "scopes" (these are permissions that determine what this service account can do. Download the corresponding JSON key file.

  2. Run gcloud auth activate-service-account --key-file <path to key file>.

Note that Google Compute Engine VMs come with a slightly-different service account; the difference is described here.

answered Apr 6, 2022 by Korak
• 5,820 points

Related Questions In GCP

+2 votes
1 answer

How to do SSH in GCP instance from command line?

Hi@akhtar, You can do SSH to GCP instance ...READ MORE

answered Aug 24, 2020 in GCP by MD
• 95,440 points
860 views
+2 votes
1 answer

How to launch one instance on GCP Cloud from command line?

Hi@akhtar, You can compute service in GCP to launch ...READ MORE

answered Aug 24, 2020 in GCP by MD
• 95,440 points
730 views
0 votes
1 answer

How to authenticate with the correct account in GCP from Command Line?

Hi@akhtar, You can check your accounts from the ...READ MORE

answered Sep 24, 2020 in GCP by MD
• 95,440 points
422 views
0 votes
1 answer

Google cloud: How to list all service-accounts from all Projects in GCP

It's been noted by the commenters that ...READ MORE

answered Nov 10, 2022 in GCP by Tejashwini
• 3,820 points
961 views
0 votes
0 answers

How to test a Cloud Function in Google Cloud Platform (GCP)?

I've been looking everywhere for the answer ...READ MORE

Nov 9, 2022 in GCP by Ashwini
• 5,430 points
263 views
0 votes
1 answer

How do I get to know my transaction history in Google Cloud Platform?

These are the following steps to look at the transaction history within the Google ...READ MORE

answered Oct 7, 2019 in GCP by Sirajul
• 59,230 points
1,661 views
0 votes
1 answer

How to check the status of logging in Google Cloud Storage?

Using gsutil, you can check logging by ...READ MORE

answered Nov 22, 2019 in GCP by Sirajul
• 59,230 points
1,247 views
0 votes
1 answer

Google cloud: How to list all service-accounts from all Projects in GCP

Your code sample suggests you want the ...READ MORE

answered Mar 14, 2022 in GCP by Korak
• 5,820 points
3,570 views
0 votes
1 answer

Google Cloud VPN - Egress from GCP to on-premise pricing clarification

IPSec traffic are charged as if the ...READ MORE

answered Mar 20, 2022 in GCP by Korak
• 5,820 points
753 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP