使用 PowerShell 将多个 IP 地址分配到虚拟机Assign multiple IP addresses to virtual machines 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.

在一个 Azure 虚拟机 (VM) 上可以附加一个或多个网络接口 (NIC)。An Azure Virtual Machine (VM) has one or more network interfaces (NIC) attached to it. 可为任何 NIC 分配一个或多个静态或动态的公共与专用 IP 地址。Any NIC can have one or more static or dynamic public and private IP addresses assigned to it. 为 VM 分配多个 IP 地址可实现以下功能:Assigning multiple IP addresses to a VM enables the following capabilities:

  • 在单个服务器上使用不同的 IP 地址和 SSL 证书托管多个网站或服务。Hosting multiple websites or services with different IP addresses and SSL certificates on a single server.
  • 用作网络虚拟设备,例如防火墙或负载均衡器。Serve as a network virtual appliance, such as a firewall or load balancer.
  • 可将任何 NIC 的任何专用 IP 地址添加到 Azure 负载均衡器后端池。The ability to add any of the private IP addresses for any of the NICs to an Azure Load Balancer back-end pool. 以往,只能将主要 NIC 的主要 IP 地址添加到后端池。In the past, only the primary IP address for the primary NIC could be added to a back-end pool. 若要详细了解如何对多个 IP 配置进行负载均衡,请阅读对多个 IP 配置进行负载均衡一文。To learn more about how to load balance multiple IP configurations, read the Load balancing multiple IP configurations article.

附加到 VM 的每个 NIC 都具有一个或多个关联的 IP 配置。Every NIC attached to a VM has one or more IP configurations associated to it. 每个配置分配有一个静态或动态专用 IP 地址。Each configuration is assigned one static or dynamic private IP address. 每个配置还可以具有一个关联的公共 IP 地址资源。Each configuration may also have one public IP address resource associated to it. 公共 IP 地址资源分配有动态或静态公共 IP 地址。A public IP address resource has either a dynamic or static public IP address assigned to it. 若要详细了解 Azure 中的 IP 地址,请阅读 Azure 中的 IP 地址一文。To learn more about IP addresses in Azure, read the IP addresses in Azure article.

分配给 NIC 的专用 IP 地址数目存在限制。There is a limit to how many private IP addresses can be assigned to a NIC. 能够在 Azure 订阅中使用的公共 IP 地址数也存在限制。There is also a limit to how many public IP addresses that can be used in an Azure subscription. 有关详细信息,请参阅 Azure 限制一文。See the Azure limits article for details.

本文介绍如何使用 PowerShell 通过 Azure Resource Manager 部署模型创建虚拟机 (VM)。This article explains how to create a virtual machine (VM) through the Azure Resource Manager deployment model using PowerShell. 无法将多个 IP 地址分配到通过经典部署模型创建的资源。Multiple IP addresses cannot be assigned to resources created through the classic deployment model. 若要详细了解 Azure 部署模型,请阅读 Understand deployment models(了解部署模型)一文。To learn more about Azure deployment models, read the Understand deployment models article.

场景Scenario

创建具有单个 NIC 的 VM 并将其连接到虚拟网络。A VM with a single NIC is created and connected to a virtual network. VM 需要三个不同的专用 IP 地址和两个公共 IP 地址。The VM requires three different private IP addresses and two public IP addresses. IP 地址将分配到以下 IP 配置:The IP addresses are assigned to the following IP configurations:

  • IPConfig-1: 分配一个静态 专用 IP 地址和一个静态 公共 IP 地址。IPConfig-1: Assigns a static private IP address and a static public IP address.

  • IPConfig-2: 分配一个静态 专用 IP 地址和一个静态 公共 IP 地址。IPConfig-2: Assigns a static private IP address and a static public IP address.

  • IPConfig-3: 分配一个静态 专用 IP 地址,不分配公共 IP 地址。IPConfig-3: Assigns a static private IP address and no public IP address.

    多个 IP 地址

创建 NIC 时,IP 配置将关联到 NIC;创建 VM 时,NIC 将附加到 VM。The IP configurations are associated to the NIC when the NIC is created and the NIC is attached to the VM when the VM is created. 本方案使用的 IP 地址类型用于演示。The types of IP addresses used for the scenario are for illustration. 可以根据需要分配任何 IP 地址和指定分配类型。You can assign whatever IP address and assignment types you require.

备注

