Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 55Cloud · Azure

How to log in to Azure and choose a subscription

In Azure one login can see many tenants (directories) and many subscriptions (billing and resource containers) — and every az command goes to the current subscription. Most "I deleted the wrong thing" stories start with the wrong one. This how-to logs in (also without a browser and in a specific tenant), lists and sets the subscription, makes it visible, and shows the right login for scripts and pipelines (managed identity, OIDC — no secrets).

  • 5 Lessons
  • 1 Commands

$ run as a normal user# run as root (or with sudo)

55.1

The quick answer

az login
az account list -o table
az account set --subscription "Prod-Shop"
az account show --query '{subscription:name, id:id, tenant:tenantId, user:user.name}' -o table

Commands in this lesson

55.2

Step by step

  1. Log in — the browser opens; newer CLI versions then show a list of your subscriptions to pick from:
az login
az login --use-device-code
az login --tenant contoso.onmicrosoft.com

--use-device-code: on a server without a browser. --tenant: when you are a guest in other tenants (customers, partners).

  1. List and choose — use the name or ID, and check after every switch:
az account list --query "[].{name:name, id:id, default:isDefault, state:state}" -o table
Name        Id                                    Default    State
----------  ------------------------------------  ---------  -------
Dev-Shop    11111111-2222-3333-4444-555555555555  True       Enabled
Prod-Shop   66666666-7777-8888-9999-000000000000  False      Enabled
az account set --subscription 66666666-7777-8888-9999-000000000000
az account show --query name -o tsv
Prod-Shop
  1. One command in another subscription without switching:
az group list --subscription Dev-Shop -o table
  1. Make it visible — add the subscription to your prompt so you always see where you are:
~/.bashrc
azsub() { az account show --query name -o tsv 2>/dev/null; }
PS1='[\u@\h \W az:$(azsub)]\$ '

(It calls az each prompt — fine on a laptop; on slow links show it only when you need it.)

  1. Logins for automation — never your own account, never a stored client secret if you can avoid it:
WhereLogin
Azure VM, App Service, Container AppsManaged identity: az login --identity
GitHub Actions, Azure DevOpsWorkload identity federation (OIDC) — azure/login with client ID, tenant ID, subscription ID, no secret
Your laptopYour personal login with MFA
  1. Log out on shared machines:
az logout
az account clear

Commands in this lesson

55.3

Check that it worked

az account show prints the subscription you want and your user, and a read command shows the resources you expect:

az group list -o table

Commands in this lesson

55.4

Common problems

ProblemCauseFix
No subscriptions found for …Wrong tenant, or no role on any subscription.az login --tenant TENANT; ask for a role (Reader at least).
AADSTS50076 / MFA requiredThe tenant needs MFA for this login.Log in again interactively (browser) with --tenant.
Device code login is blockedConditional Access blocks the device code flow (common now).Log in with the browser on a managed device.
Changes went to the wrong subscriptionThe default was another one.Always az account show first; use --subscription in scripts.
The subscription … is disabledBilling problem or the subscription was cancelled.Ask the owner / billing admin.
Script works for you, fails in CICI has no personal login.Managed identity or OIDC federation for the pipeline.

Commands in this lesson

55.5

Go further

1Every az command goes to which subscription?

The current default one (az account show) — unless you pass --subscription.

2How should a GitHub Actions workflow log in to Azure?

With workload identity federation (OIDC): no client secret is stored.

Commands in this lesson

All commands in this chapter