Keywords: Azure CLI | Default Subscription | az account set
Abstract: This article provides a detailed guide on how to change the default subscription in Azure CLI 2.0 when managing multiple subscriptions. It analyzes common issue scenarios, presents the correct syntax and parameters for the az account set command, and explains the differences between subscription ID and name. The discussion also covers configuration file storage and verification steps to ensure accurate context switching.
Problem Context and Scenario Analysis
In the Azure cloud platform, users often have multiple subscriptions to manage different projects or environments. When using Azure CLI 2.0 for resource management, the default subscription determines the target scope for subsequent commands. A common issue reported by users is that the default subscription does not change successfully despite attempts with the azure config set subscription command. This is typically due to using outdated Azure CLI 1.0 syntax, whereas Azure CLI 2.0 is currently recommended.
Core Solution: The az account set Command
Azure CLI 2.0 introduces the az account set command specifically for subscription switching. Its basic syntax is: az account set --subscription <name or id>. Here, <name or id> can be the display name or unique identifier (ID) of the subscription. For example, if the subscription name is "Production", the command is az account set --subscription Production; if using the ID, it is az account set --subscription xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
Obtaining and Selecting Subscription Identifiers
Before executing the set command, users need to accurately obtain the subscription ID or name. This can be done by listing all available subscriptions with the az account list command, which outputs details in JSON format including fields like id, name, and isDefault. It is advisable to use the ID for setting, as names may contain special characters or spaces that could cause parsing errors. If the name includes spaces, enclose it in quotes, e.g., az account set --subscription "My Production Sub".
Configuration Persistence and Verification
After successfully running az account set, the change is persisted in the local configuration file (typically located in the .azure folder within the user's home directory). Users can verify the current default subscription by using the az account show command. If permission issues arise, ensure that authentication is complete via az login and that the account has sufficient access to the target subscription.
Common Errors and Troubleshooting
Common errors include using legacy CLI commands, incorrect subscription identifier input, or network connectivity problems. If the command executes without response, check that the Azure CLI version is 2.0 or higher (confirm with az --version). For complex environments, automating subscription switches in scripts is recommended to enhance operational efficiency.