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

快速步骤:创建和使用适用于 Azure 中 Linux VM 的 SSH 公钥-私钥对Quick steps: Create and use an SSH public-private key pair for Linux VMs in Azure

使用安全外壳 (SSH) 密钥对,可以在 Azure 上创建使用 SSH 密钥进行身份验证的虚拟机 (VM),从而无需密码即可登录。With a secure shell (SSH) key pair, you can create virtual machines (VMs) in Azure that use SSH keys for authentication, eliminating the need for passwords to log in. 本文介绍如何快速生成和使用适用于 Linux VM 的 SSH 公钥-私钥文件对。This article shows you how to quickly generate and use an SSH public-private key file pair for Linux VMs. 可使用 Azure Cloud Shell、macOS 或 Linux 主机或者适用于 Linux 的 Windows 子系统以及其他支持 OpenSSH 的工具完成这些步骤。You can complete these steps with the Azure Cloud Shell, a macOS or Linux host, the Windows Subsystem for Linux, and other tools that support OpenSSH.

备注

使用 SSH 密钥创建的 VM 默认配置为已禁用密码,这将大大增加暴力猜测攻击的难度。VMs created using SSH keys are by default configured with passwords disabled, which greatly increases the difficulty of brute-force guessing attacks.

有关详细背景和示例,请参阅创建 SSH 密钥对的详细步骤For more background and examples, see Detailed steps to create SSH key pairs.

有关在 Windows 计算机上生成和使用 SSH 密钥的其他方式,请参阅如何在 Azure 上将 SSH 密钥与 Windows 配合使用For additional ways to generate and use SSH keys on a Windows computer, see How to use SSH keys with Windows on Azure.

受支持的 SSH 密钥格式Supported SSH key formats

Azure 目前支持最小长度为 2048 位的 SSH 协议 2 (SSH-2) RSA 公钥-私钥对。Azure currently supports SSH protocol 2 (SSH-2) RSA public-private key pairs with a minimum length of 2048 bits. 不支持其他密钥格式(如 ED25519 和 ECDSA)。Other key formats such as ED25519 and ECDSA are not supported.

创建 SSH 密钥对Create an SSH key pair

使用 ssh-keygen 命令生成 SSH 公钥和私钥文件。Use the ssh-keygen command to generate SSH public and private key files. 默认情况下,这些文件在 ~/.ssh 目录中创建。By default, these files are created in the ~/.ssh directory. 可以指定其他位置,以及访问私钥文件的密码 (passphrase)。You can specify a different location, and an optional password (passphrase) to access the private key file. 如果给定位置存在具有相同名称的 SSH 密钥对,则这些文件将被覆盖。If an SSH key pair with the same name exists in the given location, those files are overwritten.

以下命令使用 RSA 加密和 2048 位长度创建 SSH 密钥对:The following command creates an SSH key pair using RSA encryption and a bit length of 2048:

ssh-keygen -t rsa -b 2048

如果通过 Azure CLI 使用 az vm create 命令创建 VM,可以使用 --generate-ssh-keys 选项生成 SSH 公钥和私钥文件。If you use the Azure CLI to create your VM with the az vm create command, you can optionally generate SSH public and private key files using the --generate-ssh-keys option. 密钥文件存储在 ~/.ssh 目录中,除非 --ssh-dest-key-path 选项另有规定。The key files are stored in the ~/.ssh directory unless specified otherwise with the --ssh-dest-key-path option. --generate-ssh-keys 选项不会覆盖现有密钥文件,而是会返回错误。The --generate-ssh-keys option will not overwrite existing key files, instead returning an error. 在以下命令中,将 VMname 和 RGname 替换为你自己的值:In the following command, replace VMname and RGname with your own values:

az vm create --name VMname --resource-group RGname --generate-ssh-keys 

在部署 VM 时提供 SSH 公钥Provide an SSH public key when deploying a VM

若要创建使用 SSH 密钥进行身份验证的 Linux VM,请在使用 Azure 门户、Azure CLI、Azure 资源管理器模板或其他方法创建 VM 时指定 SSH 公钥:To create a Linux VM that uses SSH keys for authentication, specify your SSH public key when creating the VM using the Azure portal, Azure CLI, Azure Resource Manager templates, or other methods:

如果不熟悉 SSH 公钥的格式,可以使用以下 cat 命令显示公钥,将 ~/.ssh/id_rsa.pub 替换为你自己的公钥文件的路径和文件名(如有必要):If you're not familiar with the format of an SSH public key, you can display your public key with the following cat command, replacing ~/.ssh/id_rsa.pub with the path and filename of your own public key file if needed:

cat ~/.ssh/id_rsa.pub

典型公钥值如下所示:A typical public key value looks like this:

ssh-rsa AAAAB3NzaC1yc2EAABADAQABAAACAQC1/KanayNr+Q7ogR5mKnGpKWRBQU7F3Jjhn7utdf7Z2iUFykaYx+MInSnT3XdnBRS8KhC0IP8ptbngIaNOWd6zM8hB6UrcRTlTpwk/SuGMw1Vb40xlEFphBkVEUgBolOoANIEXriAMvlDMZsgvnMFiQ12tD/u14cxy1WNEMAftey/vX3Fgp2vEq4zHXEliY/sFZLJUJzcRUI0MOfHXAuCjg/qyqqbIuTDFyfg8k0JTtyGFEMQhbXKcuP2yGx1uw0ice62LRzr8w0mszftXyMik1PnshRXbmE2xgINYg5xo/ra3mq2imwtOKJpfdtFoMiKhJmSNHBSkK7vFTeYgg0v2cQ2+vL38lcIFX4Oh+QCzvNF/AXoDVlQtVtSqfQxRVG79Zqio5p12gHFktlfV7reCBvVIhyxc2LlYUkrq4DHzkxNY5c9OGSHXSle9YsO3F1J5ip18f6gPq4xFmo6dVoJodZm9N0YMKCkZ4k1qJDESsJBk2ujDPmQQeMjJX3FnDXYYB182ZCGQzXfzlPDC29cWVgDZEXNHuYrOLmJTmYtLZ4WkdUhLLlt5XsdoKWqlWpbegyYtGZgeZNRtOOdN6ybOPJqmYFd2qRtb4sYPniGJDOGhx4VodXAjT09omhQJpE6wlZbRWDvKC55R2d/CSPHJscEiuudb+1SG2uA/oik/WQ== username@domainname

如果复制和粘贴要在 Azure 门户或 Resource Manager 模板中使用的公钥文件的内容,请确保不复制尾随空格。If you copy and paste the contents of the public key file to use in the Azure portal or a Resource Manager template, make sure you don't copy any trailing whitespace. 要在 macOS 中复制公钥,可以通过管道将公钥文件传递到 pbcopy。To copy a public key in macOS, you can pipe the public key file to pbcopy. 与此类似,要在 Linux 中复制公钥,可以将公钥文件通过管道传递到 xclip 等程序。Similarly in Linux, you can pipe the public key file to programs such as xclip.

放置在 Azure 中 Linux VM 上的公钥默认存储在 ~/.ssh/id_rsa.pub,除非在创建密钥对时指定了其他位置。The public key that you place on your Linux VM in Azure is by default stored in ~/.ssh/id_rsa.pub, unless you specified a different location when you created the key pair. 要借助现有公钥使用 Azure CLI 2.0 创建 VM,请通过使用具有 --ssh-key-value 选项的 az vm create 命令来指定此公钥的值或位置。To use the Azure CLI 2.0 to create your VM with an existing public key, specify the value and optionally the location of this public key using the az vm create command with the --ssh-key-value option. 在以下命令中,将 VMname、RGname 和 keyFile 替换为你自己的值:In the following command, replace VMname, RGname, and keyFile with your own values:

az vm create --name VMname --resource-group RGname --ssh-key-value @keyFile

通过 SSH 连接到 VMSSH into your VM

凭借部署在 Azure VM 上的公钥和本地系统上的私钥,使用 VM 的 IP 地址或 DNS 名称通过 SSH 连接到 VM。With the public key deployed on your Azure VM, and the private key on your local system, SSH into your VM using the IP address or DNS name of your VM. 在以下命令中,将 azureuser 和 myvm.westus.cloudapp.azure.com 替换为管理员用户名和完全限定的域名(或 IP 地址):In the following command, replace azureuser and myvm.westus.cloudapp.azure.com with the administrator user name and the fully qualified domain name (or IP address):

ssh azureuser@myvm.westus.cloudapp.azure.com

如果创建密钥对时指定了通行短语,则在登录过程中遇到提示时,请输入该通行短语。If you specified a passphrase when you created your key pair, enter that passphrase when prompted during the login process. VM 已添加到 ~/.ssh/known_hosts 文件,系统不会要求再次连接,除非 Azure VM 上的公钥发生更改或服务器名称从 ~/.ssh/known_hosts 删除。The VM is added to your ~/.ssh/known_hosts file, and you won't be asked to connect again until either the public key on your Azure VM changes or the server name is removed from ~/.ssh/known_hosts.

后续步骤Next steps