Skip to main content

How to Sign In to Two Accounts at Once in the Azure CLI

· 15 min read
Jagdish Kumawat
Founder @ Dewiride

The Azure CLI keeps your sign-in in a configuration folder, ~/.azure by default, and the environment variable AZURE_CONFIG_DIR points a command at another folder. Sign a second account in to a folder of its own, and both accounts stay signed in at once: each az command uses the account of the folder you give it.

Why a folder of its own? A sign-in changes the folder it's saved in. Microsoft's guide to managing subscriptions says to Use az login to change the active tenant and update the subscription list to which you belong. Sign the second account in to the default folder, and those changes land in the folder your own commands use.

AZURE_CONFIG_DIR keeps the two apart. It tells the Azure CLI which configuration folder to use, so the second account's sign-in goes to its own folder and yours stays where it is.

Syntax​

Syntax
AZURE_CONFIG_DIR=<folder> az <command>
PartWhat it is
AZURE_CONFIG_DIRThe environment variable that sets the Azure CLI's configuration folder. When it isn't set, the folder is $HOME/.azure on Linux and macOS, and %USERPROFILE%\.azure on Windows
<folder>The second account's own folder, which no other account uses: for example ~/.azure-test-user in bash and zsh, or Join-Path $HOME ".azure-test-user" in PowerShell
<command>Any Azure CLI command. It runs with the sign-in saved in <folder>
AZURE_CONFIG_DIR=<folder> in front of a commandbash and zsh: sets the variable for that one command. The bash manual says These assignment statements affect only the environment seen by that command. The next command without it uses the default folder again
$env:AZURE_CONFIG_DIR = <folder>PowerShell: sets the variable for the rest of the session, so every az command after it uses <folder>
Remove-Item Env:AZURE_CONFIG_DIRPowerShell: removes the variable, so the az commands after it use the default folder again

In PowerShell, the variable stays until you remove it or end the session: When you change environment variables in PowerShell, the change affects only the current session. The az command still sees it, because environment variables are inherited by child processes.

The Example​

My own account, user@example.com, is signed in to the Azure CLI in the default folder, ~/.azure. A second account, test.manager@contoso.onmicrosoft.com, is a test user called Test Manager in the same Microsoft Entra tenant, and it has no Azure subscription. I sign it in to a folder of its own, ~/.azure-test-user, use both accounts side by side, and then sign the second one out and delete its folder. My real address and name are replaced with user@example.com and Azure User in the outputs.

I ran every step on macOS with Azure CLI 2.93.0, in zsh and in PowerShell 7.6.

Step 1: Check Which Account the Azure CLI Uses Now​

Before the second sign-in, check which account the default folder holds:

Terminal
az account show --query user.name --output tsv
Output
user@example.com

The command is the same in every shell, and I got this output in both zsh and PowerShell.

  • az account show shows the subscription the Azure CLI uses. Its reference: Get the details of a subscription. Without a subscription name, it shows the details of the default subscription, and those details include the account that signed in.
  • --query user.name keeps only that account's name. The Azure CLI uses the --query parameter to execute a JMESPath query on the results of commands, and user.name picks name inside user.
  • --output tsv prints the value as plain text, without the quotes JSON would put around it.

Step 2: Sign the Second Account In to Its Own Folder​

Terminal
AZURE_CONFIG_DIR=~/.azure-test-user az login --tenant contoso.onmicrosoft.com --allow-no-subscriptions
  • AZURE_CONFIG_DIR=~/.azure-test-user, or $env:AZURE_CONFIG_DIR in PowerShell, sends this sign-in to the second account's folder. Join-Path combines a path and child-path into a single path: your home folder and .azure-test-user.
  • az login signs in. On macOS and Linux, it initiates authorization code flow and opens the default browser to load an Azure sign-in page.
  • --tenant contoso.onmicrosoft.com signs in to that Microsoft Entra tenant. The value of this argument can either be a .onmicrosoft.com domain or the Azure object ID for the tenant. Use your own tenant's domain, or its ID: How to Find Your Microsoft Entra Tenant ID shows where to copy it.
  • --allow-no-subscriptions lets an account without an Azure subscription sign in. The reference describes it as Support accessing tenants without subscriptions. My test user has no subscription, so it needs this option.
  • Remove-Item Env:AZURE_CONFIG_DIR, the third PowerShell line, removes the variable again, so the commands after it use the default folder.

