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

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以创建具有静态公共 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 PowerShellTo use your local computer, ensure you have the Azure PowerShell installed.

  1. 打开命令会话并使用 Connect-AzAccount -Environment AzureChinaCloud 登录到 Azure。Open a command session and sign into Azure with Connect-AzAccount -Environment AzureChinaCloud.

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

    New-AzResourceGroup -Name myResourceGroup -Location ChinaEast
    
  3. 使用 New-AzVM 命令创建虚拟机。Create a virtual machine with the New-AzVM command. -AllocationMethod "Static" 选项向虚拟机分配静态公共 IP 地址。The -AllocationMethod "Static" option assigns a static public IP address to the virtual machine. 以下示例使用名为 myPublicIpAddress 的静态、基本 SKU 公共 IP 地址创建 Windows Server 虚拟机。The following example creates a Windows Server virtual machine with a static, basic SKU public IP address named myPublicIpAddress. 出现提示时,提供要用作虚拟机的登录凭据的用户名和密码:When prompted, provide a username and password to be used as the sign in credentials for the virtual machine:

    New-AzVm `
     -ResourceGroupName "myResourceGroup" `
     -Name "myVM" `
     -Location "China East" `
     -PublicIpAddressName "myPublicIpAddress" `
     -AllocationMethod "Static"
    

    如果公共 IP 地址必须是标准 SKU,则必须在单独的步骤中创建公共 IP 地址创建网络接口向网络接口分配公共 IP 地址,然后使用网络接口创建虚拟机If the public IP address must be a standard SKU, you have to create a public IP address, create a network interface, assign the public IP address to the network interface, and then create a virtual machine with the network interface, in separate steps. 详细了解公共 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. 使用 Get-AzPublicIpAddress 查看分配的公共 IP 地址并确认它创建为静态地址:View the public IP address assigned and confirm that it was created as a static address, with Get-AzPublicIpAddress:

    Get-AzPublicIpAddress `
     -ResourceGroupName "myResourceGroup" `
     -Name "myPublicIpAddress" `
     | Select "IpAddress", "PublicIpAllocationMethod" `
     | Format-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

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

Remove-AzResourceGroup -Name myResourceGroup -Force

后续步骤Next steps