您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure CLI 入门Get started with Azure CLI

欢迎使用 Azure CLI!Welcome to the Azure CLI! CLI 是旨在让你快速、高效地使用 Azure 服务且主要侧重于自动化的工具。The CLI is a tool designed to get you working quickly and efficiently with Azure services, with an emphasis on automation. 本文介绍 CLI 功能,并提供可帮助你高效工作的外部资源的链接。This article introduces features of the CLI and links out to resources that help you be productive.


在脚本和 Microsoft 文档网站中,已针对 bash shell 编写了 Azure CLI 示例。In scripts and on the Microsoft documentation site, Azure CLI examples are written for the bash shell. 一行的示例将在任何平台上运行。One-line examples will run on any platform. 包括续行符 (\) 或变量赋值的更长示例需要修改才能在其他 shell(包括 PowerShell)上工作。Longer examples which include line continuations (\) or variable assignment need to be modified to work on other shells, including PowerShell.

在 Azure Cloud Shell 中安装或运行Install or run in Azure Cloud Shell

开始使用 Azure CLI 的最简单方法是通过浏览器在 Azure Cloud Shell 环境中运行它。The easiest way to get started with the Azure CLI is by running it in an Azure Cloud Shell environment through your browser. 若要了解 Cloud Shell,请参阅 bash in Azure Cloud Shell 快速入门To learn about Cloud Shell, see Quickstart for Bash in Azure Cloud Shell.

准备好安装 CLI 时,请参阅安装说明When you're ready to install the CLI, see the installation instructions.

在第一次安装 CLI 后,请通过运行 az --version 检查它是否已安装,以及所安装的版本是否正确。After installing the CLI for the first time, check that it's installed and you've got the correct version by running az --version.

登录Sign in

对本地安装使用任何 CLI 命令之前,需要使用 az login 登录。Before using any CLI commands with a local install, you need to sign in with az login.

  1. 运行 login 命令。Run the login command.

    az login

    如果 CLI 可以打开默认浏览器,它将这样做并加载 Azure 登录页。If the CLI can open your default browser, it will do so and load an Azure sign-in page.

    否则,请在 https://aka.ms/devicelogin 处打开浏览器页,然后输入终端中显示的授权代码。Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal.

  2. 在浏览器中使用帐户凭据登录。Sign in with your account credentials in the browser.

登录后,你将看到与你的 Azure 帐户关联的订阅列表。After logging in, you see a list of subscriptions associated with your Azure account. 在使用 isDefault: true 的情况下显示的订阅信息是登录后当前已激活的订阅。The subscription information with isDefault: true is the currently activated subscription after logging in. 若要选择另一个订阅,请将 az account set 命令与要切换到的订阅 ID 配合使用。To select another subscription, use the az account set command with the subscription ID to switch to. 有关订阅选择的详细信息,请参阅使用多个 Azure 订阅For more information about subscription selection, see Use multiple Azure subscriptions.

有多种方法可用来以非交互方式登录,使用 Azure CLI 登录中详细介绍了这些方法。There are ways to sign in non-interactively, which are covered in detail in Sign in with Azure CLI.

常用命令Common commands

此表列出了 CLI 中的一些常用命令及其参考文档的链接。This table lists some common commands used in the CLI and links to their reference documentation.

资源类型Resource type Azure CLI 命令组Azure CLI command group
资源组Resource group az groupaz group
虚拟机Virtual machines az vmaz vm
存储帐户Storage accounts az storage accountaz storage account
Key VaultKey Vault az keyvaultaz keyvault
Web 应用程序Web applications az webappaz webapp
SQL 数据库SQL databases az sql serveraz sql server
CosmosDBCosmosDB az cosmosdbaz cosmosdb

查找命令Finding commands

CLI 中的命令以组命令的形式进行组织。 Commands in the CLI are organized as commands of groups. 每个组表示一个 Azure 服务,命令针对该服务运行。Each group represents an Azure service, and commands operate on that service.

若要搜索命令,请使用 az findTo search for commands, use az find. 例如,若要搜索包含 secret 的命令名称,请使用以下命令:For example, to search for command names containing secret, use the following command:

az find secret

使用 --help 参数获取组的命令和子组的完整列表。Use the --help argument to get a complete list of commands and subgroups of a group. 例如,若要查找用于处理网络安全组 (NSG) 的 CLI 命令,请运行:For example, to find the CLI commands for working with Network Security Groups (NSGs):

az network nsg --help

CLI 为 bash shell 下的命令提供完整 tab 键补全。The CLI has full tab completion for commands under the bash shell.

全局可用参数Globally available arguments

有一些参数可用于每条命令。There are some arguments that are available for every command.

  • --help 会输出有关命令及其参数的 CLI 参考信息并列出可用的子组和命令。--help prints CLI reference information about commands and their arguments and lists available subgroups and commands.
  • --output 可更改输出格式。--output changes the output format. 可用的输出格式包括 jsonjsonc(彩色 JSON)、tsv(制表符分隔值)、table(用户可读 ASCII 表)以及 yamlThe available output formats are json, jsonc (colorized JSON), tsv (Tab-Separated Values), table (human-readable ASCII tables), and yaml. 默认情况下,CLI 输出 jsonBy default the CLI outputs json. 若要详细了解可用输出格式,请参阅 Azure CLI 的输出格式To learn more about the available output formats, see Output formats for Azure CLI.
  • --query 使用 JMESPath 查询语言筛选从 Azure 服务返回的输出。--query uses the JMESPath query language to filter the output returned from Azure services. 若要详细了解查询,请参阅使用 Azure CLI 查询命令结果JMESPath 教程To learn more about queries, see Query command results with Azure CLI and the JMESPath tutorial.
  • --verbose 输出有关操作期间在 Azure 中创建的资源的信息和其他有用信息。--verbose prints information about resources created in Azure during an operation, and other useful information.
  • --debug 输出有关 CLI 操作的更详细信息,用于调试目的。--debug prints even more information about CLI operations, used for debugging purposes. 如果发现了 bug,在提交 bug 报告时,请提供启用 --debug 标志生成的输出。If you find a bug, provide output generated with the --debug flag on when submitting a bug report.

交互模式Interactive mode

CLI 提供一种交互模式,可自动显示帮助信息,并可更轻松地选择子命令。The CLI offers an interactive mode that automatically displays help information and makes it easier to select subcommands. 使用 az interactive 命令即可进入交互模式。You enter interactive mode with the az interactive command.

az interactive

有关交互模式的详细信息,请参阅 Azure CLI 交互模式For more information on interactive mode, see Azure CLI Interactive Mode.

此外,还有提供交互体验的 Visual Studio Code 插件,包括自动完成和鼠标悬停显示的文档。There's also a Visual Studio Code plugin that offers an interactive experience, including autocomplete and mouse-over documentation.

使用快速入门和教程了解 CLI 基础知识Learn CLI basics with quickstarts and tutorials

若要开始使用 Azure CLI,请试用深入教程以设置虚拟机并利用 CLI 的功能查询 Azure 资源。To get you started with the Azure CLI, try an in-depth tutorial for setting up virtual machines and using the power of the CLI to query Azure resources.

其他热门服务也有快速入门教程。There are also quickstarts for other popular services.

提供反馈Give feedback

我们欢迎你提供有关 CLI 的反馈以帮助我们改进和解决 bug。We welcome your feedback for the CLI to help us make improvements and resolve bugs. 可以在 Github 上提出问题,或利用 CLI 的内置功能来通过 az feedback 命令留下常规反馈。You can file an issue on GitHub or use the built-in features of the CLI to leave general feedback with the az feedback command.

az feedback