I used the browser sign-in, not a device code: my tenant blocks sign-in with a device code, as Fixed: "You don't have access to this" (Error Code 53003) When You Sign In to the Azure CLI with a Device Code shows.

The sign-in page opened in a browser window that already had the test user signed in, so it asked me to pick an account:

Microsoft's Pick an account page listing Test Manager, test.manager@contoso.onmicrosoft.com, as Signed in, above a Use another account option and a Back button.

Pick the second account. If the page shows only your own account, choose Use another account and sign in as the second one. After I picked Test Manager, the browser said the sign-in was done:

Browser page with the heading You have logged into Microsoft Azure! and the line You can close this window, or we will redirect you to the Azure CLI documentation in 1 minute.

Back in the terminal, the Azure CLI listed what the account can use and asked which one to keep. N/A(tenant level account) means a sign-in to the tenant without a subscription, the kind --allow-no-subscriptions allows. It was the only choice and already the default, marked with *, so I pressed Enter. The output was the same in zsh and PowerShell:

Output (IDs masked)
A web browser has been opened at https://login.microsoftonline.com/11111111-1111-1111-1111-111111111111/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.

Retrieving subscriptions for the selection...

[Tenant and subscription selection]

No Subscription name Subscription ID Tenant
----- ------------------------- ------------------------------------ ------------------------------------
[1] * N/A(tenant level account) 11111111-1111-1111-1111-111111111111 11111111-1111-1111-1111-111111111111

The default is marked with an *; the default tenant is '11111111-1111-1111-1111-111111111111' and subscription is 'N/A(tenant level account)' (11111111-1111-1111-1111-111111111111).

Select a subscription and tenant (Type a number or Enter for no changes):

Tenant: 11111111-1111-1111-1111-111111111111
Subscription: N/A(tenant level account) (11111111-1111-1111-1111-111111111111)

[Announcements]
With the new Azure CLI login experience, you can select the subscription you want to use more easily. Learn more about it and its configuration at https://go.microsoft.com/fwlink/?linkid=2271236

If you encounter any problem, please open an issue at https://aka.ms/azclibug

[Warning] The login output has been updated. Please be aware that it no longer displays the full list of available subscriptions by default.

macOS-style terminal window showing az login with AZURE_CONFIG_DIR set to ~/.azure-test-user, the tenant contoso.onmicrosoft.com and --allow-no-subscriptions, the one tenant-level account N/A(tenant level account) marked as the default, and after Enter the tenant and subscription it kept.

The second account is now signed in, in ~/.azure-test-user only.

Step 3: Use Both Accounts​

Each az command now uses the account of the folder it gets. Ask both folders who they are: the account name from Step 1, and the name Microsoft Entra ID has for the user.

Terminal
AZURE_CONFIG_DIR=~/.azure-test-user az account show --query user.name --output tsv
AZURE_CONFIG_DIR=~/.azure-test-user az ad signed-in-user show --query displayName --output tsv
az account show --query user.name --output tsv
az ad signed-in-user show --query displayName --output tsv
Output
test.manager@contoso.onmicrosoft.com
Test Manager
user@example.com
Azure User
  • The first two commands run with the second account's folder. The last two run without the variable, so they use the default folder.
  • az ad signed-in-user show asks Microsoft Graph about the account: Get the details for the currently logged-in user. --query displayName keeps only the user's display name. Because it calls Microsoft Graph, it also shows that the second sign-in works, not only that its name is saved.

macOS-style terminal window showing az account show and az ad signed-in-user show run twice: with AZURE_CONFIG_DIR set to ~/.azure-test-user they print test.manager@contoso.onmicrosoft.com and Test Manager, and without it user@example.com and Azure User.

Two accounts, signed in at once: the second folder answers as Test Manager, and the default folder still answers as me.

