azure运行linux,快速入门:使用 Azure CLI 创建 Linux VM - Azure Linux Virtual Machines | Azure Docs...

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

04/15/2021

本文内容

本快速入门向你展示了如何使用 Azure 命令行接口 (CLI) 在 Azure 中部署 Linux 虚拟机 (VM)。This quickstart shows you how to use the Azure command-line interface (CLI) to deploy a Linux virtual machine (VM) in Azure. Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts.

在本教程中,我们将安装最新的 Ubuntu LTS 映像。In this tutorial, we will be installing the latest Ubuntu LTS image. 为了显示运转中的 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 Trial before you begin.

启动 Azure 本地 ShellLaunch Azure Local Shell

如果希望在本地安装并使用 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 CLI。If you need to install or upgrade, see Install Azure CLI.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud。If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

创建资源组Create a resource group

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. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

创建虚拟机Create virtual machine

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 is 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//resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",

"location": "chinaeast",

"macAddress": "00-0D-3A-23-9A-49",

"powerState": "VM running",

"privateIpAddress": "10.0.0.4",

"publicIpAddress": "40.68.254.142",

"resourceGroup": "myResourceGroup"

}

记下 VM 输出中自己的 publicIpAddress。Note 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. 将示例中的 IP 地址替换为 VM 的公共 IP 地址(如上一输出所示):Replace the IP address in the example with the public IP address of your VM as noted in the previous output:

ssh azureuser@40.68.254.142

安装 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:

ea475b2c5e740b557980e43b162a137d.png

清理资源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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值