尽管本文是将所有 IP 配置分配到单个 NIC,但也可以将多个 IP 配置分配到具有多个 NIC 的 VM 中的任意 NIC。Though the steps in this article assigns all IP configurations to a single NIC, you can also assign multiple IP configurations to any NIC in a multi-NIC VM. 若要了解如何创建具有多个 NIC 的 VM,请阅读创建具有多个 NIC 的 VM一文。To learn how to create a VM with multiple NICs, read the Create a VM with multiple NICs article.

创建具有多个 IP 地址的 VMCreate a VM with multiple IP addresses

下面的步骤说明如何根据方案中所述,创建具有多个 IP 地址的示例 VM。The steps that follow explain how to create an example VM with multiple IP addresses, as described in the scenario. 根据实现的需要,更改变量值。Change variable values as required for your implementation.

  1. 打开 PowerShell 命令提示符,在单个 PowerShell 会话中完成本部分余下的步骤。Open a PowerShell command prompt and complete the remaining steps in this section within a single PowerShell session. 如果尚未安装并配置 PowerShell,请先完成如何安装和配置 Azure PowerShell 一文中所述的步骤。If you don't already have PowerShell installed and configured, complete the steps in the How to install and configure Azure PowerShell article.

  2. 使用 Connect-AzAccount -Environment AzureChinaCloud 命令登录帐户。Login to your account with the Connect-AzAccount -Environment AzureChinaCloud command.

  3. 将 myResourceGroup** 和 chinanorth** 替换为所选名称和位置。Replace myResourceGroup and chinanorth with a name and location of your choosing. 创建资源组。Create a resource group. 资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed.

    $RgName   = "MyResourceGroup"
    $Location = "chinanorth"
    
    New-AzResourceGroup `
    -Name $RgName `
    -Location $Location
    
  4. 在与资源组相同的位置创建虚拟网络 (VNet) 和子网:Create a virtual network (VNet) and subnet in the same location as the resource group:

    
    # Create a subnet configuration
    $SubnetConfig = New-AzVirtualNetworkSubnetConfig `
    -Name MySubnet `
    -AddressPrefix 10.0.0.0/24
    
    # Create a virtual network
    $VNet = New-AzVirtualNetwork `
    -ResourceGroupName $RgName `
    -Location $Location `
    -Name MyVNet `
    -AddressPrefix 10.0.0.0/16 `
    -Subnet $subnetConfig
    
    # Get the subnet object
    $Subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetConfig.Name -VirtualNetwork $VNet
    
  5. 创建网络安全组 (NSG) 和规则。Create a network security group (NSG) and a rule. NSG 使用入站和出站规则保护 VM。The NSG secures the VM using inbound and outbound rules. 在本例中,会为端口 3389 创建一个入站规则,该规则允许传入的远程桌面连接。In this case, an inbound rule is created for port 3389, which allows incoming remote desktop connections.

    
    # Create an inbound network security group rule for port 3389
    
    $NSGRule = New-AzNetworkSecurityRuleConfig `
    -Name MyNsgRuleRDP `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1000 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389 -Access Allow
    
    # Create a network security group
    $NSG = New-AzNetworkSecurityGroup `
    -ResourceGroupName $RgName `
    -Location $Location `
    -Name MyNetworkSecurityGroup `
    -SecurityRules $NSGRule
    
  6. 定义 NIC 的主 IP 配置。Define the primary IP configuration for the NIC. 如果没有使用以前定义的值,请将 10.0.0.4 更改成创建的子网中的有效地址。Change 10.0.0.4 to a valid address in the subnet you created, if you didn't use the value defined previously. 分配静态 IP 地址前,建议先确认它未被占用。Before assigning a static IP address, it's recommended that you first confirm it's not already in use. 输入命令 Test-AzPrivateIPAddressAvailability -IPAddress 10.0.0.4 -VirtualNetwork $VNetEnter the command Test-AzPrivateIPAddressAvailability -IPAddress 10.0.0.4 -VirtualNetwork $VNet. 如果该地址可用,则输出返回 TrueIf the address is available, the output returns True. 如果该地址不可用,输出返回 False 以及可用的地址列表。If it's not available, the output returns False and a list of addresses that are available.

    在以下命令中,将 <replace-with-your-unique-name> 替换为要使用的唯一 DNS 名称****。In the following commands, Replace <replace-with-your-unique-name> with the unique DNS name to use. 该名称在 Azure 区域内的所有公共 IP 地址中必须唯一。The name must be unique across all public IP addresses within an Azure region. 这是一个可选参数。This is an optional parameter. 如果只想使用公共 IP 地址连接到 VM,则可删除该名称。It can be removed if you only want to connect to the VM using the public IP address.

    
    # Create a public IP address
    $PublicIP1 = New-AzPublicIpAddress `
    -Name "MyPublicIP1" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -DomainNameLabel <replace-with-your-unique-name> `
    -AllocationMethod Static
    
    #Create an IP configuration with a static private IP address and assign the public IP address to it
    $IpConfigName1 = "IPConfig-1"
    $IpConfig1     = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName1 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.4 `
    -PublicIpAddress $PublicIP1 `
    -Primary
    

    向 NIC 分配多个 IP 配置时,必须将一个配置指定为 Primary**。When you assign multiple IP configurations to a NIC, one configuration must be assigned as the -Primary.

    备注

    公共 IP 地址会产生少许费用。Public IP addresses have a nominal fee. 有关 IP 地址定价的详细信息,请阅读 IP 地址定价页。To learn more about IP address pricing, read the IP address pricing page. 可在一个订阅中使用的公共 IP 地址数有限制。There is a limit to the number of public IP addresses that can be used in a subscription. 有关限制的详细信息,请阅读 Azure limits(Azure 限制)一文。To learn more about the limits, read the Azure limits article.

  7. 定义 NIC 的辅助 IP 配置。Define the secondary IP configurations for the NIC. 可以根据需要添加或删除配置。You can add or remove configurations as necessary. 每个 IP 配置必须分配有专用 IP 地址。Each IP configuration must have a private IP address assigned. 每个配置可选择性分配有一个公共 IP 地址。Each configuration can optionally have one public IP address assigned.

    
    # Create a public IP address
    $PublicIP2 = New-AzPublicIpAddress `
    -Name "MyPublicIP2" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod Static
    
    #Create an IP configuration with a static private IP address and assign the public IP address to it
    $IpConfigName2 = "IPConfig-2"
    $IpConfig2     = New-AzNetworkInterfaceIpConfig `
    -Name $IpConfigName2 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.5 `
    -PublicIpAddress $PublicIP2
    
    $IpConfigName3 = "IpConfig-3"
    $IpConfig3 = New-AzNetworkInterfaceIpConfig `
    -Name $IPConfigName3 `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.6
    
  8. 创建 NIC 并将以下三个 IP 配置与之关联:Create the NIC and associate the three IP configurations to it:

    $NIC = New-AzNetworkInterface `
    -Name MyNIC `
    -ResourceGroupName $RgName `
    -Location $Location `
    -NetworkSecurityGroupId $NSG.Id `
    -IpConfiguration $IpConfig1,$IpConfig2,$IpConfig3
    

    备注

    尽管本文中的所有配置均分配给了一个 NIC,但可将多个 IP 配置分配给附加到 VM 的每个 NIC。Though all configurations are assigned to one NIC in this article, you can assign multiple IP configurations to every NIC attached to the VM. 若要了解如何创建具有多个 NIC 的 VM,请阅读创建具有多个 NIC 的 VM一文。To learn how to create a VM with multiple NICs, read the Create a VM with multiple NICs article.

  9. 通过输入以下命令创建 VM:Create the VM by entering the following commands:

    
    # Define a credential object. When you run these commands, you're prompted to enter a username and password for the VM you're creating.
    $cred = Get-Credential
    
    # Create a virtual machine configuration
    $VmConfig = New-AzVMConfig `
    -VMName MyVM `
    -VMSize Standard_DS1_v2 | `
    Set-AzVMOperatingSystem -Windows `
    -ComputerName MyVM `
    -Credential $cred | `
    Set-AzVMSourceImage `
    -PublisherName MicrosoftWindowsServer `
    -Offer WindowsServer `
    -Skus 2016-Datacenter `
    -Version latest | `
    Add-AzVMNetworkInterface `
    -Id $NIC.Id
    
    # Create the VM
    New-AzVM `
    -ResourceGroupName $RgName `
    -Location $Location `
    -VM $VmConfig
    
  10. 将专用 IP 地址添加到 VM 操作系统,只需完成本文将 IP 地址添加到 VM 操作系统部分针对操作系统的步骤即可。Add the private IP addresses to the VM operating system by completing the steps for your operating system in the Add IP addresses to a VM operating system section of this article. 请勿向操作系统添加公共 IP 地址。Do not add the public IP addresses to the operating system.

