$ run as a normal user# run as root (or with sudo)
The quick answer
Step by step
- Log in — the browser opens; newer CLI versions then show a list of your subscriptions to pick from:
--use-device-code: on a server without a browser. --tenant: when you are a guest in other tenants (customers, partners).
- 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- One command in another subscription without switching:
az group list --subscription Dev-Shop -o table- Make it visible — add the subscription to your prompt so you always see where you are:
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.)
- Logins for automation — never your own account, never a stored client secret if you can avoid it:
| Where | Login |
|---|---|
| Azure VM, App Service, Container Apps | Managed identity: az login --identity |
| GitHub Actions, Azure DevOps | Workload identity federation (OIDC) — azure/login with client ID, tenant ID, subscription ID, no secret |
| Your laptop | Your personal login with MFA |
- Log out on shared machines:
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 tableCommon problems
| Problem | Cause | Fix |
|---|---|---|
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 required | The tenant needs MFA for this login. | Log in again interactively (browser) with --tenant. |
| Device code login is blocked | Conditional Access blocks the device code flow (common now). | Log in with the browser on a managed device. |
| Changes went to the wrong subscription | The default was another one. | Always az account show first; use --subscription in scripts. |
The subscription … is disabled | Billing problem or the subscription was cancelled. | Ask the owner / billing admin. |
| Script works for you, fails in CI | CI has no personal login. | Managed identity or OIDC federation for the pipeline. |
Go further
- Command pages: az.
- Next how-tos: Get the kubeconfig of EKS, AKS or GKE, Log in to AWS with SSO, Create an Azure VM with the CLI.
- Full guide: Install Azure CLI and log in.
- Learn it: AZ-104 — Management groups and subscriptions, AZ-104 — Azure CLI, AZ-104 — Subscriptions, GitHub Actions — OIDC to Azure.
- Practise: Lab: govern a subscription with RBAC, Policy, tags, a lock and a budget.
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.