使用 Azure CLI 创建具有静态公共 IP 地址的虚拟机Create a virtual machine with a static public IP address using the Azure CLI

可以创建具有静态公共 IP 地址的虚拟机。You can create a virtual machine with a static public IP address. 使用公共 IP 地址可以通过 Internet 来与虚拟机通信。A public IP address enables you to communicate to a virtual machine from the internet. 分配静态公共 IP 地址而非动态地址可以确保地址永远不会改变。Assign a static public IP address, rather than a dynamic address, to ensure that the address never changes. 详细了解静态公共 IP 地址Learn more about static public IP addresses. 若要将分配给现有虚拟机的公共 IP 地址从动态更改为静态,或者要使用专用 IP 地址,请参阅添加、更改或删除 IP 地址To change a public IP address assigned to an existing virtual machine from dynamic to static, or to work with private IP addresses, see Add, change, or remove IP addresses. 公共 IP 地址会产生少许费用,可为每个订阅使用的公共 IP 地址数有限制Public IP addresses have a nominal charge, and there is a limit to the number of public IP addresses that you can use per subscription.

创建虚拟机Create a virtual machine

可以从本地计算机完成以下步骤。You can complete the following steps from your local computer. 若要使用本地计算机,请确保安装 Azure CLITo use your local computer, ensure you have the Azure CLI installed.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

  1. 打开命令会话并使用 az login 登录到 Azure。Open a command session and sign into Azure with az login.

  2. 使用 az group create 命令创建资源组。Create a resource group with the az group create command. 以下示例在“中国东部”Azure 区域中创建一个资源组:The following example creates a resource group in the China East Azure region:

    az group create --name myResourceGroup --location chinaeast
    
  3. 使用 az vm create 命令创建虚拟机。Create a virtual machine with the az vm create command. --public-ip-address-allocation=static 选项向虚拟机分配静态公共 IP 地址。The --public-ip-address-allocation=static option assigns a static public IP address to the virtual machine. 以下示例使用名为 myPublicIpAddress 的静态基本 SKU 公共 IP 地址创建 Ubuntu 虚拟机:The following example creates an Ubuntu virtual machine with a static, basic SKU public IP address named myPublicIpAddress:

    az vm create \
     --resource-group myResourceGroup \
     --name myVM \
     --image UbuntuLTS \
     --admin-username azureuser \
     --generate-ssh-keys \
     --public-ip-address myPublicIpAddress \
     --public-ip-address-allocation static
    

    如果公共 IP 地址必须是标准 SKU,请将 --public-ip-sku Standard 添加到上述命令。If the public IP address must be a standard SKU, add --public-ip-sku Standard to the previous command. 详细了解公共 IP 地址 SKULearn more about Public IP address SKUs. 如果虚拟机将添加到公共 Azure 负载均衡器的后端池,则虚拟机公共 IP 地址的 SKU 必须与负载均衡器的公共 IP 地址的 SKU 相匹配。If the virtual machine will be added to the back-end pool of a public Azure Load Balancer, the SKU of the virtual machine's public IP address must match the SKU of the load balancer's public IP address. 有关详细信息,请参阅 Azure 负载均衡器For details, see Azure Load Balancer.

  4. 使用 az network public-ip show 查看分配的公共 IP 地址,并确认它是否创建为静态基本 SKU 地址:View the public IP address assigned and confirm that it was created as a static, basic SKU address, with az network public-ip show:

    az network public-ip show \
     --resource-group myResourceGroup \
     --name myPublicIpAddress \
     --query [ipAddress,publicIpAllocationMethod,sku] \
     --output table
    

    Azure 从你在其中创建虚拟机的区域使用的地址中分配了一个公共 IP 地址。Azure assigned a public IP address from addresses used in the region you created the virtual machine in. 可以下载 Azure 中国云的范围(前缀)列表。You can download the list of ranges (prefixes) for the Azure China cloud.

警告

不要修改虚拟机操作系统中的 IP 地址设置。Do not modify the IP address settings within the virtual machine's operating system. 操作系统不知道 Azure 公共 IP 地址。The operating system is unaware of Azure public IP addresses. 虽然可以向操作系统添加专用 IP 地址设置,但除非必要,否则我们建议不要这样做,而只能阅读向操作系统添加专用 IP 地址之后才执行此操作。Though you can add private IP address settings to the operating system, we recommend not doing so unless necessary, and not until after reading Add a private IP address to an operating system.

清理资源Clean up resources

如果不再需要资源组及其包含的所有资源,可以使用 az group delete 将其删除:When no longer needed, you can use az group delete to remove the resource group and all of the resources it contains:

az group delete --name myResourceGroup --yes

后续步骤Next steps