将 IP 地址添加到 VMAdd IP addresses to a VM

完成以下步骤可将专用和公共 IP 地址添加到 Azure 网络接口。You can add private and public IP addresses to the Azure network interface by completing the steps that follow. 以下部分的示例假定用户的 VM 已完成本文 方案 中描述的三项 IP 配置,但这不是必需的。The examples in the following sections assume that you already have a VM with the three IP configurations described in the scenario in this article, but it's not required that you do.

  1. 打开 PowerShell 命令提示符,在单个 PowerShell 会话中完成本部分余下的步骤。Open a PowerShell command prompt and complete the remaining steps in this section within a single PowerShell session. 如果尚未安装并配置 PowerShell,请先完成如何安装和配置 Azure PowerShell 一文中所述的步骤。If you don't already have PowerShell installed and configured, complete the steps in the How to install and configure Azure PowerShell article.

  2. 将以下 $Variable 的“值”分别更改为要向其添加 IP 地址的 NIC 名称,以及 NIC 所在的资源组和位置:Change the "values" of the following $Variables to the name of the NIC you want to add IP address to and the resource group and location the NIC exists in:

    $NicName  = "MyNIC"
    $RgName   = "MyResourceGroup"
    $Location = "chinanorth"
    

    如果不知道要更改的 NIC 名称,请输入以下命令,并更改上述变量的值:If you don't know the name of the NIC you want to change, enter the following commands, then change the values of the previous variables:

    Get-AzNetworkInterface | Format-Table Name, ResourceGroupName, Location
    
  3. 键入以下命令创建变量,并将它设置为现有的 NIC:Create a variable and set it to the existing NIC by typing the following command:

    $MyNIC = Get-AzNetworkInterface -Name $NicName -ResourceGroupName $RgName
    
  4. 在以下命令中,请将 MyVNet** 和 MySubnet** 更改为 NIC 连接到的 VNet 和子网的名称。In the following commands, change MyVNet and MySubnet to the names of the VNet and subnet the NIC is connected to. 输入以下命令,检索 NIC 连接到的 VNet 和子网对象:Enter the commands to retrieve the VNet and subnet objects the NIC is connected to:

    $MyVNet = Get-AzVirtualnetwork -Name MyVNet -ResourceGroupName $RgName
    $Subnet = $MyVnet.Subnets | Where-Object { $_.Name -eq "MySubnet" }
    

    如果不知道 NIC 所连接到的 VNet 或子网名称,请输入以下命令:If you don't know the VNet or subnet name the NIC is connected to, enter the following command:

    $MyNIC.IpConfigurations
    

    在输出中查找类似于如下示例输出的文本:In the output, look for text similar to the following example output:

    "Id": "/subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/MyVNet/subnets/MySubnet"
    

    在此输出中,MyVnet** 是 NIC 连接到的 VNet,MySubnet** 是其连接到的子网。In this output, MyVnet is the VNet and MySubnet is the subnet the NIC is connected to.

  5. 根据要求完成以下部分之一中的步骤:Complete the steps in one of the following sections, based on your requirements:

    添加专用 IP 地址Add a private IP address

    要将专用 IP 地址添加到 NIC,必须创建 IP 配置。To add a private IP address to a NIC, you must create an IP configuration. 以下命令创建具有静态 IP 地址 10.0.0.7 的配置。The following command creates a configuration with a static IP address of 10.0.0.7. 指定静态 IP 地址时,该地址必须是未使用的子网地址。When specifying a static IP address, it must be an unused address for the subnet. 建议首先输入 Test-AzPrivateIPAddressAvailability -IPAddress 10.0.0.7 -VirtualNetwork $myVnet 命令测试地址,确保地址可用。It's recommended that you first test the address to ensure it's available by entering the Test-AzPrivateIPAddressAvailability -IPAddress 10.0.0.7 -VirtualNetwork $myVnet command. 如果 IP 地址可用,输出会返回 TrueIf the IP address is available, the output returns True. 如果该地址不可用,则输出返回 False 以及可用的地址列表。If it's not available, the output returns False, and a list of addresses that are available.

    Add-AzNetworkInterfaceIpConfig -Name IPConfig-4 -NetworkInterface `
    $MyNIC -Subnet $Subnet -PrivateIpAddress 10.0.0.7
    

    使用唯一的配置名称和专用 IP 地址创建任意数目的配置(适用于具有静态 IP 地址的配置)。Create as many configurations as you require, using unique configuration names and private IP addresses (for configurations with static IP addresses).

    将专用 IP 地址添加到 VM 操作系统,只需完成本文 将 IP 地址添加到 VM 操作系统 部分针对操作系统的步骤即可。Add the private IP address to the VM operating system by completing the steps for your operating system in the Add IP addresses to a VM operating system section of this article.

    添加公共 IP 地址Add a public IP address

    将公共 IP 地址资源关联到新 IP 配置或现有 IP 配置即可添加公共 IP 地址。A public IP address is added by associating a public IP address resource to either a new IP configuration or an existing IP configuration. 根据需要,完成以下任一部分中的步骤。Complete the steps in one of the sections that follow, as you require.

    备注

    公共 IP 地址会产生少许费用。Public IP addresses have a nominal fee. 有关 IP 地址定价的详细信息,请阅读 IP 地址定价页。To learn more about IP address pricing, read the IP address pricing page. 可在一个订阅中使用的公共 IP 地址数有限制。There is a limit to the number of public IP addresses that can be used in a subscription. 若要了解有关限制的详细信息,请阅读 Azure 限制 一文。To learn more about the limits, read the Azure limits article.

    将公共 IP 地址资源关联到新 IP 配置Associate the public IP address resource to a new IP configuration

    每次在新 IP 配置中添加公共 IP 地址时,还必须添加专用 IP 地址,因为所有 IP 配置都必须具有专用 IP 地址。Whenever you add a public IP address in a new IP configuration, you must also add a private IP address, because all IP configurations must have a private IP address. 可添加现有公共 IP 地址资源,也可创建新的公共 IP 地址资源。You can either add an existing public IP address resource, or create a new one. 若要新建此类资源,请输入以下命令:To create a new one, enter the following command:

    $myPublicIp3 = New-AzPublicIpAddress `
    -Name "myPublicIp3" `
    -ResourceGroupName $RgName `
    -Location $Location `
    -AllocationMethod Static
    

    若要新建具有静态专用 IP 地址和关联的 myPublicIp3** 公共 IP 地址资源的 IP 配置,请输入下面的命令:To create a new IP configuration with a static private IP address and the associated myPublicIp3 public IP address resource, enter the following command:

    Add-AzNetworkInterfaceIpConfig `
    -Name IPConfig-4 `
    -NetworkInterface $myNIC `
    -Subnet $Subnet `
    -PrivateIpAddress 10.0.0.7 `
    -PublicIpAddress $myPublicIp3
    

    将公共 IP 地址资源关联到现有 IP 配置Associate the public IP address resource to an existing IP configuration

    公共 IP 地址资源仅可关联到尚未关联公共 IP 地址资源的 IP 配置。A public IP address resource can only be associated to an IP configuration that doesn't already have one associated. 输入以下命令即可确定某个 IP 配置是否具有关联的公共 IP 地址:You can determine whether an IP configuration has an associated public IP address by entering the following command:

    $MyNIC.IpConfigurations | Format-Table Name, PrivateIPAddress, PublicIPAddress, Primary
    

    会显示类似于下面的输出:You see output similar to the following:

    Name       PrivateIpAddress PublicIpAddress                                           Primary
    
    IPConfig-1 10.0.0.4         Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress    True
    IPConfig-2 10.0.0.5         Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress   False
    IpConfig-3 10.0.0.6                                                                     False
    

    IpConfig-3PublicIpAddress 列为空,这表示该 IP 配置当前没有任何关联的公共 IP 地址资源。Since the PublicIpAddress column for IpConfig-3 is blank, no public IP address resource is currently associated to it. 可将现有公共 IP 地址资源添加到 IpConfig-3,或输入以下命令进行创建:You can add an existing public IP address resource to IpConfig-3, or enter the following command to create one:

    $MyPublicIp3 = New-AzPublicIpAddress `
    -Name "MyPublicIp3" `
    -ResourceGroupName $RgName `
    -Location $Location -AllocationMethod Static
    

    输入以下命令,将公共 IP 地址资源关联到名为 IPConfig-3的现有 IP 配置:Enter the following command to associate the public IP address resource to the existing IP configuration named IpConfig-3:

    Set-AzNetworkInterfaceIpConfig `
    -Name IpConfig-3 `
    -NetworkInterface $mynic `
    -Subnet $Subnet `
    -PublicIpAddress $myPublicIp3
    
  6. 输入以下命令,为 NIC 设置新 IP 配置:Set the NIC with the new IP configuration by entering the following command:

    Set-AzNetworkInterface -NetworkInterface $MyNIC
    
  7. 输入以下命令,查看分配给 NIC 的专用 IP 地址和公共 IP 地址资源:View the private IP addresses and the public IP address resources assigned to the NIC by entering the following command:

    $MyNIC.IpConfigurations | Format-Table Name, PrivateIPAddress, PublicIPAddress, Primary
    
  8. 将专用 IP 地址添加到 VM 操作系统,只需完成本文 将 IP 地址添加到 VM 操作系统 部分针对操作系统的步骤即可。Add the private IP address to the VM operating system by completing the steps for your operating system in the Add IP addresses to a VM operating system section of this article. 请勿向操作系统添加公共 IP 地址。Do not add the public IP address to the operating system.

将 IP 地址添加到 VM 操作系统Add IP addresses to a VM operating system

连接并登录到使用多个专用 IP 地址创建的 VM。Connect and sign in to a VM you created with multiple private IP addresses. 必须手动添加 VM 中的所有专用 IP 地址(包括主要地址)。You must manually add all the private IP addresses (including the primary) that you added to the VM. 根据 VM 操作系统完成后面的步骤。Complete the steps that following for your VM operating system.

WindowsWindows

  1. 在命令提示符下,键入 ipconfig /allFrom a command prompt, type ipconfig /all. 只能看到主要专用 IP 地址(通过 DHCP)。You only see the Primary private IP address (through DHCP).

  2. 在命令提示符下键入 ncpa.cpl,打开“网络连接”窗口。 Type ncpa.cpl in the command prompt to open the Network connections window.

  3. 打开相应适配器的属性:本地区域连接Open the properties for the appropriate adapter: Local Area Connection.

  4. 双击“Internet 协议版本 4 (IPv4)”。Double-click Internet Protocol version 4 (IPv4).

  5. 单击“使用下面的 IP 地址”并输入以下值: Select Use the following IP address and enter the following values:

    • IP 地址:输入主要专用 IP 地址IP address: Enter the Primary private IP address

    • 子网掩码:根据子网设置此值。Subnet mask: Set based on your subnet. 例如,如果子网为 /24 子网,则子网掩码为 255.255.255.0。For example, if the subnet is a /24 subnet then the subnet mask is 255.255.255.0.

    • 默认网关:子网中的第一个 IP 地址。Default gateway: The first IP address in the subnet. 如果子网为 10.0.0.0/24,则网关 IP 地址为 10.0.0.1。If your subnet is 10.0.0.0/24, then the gateway IP address is 10.0.0.1.

    • 选择“使用下面的 DNS 服务器地址” 并输入以下值:Select Use the following DNS server addresses and enter the following values:

      • 首选 DNS 服务器:如果不使用自己的 DNS 服务器,请输入 168.63.129.16。Preferred DNS server: If you are not using your own DNS server, enter 168.63.129.16. 如果使用自己的 DNS 服务器,请输入服务器的 IP 地址。If you are using your own DNS server, enter the IP address for your server.
    • 选择“高级”按钮,并添加其他 IP 地址。 Select the Advanced button and add additional IP addresses. 将在前面的步骤中添加到 Azure 网络接口的每个辅助专用 IP 地址添加到分配有分配给 Azure 网络接口的主 IP 地址的 Windows 网络接口。Add each of the secondary private IP addresses, that you added to the Azure network interface in a previous step, to the Windows network interface that is assigned the primary IP address assigned to the Azure network interface.

      切勿在虚拟机的操作系统中手动分配已分配给 Azure 虚拟机的公共 IP 地址。You should never manually assign the public IP address assigned to an Azure virtual machine within the virtual machine's operating system. 在操作系统中手动设置该 IP 地址时,请确保它与分配给 Azure 网络接口的专用 IP 地址是同一地址,否则可能会丢失与虚拟机的连接。When you manually set the IP address within the operating system, ensure that it is the same address as the private IP address assigned to the Azure network interface, or you can lose connectivity to the virtual machine. 详细了解专用 IP 地址设置。Learn more about private IP address settings. 绝不要在操作系统中分配 Azure 公共 IP 地址。You should never assign an Azure public IP address within the operating system.

    • 单击“确定”关闭“TCP/IP 设置”,并再次单击“确定”关闭适配器设置。 Click OK to close out the TCP/IP settings and then OK again to close the adapter settings. 将重新建立 RDP 连接。Your RDP connection is re-established.

  6. 在命令提示符下,键入 ipconfig /allFrom a command prompt, type ipconfig /all. 此时会显示添加的所有 IP 地址,DHCP 已关闭。All IP addresses you added are shown and DHCP is turned off.

  7. 将 Windows 配置为使用 Azure 中主 IP 配置的专用 IP 地址作为 Windows 的主 IP 地址。Configure Windows to use the private IP address of the primary IP configuration in Azure as the primary IP address for Windows. 有关详细信息,请参阅无法通过具有多个 IP 地址的 Azure Windows VM 访问 InternetSee No Internet access from Azure Windows VM that has multiple IP addresses for details.

验证 (Windows)Validation (Windows)

要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请在通过上述步骤将其正确添加以后,使用以下命令:To ensure you are able to connect to the internet from your secondary IP configuration via the public IP associated it, once you have added it correctly using steps above, use the following command:

ping -S 10.0.0.5 hotmail.com

备注

对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。For secondary IP configurations, you can only ping to the Internet if the configuration has a public IP address associated with it. 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。For primary IP configurations, a public IP address is not required to ping to the Internet.

Linux (Ubuntu 14/16)Linux (Ubuntu 14/16)

我们建议你查看 Linux 发行版的最新文档。We recommend looking at the latest documentation for your Linux distribution.

  1. 打开终端窗口。Open a terminal window.

  2. 请确保以 root 用户身份操作。Make sure you are the root user. 否则,请输入以下命令:If you are not, enter the following command:

    sudo -i
    
  3. 更新网络接口(假设为“eth0”)的配置文件。Update the configuration file of the network interface (assuming 'eth0').

    • 保留 dhcp 的现有行项。Keep the existing line item for dhcp. 主要 IP 地址将保留以前的配置。The primary IP address remains configured as it was previously.

    • 使用以下命令添加其他静态 IP 地址的配置:Add a configuration for an additional static IP address with the following commands:

      cd /etc/network/interfaces.d/
      ls
      

      应会看到一个 .cfg 文件。You should see a .cfg file.

  4. 打开 文件。Open the file. 该文件的末尾应会显示以下命令行:You should see the following lines at the end of the file:

    auto eth0
    iface eth0 inet dhcp
    
  5. 在此文件包含的命令行后面添加以下命令行:Add the following lines after the lines that exist in this file:

    iface eth0 inet static
    address <your private IP address here>
    netmask <your subnet mask>
    
  6. 使用以下命令保存该文件:Save the file by using the following command:

    :wq
    
  7. 使用以下命令重置网络接口:Reset the network interface with the following command:

    sudo ifdown eth0 && sudo ifup eth0
    

    重要

    如果使用远程连接,请在同一行中同时运行 ifdown 和 ifup。Run both ifdown and ifup in the same line if using a remote connection.

  8. 使用以下命令验证 IP 地址是否已添加到网络接口:Verify the IP address is added to the network interface with the following command:

    ip addr list eth0
    

    应会在列表中看到添加的 IP 地址。You should see the IP address you added as part of the list.

Linux (Ubuntu 18.04+)Linux (Ubuntu 18.04+)

Ubuntu 18.04 及更高版本已更改为 netplan 以进行 OS 网络管理。Ubuntu 18.04 and above have changed to netplan for OS network management. 我们建议你查看 Linux 发行版的最新文档。We recommend looking at the latest documentation for your Linux distribution.

  1. 打开终端窗口。Open a terminal window.

  2. 请确保以 root 用户身份操作。Make sure you are the root user. 否则,请输入以下命令:If you are not, enter the following command:

    sudo -i
    
  3. 为第二个接口创建一个文件,并在文本编辑器中将其打开:Create a file for the second interface and open it in a text editor:

    vi /etc/netplan/60-static.yaml
    
  4. 将以下行添加到该文件,并将 10.0.0.6/24 替换为你的 IP/网络掩码:Add the following lines to the file, replacing 10.0.0.6/24 with your IP/netmask:

    network:
        version: 2
        ethernets:
            eth0:
                addresses:
                    - 10.0.0.6/24
    
  5. 使用以下命令保存该文件:Save the file by using the following command:

    :wq
    
  6. 使用 netplan try 测试更改以确认语法:Test the changes using netplan try to confirm syntax:

    netplan try
    

    备注

    netplan try 将暂时应用更改,并在 120 秒后回退更改。netplan try will apply the changes temporarily and roll the changes back after 120 seconds. 如果连接丢失,请等待 120 秒,然后重新连接。If there is a loss of connectivity, please wait 120 seconds, and then reconnect. 到那时,更改将已回退。At that time, the changes will have been rolled back.

  7. 假如 netplan try 没有问题,请应用配置更改:Assuming no issues with netplan try, apply the configuration changes:

    netplan apply
    
  8. 使用以下命令验证 IP 地址是否已添加到网络接口:Verify the IP address is added to the network interface with the following command:

    ip addr list eth0
    

    应会在列表中看到添加的 IP 地址。You should see the IP address you added as part of the list. 示例:Example:

    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:0d:3a:8c:14:a5 brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.6/24 brd 10.0.0.255 scope global eth0
        valid_lft forever preferred_lft forever
        inet 10.0.0.4/24 brd 10.0.0.255 scope global secondary eth0
        valid_lft forever preferred_lft forever
        inet6 fe80::20d:3aff:fe8c:14a5/64 scope link
        valid_lft forever preferred_lft forever
    

Linux(CentOS 和其他操作系统)Linux (CentOS, and others)

  1. 打开终端窗口。Open a terminal window.

  2. 请确保以 root 用户身份操作。Make sure you are the root user. 否则,请输入以下命令:If you are not, enter the following command:

    sudo -i
    
  3. 输入密码,根据提示的说明操作。Enter your password and follow instructions as prompted. 切换为 root 用户后,使用以下命令导航到网络脚本文件夹:Once you are the root user, navigate to the network scripts folder with the following command:

    cd /etc/sysconfig/network-scripts
    
  4. 使用以下命令列出相关的 ifcfg 文件:List the related ifcfg files using the following command:

    ls ifcfg-*
    

    应会看到其中一个文件是 ifcfg-eth0You should see ifcfg-eth0 as one of the files.

  5. 若要添加 IP 地址,请为其创建配置文件,如下所示。To add an IP address, create a configuration file for it as shown below. 请注意,必须为每个 IP 配置创建一个文件。Note that one file must be created for each IP configuration.

    touch ifcfg-eth0:0
    
  6. 使用以下命令打开 ifcfg-eth0:0 文件:Open the ifcfg-eth0:0 file with the following command:

    vi ifcfg-eth0:0
    
  7. 使用以下命令将内容添加到该文件(此示例中为 eth0:0)。Add content to the file, eth0:0 in this case, with the following command. 请务必更新基于 IP 地址的信息。Be sure to update information based on your IP address.

    DEVICE=eth0:0
    BOOTPROTO=static
    ONBOOT=yes
    IPADDR=192.168.101.101
    NETMASK=255.255.255.0
    
  8. 使用以下命令保存该文件:Save the file with the following command:

    :wq
    
  9. 运行以下命令重新启动网络服务,确保更改成功:Restart the network services and make sure the changes are successful by running the following commands:

    /etc/init.d/network restart
    ifconfig
    

    应会在返回的列表中看到添加的 IP 地址 eth0:0You should see the IP address you added, eth0:0, in the list returned.

验证 (Linux)Validation (Linux)

要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请使用以下命令:To ensure you are able to connect to the internet from your secondary IP configuration via the public IP associated it, use the following command:

ping -I 10.0.0.5 hotmail.com

备注

对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。For secondary IP configurations, you can only ping to the Internet if the configuration has a public IP address associated with it. 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。For primary IP configurations, a public IP address is not required to ping to the Internet.

对于 Linux VM,在尝试验证来自辅助 NIC 的出站连接时,可能需要添加适当的路由。For Linux VMs, when trying to validate outbound connectivity from a secondary NIC, you may need to add appropriate routes. 可通过多种方式来执行此操作。There are many ways to do this. 请参阅针对 Linux 分发的相应文档。Please see appropriate documentation for your Linux distribution. 下面是实现此目的的一种方法:The following is one method to accomplish this:

echo 150 custom >> /etc/iproute2/rt_tables 

ip rule add from 10.0.0.5 lookup custom
ip route add default via 10.0.0.1 dev eth2 table custom

  • 确保将Be sure to replace:
    • 10.0.0.5 替换为专用 IP 地址,该地址有一个与之关联的公共 IP 地址。10.0.0.5 with the private IP address that has a public IP address associated to it
    • 10.0.0.1 替换为默认网关10.0.0.1 to your default gateway
    • eth2 替换为辅助 NIC 的名称eth2 to the name of your secondary NIC