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

使用 apt 安装 Azure CLIInstall Azure CLI with apt

如果运行附带 apt 的发行版(例如 Ubuntu 或 Debian),则可以安装适用于 Azure CLI 的 x86_64 包。If you are running a distribution that comes with apt, such as Ubuntu or Debian, there's an x86_64 package available for the Azure CLI. 此包已经过测试,支持:This package has been tested with and is supported for:

  • Ubuntu trusty、xenial、artful、bionic 和 discoUbuntu trusty, xenial, artful, bionic, and disco
  • Debian wheezy、jessie、stretch 和 busterDebian wheezy, jessie, stretch, and buster

Azure CLI 的当前版本为 2.0.75The current version of the Azure CLI is 2.0.75. 有关最新版本的信息,请参阅发行说明For information about the latest release, see the release notes. 若要查找你已安装的版本并查看是否需要更新,请运行 az --versionTo find your installed version and see if you need to update, run az --version.

备注

用于 Azure CLI 的包会安装其自己的 Python 解释器,而不使用系统 Python。The package for Azure CLI installs its own Python interpreter, and does not use the system Python.

安装Install

我们提供了两种方法来为支持 apt 的分发版安装 Azure CLI:可以自动运行 install 命令的一体式脚本;可由用户作为分步式过程运行的指令。We offer two ways to install the Azure CLI with distributions that support apt: As an all-in-one script that runs the install commands for you, and instructions that you can run as a step-by-step process on your own.

使用一条命令安装Install with one command

我们将提供并维护可以通过一个步骤运行所有安装命令的脚本。We offer and maintain a script which runs all of the installation commands in one step. 可以使用 curl 运行该脚本并通过管道将其直接传递给 bash,或者将该脚本下载到某个文件,并在检查后再运行它。Run it by using curl and pipe directly to bash, or download the script to a file and inspect it before running.

重要

此脚本只在 Ubuntu 16.04+ 和 Debian 8+ 中经过验证。This script is only verified for Ubuntu 16.04+ and Debian 8+. 它不一定可在其他分发版上运行。It may not work on other distributions. 如果使用衍生的分发版(例如 Linux Mint),请遵照手动安装说明,并执行任何必要的故障排除。If you're using a derived distribution such as Linux Mint, follow the manual install instructions and perform any necessary troubleshooting.

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

手动安装说明Manual install instructions

如果不想以超级用户的身份运行脚本,或者一体化脚本失败,请遵循以下步骤安装 Azure CLI。If you don't want to run a script as superuser or the all-in-one script fails, follow these steps to install the Azure CLI.

  1. 获取该安装过程所需的包:Get packages needed for the install process:

    sudo apt-get update
    sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
    
  2. 下载并安装 Microsoft 签名密钥:Download and install the Microsoft signing key:

    curl -sL https://packages.microsoft.com/keys/microsoft.asc | 
        gpg --dearmor | 
        sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null
    
  3. 添加 Azure CLI 软件存储库:Add the Azure CLI software repository:
    AZ_REPO=$(lsb_release -cs)
    echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | 
        sudo tee /etc/apt/sources.list.d/azure-cli.list
    
  4. 更新存储库信息并安装 azure-cli 包:Update repository information and install the azure-cli package:

    sudo apt-get update
    sudo apt-get install azure-cli
    

使用 az 命令运行 Azure CLI。Run the Azure CLI with the az command. 若要登录,请使用 az login 命令。To sign in, use the az login command.

  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 CLI 登录To learn more about different authentication methods, see Sign in with Azure CLI.

故障排除Troubleshooting

下面是使用 apt 安装时出现的一些常见问题。Here are some common problems seen when installing with apt. 如果遇到的问题未在本文中列出,请在 github 上提出问题If you experience a problem not covered here, file an issue on github.

lsb_release 没有返回正确的基础发行版本lsb_release does not return the correct base distribution version

某些 Ubuntu 或 Debian 派生的版本(例如 Linux Mint)不会通过 lsb_release 返回正确的版本名称。Some Ubuntu- or Debian-derived distributions such as Linux Mint may not return the correct version name from lsb_release. 此值在安装过程中用于确定要安装的包。This value is used in the install process to determine the package to install. 如果知道自己的发行版派生自的 Ubuntu 或 Debian 版本的代码名称,则可在添加存储库时手动设置 AZ_REPO 值。If you know the code name of the Ubuntu or Debian version your distribution is derived from, you can set the AZ_REPO value manually when adding the repository. 否则,请查看发行版的信息,了解如何确定基础发行版代码名称,然后将 AZ_REPO 设置为正确值。Otherwise, look up information for your distribution on how to determine the base distribution code name and set AZ_REPO to the correct value.

