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

快速入门:使用 Azure CLI 创建 Linux 虚拟机Quickstart: Create a Linux virtual machine with the Azure CLI

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 本快速入门介绍了如何使用 Azure CLI 在 Azure 中部署 Linux 虚拟机 (VM)。This quickstart shows you how to use the Azure CLI to deploy a Linux virtual machine (VM) in Azure. 在本教程中,我们将安装 Ubuntu 160.04 LTS。In this tutorial, we will be installing Ubuntu 160.04 LTS. 为了显示运转中的 VM,我们将使用 SSH 连接到它并安装 NGINX Web 服务器。To show the VM in action, you'll connect to it using SSH and install the NGINX web server.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account.

若要打开 Cloud Shell,只需要从代码块的右上角选择“试一试”。To open the Cloud Shell, just select Try it from the upper right corner of a code block. 也可以通过转到 https://shell.azure.com/bash 在单独的浏览器选项卡中启动 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/bash. 选择“复制”以复制代码块,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.

如果希望在本地安装并使用 CLI,则本快速入门需要 Azure CLI version 2.0.30 或更高版本。If you prefer to install and use the CLI locally, this quickstart requires Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

使用 az group create 命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在 eastus 位置创建名为 myResourceGroup 的资源组:The following example creates a resource group named myResourceGroup in the eastus location:

az group create --name myResourceGroup --location eastus

创建虚拟机Create virtual machine

使用 az vm create 命令创建 VM。Create a VM with the az vm create command.

以下示例创建一个名为 myVM 的 VM 并添加一个名为 azureuser 的用户帐户。The following example creates a VM named myVM and adds a user account named azureuser. --generate-ssh-keys 参数用来自动生成一个 SSH 密钥,并将其放置在默认密钥位置 (~/.ssh) 中。The --generate-ssh-keys parameter us used to automatically generate an SSH key, and put it in the default key location (~/.ssh). 若要改为使用一组特定的密钥,请使用 --ssh-key-value 选项。To use a specific set of keys instead, use the --ssh-key-value option.

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

创建 VM 和支持资源需要几分钟时间。It takes a few minutes to create the VM and supporting resources. 以下示例输出表明 VM 创建操作已成功。The following example output shows the VM create operation was successful.

{
  "fqdns": "",
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "eastus",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "40.68.254.142",
  "resourceGroup": "myResourceGroup"
}

记下 VM 输出中自己的 publicIpAddressNote your own publicIpAddress in the output from your VM. 在后续步骤中,将使用此地址访问 VM。This address is used to access the VM in the next steps.

为 Web 流量打开端口 80Open port 80 for web traffic

默认情况下,在 Azure 中创建 Linux VM 时仅打开 SSH 连接。By default, only SSH connections are opened when you create a Linux VM in Azure. 使用 az vm open-port 打开 TCP 端口 80 以供 NGINX Web 服务器使用:Use az vm open-port to open TCP port 80 for use with the NGINX web server:

az vm open-port --port 80 --resource-group myResourceGroup --name myVM

连接到虚拟机Connect to virtual machine

通过 SSH 照常连接到 VM。SSH to your VM as normal. 将 publicIpAddress 替换为 VM 的公共 IP 地址(在 VM 的上一输出中记下):Replace publicIpAddress with the public IP address of your VM as noted in the previous output from your VM:

ssh azureuser@publicIpAddress

安装 Web 服务器Install web server

若要查看运行中的 VM,请安装 NGINX Web 服务器。To see your VM in action, install the NGINX web server. 更新程序包来源,然后安装最新的 NGINX 程序包。Update your package sources and then install the latest NGINX package.

sudo apt-get -y update
sudo apt-get -y install nginx

完成后,键入 exit 以离开 SSH 会话。When done, type exit to leave the SSH session.

查看运行中的 Web 服务器View the web server in action

使用所选的 Web 浏览器查看默认的 NGINX 欢迎页。Use a web browser of your choice to view the default NGINX welcome page. 使用你的 VM 的公用 IP 地址作为 Web 地址。Use the public IP address of your VM as the web address. 以下示例演示了默认 NGINX 网站:The following example shows the default NGINX web site:

NGINX 默认站点

清理资源Clean up resources

如果不再需要资源组、VM 和所有相关的资源,可以使用 az group delete 命令将其删除。When no longer needed, you can use the az group delete command to remove the resource group, VM, and all related resources.

az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,你部署了简单的虚拟机,打开了 Web 流量的网络端口,并安装了一个基本 Web 服务器。In this quickstart, you deployed a simple virtual machine, open a network port for web traffic, and installed a basic web server. 若要详细了解 Azure 虚拟机,请继续学习 Linux VM 的教程。To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.