你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure CLI 可帮助你管理 Azure 订阅、创建管理组和锁定订阅。Azure 中可能有多个订阅。 你可以是多个组织的一部分,或者你的组织可能会跨分组划分对某些资源的访问。 Azure CLI 支持在全局和每个命令内分别选择订阅。
有关订阅、计费和成本管理的详细信息,请参阅 计费和成本管理文档。
术语
租户是Microsoft Entra ID 的实例,其中包含有关单个组织的信息。 多租户组织是具有多个Microsoft Entra ID 实例的组织。 租户有一个或多个 订阅 和 用户。
用户是登录到 Azure 以创建、管理和使用资源的帐户。 用户可能有权访问多个 租户 和 订阅。
订阅 是与 Microsoft 签订的使用云服务(包括 Azure)的协议。 每个资源都与订阅相关联。 订阅包含资源组。
Azure 资源组 是一个容器,用于保存 Azure 解决方案的相关资源。 若要了解如何管理订阅中的资源组,请参阅 如何使用 Azure CLI 管理 Azure 资源组
获取活跃租户
使用 az account tenant list 或 az account show 获取活动租户 ID。
az account tenant list
az account show
更改当前租户
若要切换租户,有两个选项。
在目标租户中以用户身份登录。 使用 az login 更改活动租户,并更新所属的订阅列表。
# sign in as a different user az login --user <myAlias@myCompany.com> --password <myPassword> # sign in with a different tenant az login --tenant <myTenantID>
如果你的组织需要多重身份验证,则使用
az login --user
以下方法时可能会收到此错误:Due to a configuration change made by your administrator, or because you moved to a new ___location, you must use multi-factor authentication to access...
使用备用
az login --tenant
命令提示打开 HTTPS 页面并输入提供的代码。 然后,可以使用多重身份验证并成功登录。 若要了解有关使用 Azure CLI 登录选项的详细信息,请参阅 使用 Azure CLI 登录。
获取订阅信息
大多数 Azure CLI 命令在订阅中起作用。 可以在命令中通过参数 --subscription
指定要操作的订阅。 如果未指定订阅,该命令将使用当前活动订阅。
若要查看当前正在使用的订阅或获取可用订阅的列表,请运行 az account show 或 az account list 命令。 转到 “了解如何将 Bash 与 Azure CLI 配合使用 ”,查看使用这些命令的更多方法示例。
下面是演示如何获取订阅信息的示例:
# get the current default subscription using show
az account show --output table
# get the current default subscription using list
az account list --query "[?isDefault]"
# get a subscription that contains search words or phrases
az account list --query "[?contains(name,'search phrase')].{SubscriptionName:name, SubscriptionID:id, TenantID:tenantId}" --output table
还可以将订阅信息存储在变量中,以便在脚本中使用。
# store the default subscription in a variable
subscriptionId="$(az account list --query "[?isDefault].id" --output tsv)"
echo $subscriptionId
# store a subscription of certain name in a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
echo $subscriptionId
小窍门
--output
参数是全局参数,适用于所有命令。 "表格值以友好的格式呈现输出。" 有关详细信息,请参阅 Azure CLI 命令的输出格式。
更改当前订阅
Azure 订阅既有名称又有 ID。 可以使用 az account set 并指定所需的订阅 ID 或名称,切换到其他订阅。
# change the active subscription using the subscription name
az account set --subscription "My Demos"
# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
还可以使用变量更改订阅。 下面是一个示例:
# change the active subscription using a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
az account set --subscription $subscriptionId
如果更改为另一租户中的订阅,则还会更改活动租户。 若要了解如何向 Microsoft Entra 租户添加新订阅,请参阅 关联或向 Microsoft Entra 租户添加 Azure 订阅。
如果收到“... 的订阅”不存在...”错误,请参阅 故障排除 ,了解可能的解决方案。
清除订阅缓存
若要更新订阅列表,请使用 az account clear 命令。 需要再次登录才能看到更新的列表。
az account clear
az login
清除订阅缓存在技术上与注销 Azure 的过程不同。 但是,清除订阅缓存时,在再次登录之前,无法运行 Azure CLI 命令,包括 az account set
。
创建 Azure 管理组
Azure 管理组中包括订阅。 管理组提供了管理这些订阅的访问、策略和符合性的方法。 有关详细信息,请参阅 什么是 Azure 管理组。
使用 az account management-group 命令创建和管理 Azure 管理组。
可以使用 az account management-group create 命令为多个订阅创建管理组:
az account management-group create --name Contoso01
若要查看所有管理组,请使用 az account management-group list 命令:
az account management-group list
使用 az account management-group subscription add 命令将订阅添加到新组:
az account management-group subscription add --name Contoso01 --subscription "My Demos"
az account management-group subscription add --name Contoso01 --subscription "My Second Demos"
若要删除订阅,请使用 az account management-group subscription remove 命令:
az account management-group subscription remove --name Contoso01 --subscription "My Demos"
若要删除管理组,请运行 az account management-group delete 命令:
az account management-group delete --name Contoso01
删除订阅或删除管理组不会删除或停用订阅。
设置 Azure 订阅锁
作为管理员,你可能需要锁定订阅,以防止用户删除或修改订阅。 有关详细信息,请参阅锁定资源以防止意外更改。
在 Azure CLI 中,使用 az account lock 命令。 例如, az account lock create 命令可以阻止用户删除订阅:
az account lock create --name "Cannot delete subscription" --lock-type CanNotDelete
注释
你需要在订阅上具有 contributor
权限才能创建或更改锁。
若要查看订阅上的当前锁,请使用 az account lock list 命令:
az account lock list --output table
如果将帐户设为只读,则结果类似于向所有用户分配“读取者”角色的权限。 若要了解如何为单个用户和角色设置权限,请参阅 使用 Azure CLI 添加或删除 Azure 角色分配。
若要查看锁的详细信息,请使用 az account lock show 命令:
az account lock show --name "Cannot delete subscription"
可以使用 az account lock delete 命令删除锁:
az account lock delete --name "Cannot delete subscription"
故障排除
订阅不存在
除了版式错误之外,当存在权限计时问题时,还可以收到此错误。 例如,如果您在 当前终端窗口打开时已向新订阅授予权限,则可能会出现此错误。 解决方案是关闭并重新打开终端窗口,或者使用az logout
、az login
命令刷新可用订阅列表。
下面是一个脚本,可帮助你查找和更改订阅。
# See what subscription you are currently using.
az account show
# Get a list of available subscriptions.
az account list --output table
# If the subscription you are seeking is not in the list
# close and reopen your terminal window,
# or logout and then sign in again.
az logout
az login
# You can also clear your cache to refresh the
# available subscription list
az account clear
az login
# Did your available subscription list change?
az account list --output table
# If the subscription you are seeking is still not in the list,
# contact your system administrator. You cannot change your
# subscription to an ID that is not in the list.
# If the subscription you are seeking is now in the list,
# change your subscription.
az account set --subscription 00000000-0000-0000-0000-00000000000