没有适用于你的发行版的程序包No package for your distribution

有时候,在某个发行版发布后,可能要过一段时间才会有 Azure CLI 包可用于它。Sometimes it may be a while after a distribution is released before there's an Azure CLI package available for it. 对于依赖项的将来版本,Azure CLI 设计为弹性的并且尽可能少地依赖这些依赖项。The Azure CLI designed to be resilient with regards to future versions of dependencies and rely on as few of them as possible. 如果没有适用于你的基础发行版的程序包,请尝试使用较早发行版的程序包。If there's no package available for your base distribution, try a package for an earlier distribution.

为此,请在添加存储库时手动设置 AZ_REPO 的值。To do this, set the value of AZ_REPO manually when adding the repository. 对于 Ubuntu 发行版,请使用 bionic 存储库,对于 Debian 发行版,请使用 stretchFor Ubuntu distributions use the bionic repository, and for Debian distributions use stretch. 在 Ubuntu Trusty 和 Debian Wheezy 之前发布的发行版不受支持。Distributions released before Ubuntu Trusty and Debian Wheezy are not supported.

代理块连接Proxy blocks connection

如果由于代理而无法连接到外部资源,请确保已在 shell 中正确设置了 HTTP_PROXYHTTPS_PROXY 变量。If you're unable to connect to an external resource due to a proxy, make sure that you've correctly set the HTTP_PROXY and HTTPS_PROXY variables in your shell. 你需要与系统管理员联系以了解要对这些代理使用哪些主机和端口。You will need to contact your system administrator to know what host(s) and port(s) to use for these proxies.

许多 Linux 程序(包括那些在安装过程中使用的程序)也会采用这些值。These values are respected by many Linux programs, including those which are used in the install process. 若要设置这些值,请执行以下操作:To set these values:

# No auth
export HTTP_PROXY=http://[proxy]:[port]
export HTTPS_PROXY=https://[proxy]:[port]

# Basic auth
export HTTP_PROXY=http://[username]:[password]@[proxy]:[port]
export HTTPS_PROXY=https://[username]:[password]@[proxy]:[port]

重要

如果你位于代理后面,则必须设置这些 shell 变量以通过 CLI 连接到 Azure 服务。If you are behind a proxy, these shell variables must be set to connect to Azure services with the CLI. 如果不使用基本身份验证,建议将这些变量导出到 .bashrc 文件中。If you are not using basic auth, it's recommended to export these variables in your .bashrc file. 请始终遵循企业的安全策略和系统管理员的要求。Always follow your business' security policies and the requirements of your system administrator.

你可能还想要显式配置 apt 以便在所有情况下都使用此代理。You may also want to explicitly configure apt to use this proxy at all times. 请确保以下行显示在 /etc/apt/apt.conf.d/ 下的 apt 配置文件中。Make sure that the following lines appear in an apt configuration file in /etc/apt/apt.conf.d/. 我们建议使用现有的全局配置文件(现有的代理配置文件 40proxies)或 99local,但要遵守系统管理要求。We recommend using either your existing global configuration file, an existing proxy configuration file, 40proxies, or 99local, but follow your system administration requirements.

Acquire {
    http::proxy "http://[username]:[password]@[proxy]:[port]";
    https::proxy "https://[username]:[password]@[proxy]:[port]";
}

如果代理不使用基本身份验证,请__删除__代理 URI 的 [username]:[password]@ 部分。If your proxy does not use basic auth, remove the [username]:[password]@ portion of the proxy URI. 如果需要代理配置的详细信息,请参阅官方的 Ubuntu 文档:If you require more information for proxy configuration, see the official Ubuntu documentation:

为了获取 Microsoft 签名密钥并从我们的存储库中获取包,代理必须允许与以下地址之间的 HTTPS 连接:In order to get the Microsoft signing key and get the package from our repository, your proxy needs to allow HTTPS connections to the following address:

  • https://packages.microsoft.com