Step 4: Sign the Second Account Out and Delete Its Folder​

When you no longer need the second account, sign it out and delete its folder:

Terminal
AZURE_CONFIG_DIR=~/.azure-test-user az logout
rm -rf ~/.azure-test-user
ls ~/.azure-test-user
az account show --query user.name --output tsv
Output
ls: /Users/you/.azure-test-user: No such file or directory
user@example.com
  • az logout with the second account's folder signs that account out: Log out to remove access to Azure subscriptions. It printed nothing.
  • rm -rf, or Remove-Item -Recurse -Force in PowerShell, deletes the folder with everything in it. rm -r removes the whole folder tree and -f doesn't ask first; in PowerShell, -Recurse deletes the items in the specified locations and in all child items of the locations.
  • ls and Test-Path check that the folder is gone: ls reports No such file or directory, and Test-Path prints False.
  • az account show without the variable shows that my own account is still signed in.

macOS-style terminal window showing az logout with AZURE_CONFIG_DIR set to ~/.azure-test-user, rm -rf on that folder, ls reporting No such file or directory, and az account show still printing user@example.com.

The folder can come back

When I listed the folder a little after the PowerShell cleanup, ~/.azure-test-user was back, holding only logs/telemetry.log. The last az command that used the folder, az logout, starts a separate telemetry upload process as it ends, and when the folder is deleted right after it, that process can create the folder again for its log. Fixed: A Deleted Azure CLI Configuration Folder Comes Back with logs/telemetry.log shows why, and how to delete it for good.

Common Mistakes​

MistakeWhat happensDo this instead
Running az login for the second account without AZURE_CONFIG_DIRThe sign-in goes to the default folder, and az login changes the active tenant and subscription list your own commands useGive the second account's commands their own folder
export AZURE_CONFIG_DIR=~/.azure-test-user in bash or zshEvery command after it in that shell uses the second account's folderPut AZURE_CONFIG_DIR=<folder> in front of each command instead
Leaving $env:AZURE_CONFIG_DIR set in PowerShellEvery az command after it in that session uses the second accountRun Remove-Item Env:AZURE_CONFIG_DIR when you're done

Troubleshooting​

SymptomLikely causeFix
"You don't have access to this", Error Code 53003, after entering a device codeYour tenant blocks the device code flow with Conditional AccessSign in without --use-device-code (Fixed)
The deleted folder is back, with only logs/telemetry.log in itThe telemetry upload process of the last az command recreated itDelete it again (Fixed)
az account show prints the second account where you expected yoursAZURE_CONFIG_DIR is still set in your PowerShell sessionRemove-Item Env:AZURE_CONFIG_DIR

FAQ​

Where is the second account's sign-in saved?​

In the folder you set. Microsoft's tips page for the Azure CLI says that with AZURE_CONFIG_DIR set, Azure CLI commands in that script save the configuration and token cache to the configured location instead of the default ~/.azure folder. Your own sign-in stays in ~/.azure, as Step 3 shows.

Can both accounts run commands at the same time?​

Separate folders are what Microsoft recommends for that: If you run Azure CLI commands concurrently on the same machine, write conflicts can happen if multiple Azure CLI commands write to the same Microsoft Authentication Library (MSAL) token cache. With its own folder, each account's commands write to their own token cache.

Does this work on Windows?​

I ran these steps on macOS only. On Windows, az login uses Web Account Manager (WAM) by default instead of a browser page, so Step 2 looks different there: WAM is a Windows 10+ component that acts as an authentication broker. The variable is the same; the tips page also shows it for Cmd, as set AZURE_CONFIG_DIR=D:\my\config\dir. I haven't tried two accounts with WAM.

Conclusion​

To keep two accounts signed in to the Azure CLI at once, give the second one its own configuration folder: AZURE_CONFIG_DIR=<folder> in front of its commands in bash and zsh, or $env:AZURE_CONFIG_DIR set before and removed after them in PowerShell. Commands without the variable keep using your own sign-in in ~/.azure. When you're done, sign the second account out with az logout in its folder, delete the folder, and check again a little later that it stayed deleted.

Additional Resources​