Working with multiple accounts in gcloud command line SDK

 Here is a short one but useful because it's the kind of thing I forget often and I've found myself forgetting this several times, so here it goes:

GCloud tutorials seem oriented to having just one account but if you want to have multiple accounts (i.e. one for work and one personal) on the same machine, you'll find out the gcloud auth login command won't allow you to easily swap between accounts.

For this example, the work account will be called with default name and the personal account will be called rosana-personal

To get the first one set up, use the plain command gcloud auth login and the browser will launch the Google Oauth where you'll just select the account you want as main. Under the hood this is editing some configuration files.

This command doesn't support multi-account login today, so you'll need to use a different command to setup your secondary account: gcloud config configurations create rosana-personal and then use the first command again but with the flag --no-launch-browser flag: gcloud auth login --no-launch-browser.

And that's it. 

In summary

  1. gcloud auth login - Then, log in with what you want your main account to be.
  2. gcloud config configurations create <secondary-name>
  3. gcloud auth login --no-launch-browser and follow instructions.

Config directory

  • Lives under ~/.config/gcloud
  • You'll have one file per account under ~/.config/gcloud/configurations
    • Your default will be configurations/config_default
    • Your secondary will be configurations/config_<secondary-name>
  • The active configuration is saved in ~/.config/gcloud/active_config



Other useful commands for working with multiple accounts

Work with configurations:

  • gcloud config configurations list - List available accounts (a.ka. configurations)
  • gcloud config configurations activate <account-name> - Activate the configuration with the account name from the list. 

Edit currently active configuration:

  • gcloud config set project <project-ID>  - Will override the project ID
  • gcloud config set account <easy-name> - Will override the value of  the account variable in the config file for the currently active account.