CLI 未能在适用于 Linux 的 Windows 子系统上安装或运行CLI fails to install or run on Windows Subsystem for Linux

由于适用于 Linux 的 Windows 子系统 (WSL) 是基于 Windows 平台的一个系统调用转换层,因此,在尝试安装或运行 Azure CLI 时可能会发生错误。Since Windows Subsystem for Linux (WSL) is a system call translation layer on top of the Windows platform, you might experience an error when trying to install or run the Azure CLI. CLI 依赖于在 WSL 中可能具有 bug 的某些功能。The CLI relies on some features that may have a bug in WSL. 如果无论你以何方式安装 CLI 都会发生错误,则很可能是因为 WSL 有问题而不是 CLI 安装过程有问题。If you experience an error no matter how you install the CLI, there's a good chance it's an issue with WSL and not with the CLI install process.

若要对 WSL 安装进行故障排除并尽可能解决问题,请执行以下操作:To troubleshoot your WSL installation and possibly resolve issues:

  • 如果可以,在 Linux 计算机或 VM 上运行相同的安装过程来看看它是否会成功。If you can, run an identical install process on a Linux machine or VM to see if it succeeds. 如果成功,则几乎可以肯定问题与 WSL 有关。If it does, your issue is almost certainly related to WSL. 若要启动 Azure 中的 Linux VM,请参阅在 Azure 门户中创建 Linux VM 文档。To start a Linux VM in Azure, see the create a Linux VM in the Azure Portal documentation.
  • 确保运行的是最新版本的 WSL。Make sure that you're running the latest version of WSL. 若要获取最新版本,请更新 Windows 10 安装To get the latest version, update your Windows 10 installation.
  • 检查 WSL 是否存在可能会解决你的问题的待解决问题Check for any open issues with WSL which might address your problem. 那里通常会提供有关如何解决该问题的建议,或者提供有关将修复该问题的发行版的信息。Often there will be suggestions on how to work around the problem, or information about a release where the issue will be fixed.
  • 如果没有与你的问题对应的现有问题,请提交一个新的 WSL 问题并确保提供尽可能多的信息。If there are no existing issues for your problem, file a new issue with WSL and make sure that you include as much information as possible.

如果在 WSL 上安装或运行时继续出现问题,请考虑安装适用于 Windows 的 CLIIf you continue to have issues installing or running on WSL, consider installing the CLI for Windows.

更新Update

使用 apt-get upgrade 更新 CLI 包。Use apt-get upgrade to update the CLI package.

sudo apt-get update && sudo apt-get upgrade

备注

此命令将会升级系统上所有未发生依赖关系更改的已安装包。This command upgrades all of the installed packages on your system that have not had a dependency change. 若只要升级 CLI,请使用 apt-get installTo upgrade the CLI only, use apt-get install.

sudo apt-get update && sudo apt-get install --only-upgrade -y azure-cli

卸载Uninstall

如果你决定卸载 Azure CLI,我们会很遗憾。If you decide to uninstall the Azure CLI, we're sorry to see you go. 在卸载之前,请使用 az feedback 命令告诉我们在哪些方面需要改进或修复。Before you uninstall, use the az feedback command to let us know what could be improved or fixed. 我们的目标是消除 Azure CLI 中的 bug 并使其变得用户友好。Our goal is to make the Azure CLI bug-free and user-friendly. 如果你在发现 bug 后提出 GitHub 问题,我们将十分感激。If you found a bug, we'd appreciate it if you file a GitHub issue.

  1. 使用 apt-get remove 进行卸载:Uninstall with apt-get remove:

    sudo apt-get remove -y azure-cli
    
  2. 如果不打算重新安装 CLI,请删除 Azure CLI 存储库信息:If you don't plan to reinstall the CLI, remove the Azure CLI repository information:

    sudo rm /etc/apt/sources.list.d/azure-cli.list
    
  3. 如果不使用 Microsoft 的其他包,请删除签名密钥:If you use no other packages from Microsoft, remove the signing key:

    sudo rm /etc/apt/trusted.gpg.d/microsoft.asc.gpg
    
  4. 删除任何不需要的包:Remove any unneeded packages:

    sudo apt autoremove
    

后续步骤Next Steps

现在你已经安装了 Azure CLI,下面简要介绍其功能和常用命令。Now that you've installed the Azure CLI, take a short tour of its features and common commands.