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

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

Azure CLI 2.0 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI 2.0 is used to create and manage Azure resources from the command line or in scripts. 本快速入门演示如何使用 Azure CLI 2.0 在 Azure 中部署运行 Ubuntu 的 Linux 虚拟机 (VM)。This quickstart shows you how to use the Azure CLI 2.0 to deploy a Linux virtual machine (VM) in Azure that runs Ubuntu. 若要查看运行中的 VM,可以通过 SSH 登录到该 VM 并安装 NGINX Web 服务器。To see your VM in action, you then SSH to the VM and install the NGINX web server.

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

打开 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Cloud Shell 中预安装并配置了常用 Azure 工具供你与帐户一起使用。Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. 只需选择“复制”按钮即可复制代码,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. 可通过多种方式打开 Cloud Shell:There are a few ways to open Cloud Shell:

选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bash
选择 Azure 门户右上角菜单上的“Cloud Shell”按钮。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. 门户中的 Cloud Shell

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

创建资源组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 的用户帐户,并生成 SSH 密钥(如果这些密钥尚未存在于默认密钥位置 (~/.ssh) 中)。The following example creates a VM named myVM, adds a user account named azureuser, and generates SSH keys if they do not already exist in the default key location (~/.ssh). 若要使用一组特定密钥,请使用 --ssh-key-value 选项:To use a specific set of keys, 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 包,请从 SSH 会话运行以下命令:To update package sources and install the latest NGINX package, run the following commands from your SSH session:

# update packages
sudo apt-get -y update

# install NGINX
sudo apt-get -y install nginx

完成后,exit SSH 会话。When done, exit the SSH session.

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

NGINX 已安装,并且现在已从 Internet 打开 VM 上的端口 80 - 可以使用所选的 Web 浏览器查看默认的 NGINX 欢迎页。With NGINX installed and port 80 now open on your VM from the Internet, use a web browser of your choice to view the default NGINX welcome page. 使用上一步中获取的 VM 的公用 IP 地址。Use the public IP address of your VM obtained in a previous step. 以下示例演示了默认 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. 请确保已退出与 VM 的 SSH 会话,然后删除资源,如下所示:Make sure that you have exited the SSH session to your VM, then delete the resources as follows:

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.