使用 PowerShell 對虛擬機器指派多個 IP 位址Assign multiple IP addresses to virtual machines using PowerShell

注意

本文已更新為使用 Azure Az PowerShell 模組。This article has been updated to use the Azure Az PowerShell module. Az PowerShell 模組是用來與 Azure 互動的建議 PowerShell 模組。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要開始使用 Az PowerShell 模組,請參閱安裝 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要瞭解如何遷移至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 遷移至 Az。To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

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. 將多個 IP 位址指派給 VM 可啟用下列功能: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 Load Balancer 後端集區。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 部署模型,請參閱了解部署模型文章。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 命令來登入您的帳戶。Login to your account with the Connect-AzAccount command.

  3. 使用您選擇的名稱和位置來取代 myResourceGroupwestusReplace myResourceGroup and westus 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 = "westus"
    
    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
    

    當您指派多個 IP 組態給 NIC 時,必須將一個組態指派為 -PrimaryWhen 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 限制文章。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. 將下列 $Variables 的「值」變更為您想要對其新增 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 = "westus"
    

    如果您不知道要變更的 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. 在下列命令中,將 MyVNetMySubnet 變更成 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 是 VNet 而 MySubnet 是 NIC 所連線的子網路。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 位址資源,或建立一個新的資源。You can either add an existing public IP address resource, or create a new one. 若要建立新的公用 IP 位址資源,請輸入下列命令: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 組態產生關聯。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 3 的 PublicIpAddress 欄是空白,目前沒有與其相關聯的公用 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. 輸入下列命令,可設定具有新 IP 組態的 NIC︰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.

Windows ServerWindows Server

展開Expand
  1. 從命令提示字元輸入 ipconfig /allFrom a command prompt, type ipconfig /all. 您只會看到 Primary 私人 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: Ethernet.

  4. 按兩下 [網際網路通訊協定第 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. (針對替代 DNS 伺服器,您可以挑選任何免費的公用 DNS 伺服器位址)。(For Alternate DNS Server you can pick any free public DNS server address.)
    • 選取 [進階] 按鈕,然後新增其他 IP 位址。Select the Advanced button and add additional IP addresses. 將您在上一個步驟中新增至 Azure 網路介面的每個次要私人 IP 位址新增至 Windows 網路介面,而該 Windows 網路介面已獲取指派給 Azure 網路介面的主要 IP 位址。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 是關閉的。Verify 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 無權存取網際網路See No Internet access from Azure Windows VM that has multiple IP addresses for details.

驗證 (Windows Server)Validation (Windows Server)

若要確保您能夠透過相關聯的公用 IP 從第二個 IP 組態連接到網際網路,在使用上述步驟正確地新增該 IP 後,請使用下列命令 (將 10.0.0.7 取代為次要、私人 IP 位址):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 (replacing 10.0.0.7 with the secondary, private IP address):

ping -S 10.0.0.7 outlook.com

注意

對於次要 IP 組態,如果組態有與其相關聯的公用 IP 位址,您只可以 Ping 網際網路。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 網際網路。For primary IP configurations, a public IP address is not required to ping to the Internet.

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

展開Expand 我們建議您查看 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.

驗證 (Ubuntu 14/16)Validation (Ubuntu 14/16)

若要確保您能夠透過相關聯的公用 IP 從第二個 IP 組態連接到網際網路,請使用下列命令︰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 outlook.com

注意

對於次要 IP 組態,如果組態有與其相關聯的公用 IP 位址,您只可以 Ping 網際網路。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 網際網路。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

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

展開Expand Ubuntu 18.04 和更新版本已變更為 OS 網路管理的 `netplan`。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.

  1. 假設 netplan try 沒有問題,請套用設定變更:Assuming no issues with netplan try, apply the configuration changes:

    netplan apply
    
  2. 使用下列命令確認 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
    

驗證 (Ubuntu 18.04+)Validation (Ubuntu 18.04+)

若要確保您能夠透過相關聯的公用 IP 從第二個 IP 組態連接到網際網路,請使用下列命令︰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 outlook.com

注意

對於次要 IP 組態,如果組態有與其相關聯的公用 IP 位址,您只可以 Ping 網際網路。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 網際網路。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

Linux (Red Hat、CentOS 以及其他)Linux (Red Hat, CentOS, and others)

展開Expand
  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.

驗證 (Red Hat、CentOS 以及其他)Validation (Red Hat, CentOS, and others)

若要確保您能夠透過相關聯的公用 IP 從第二個 IP 組態連接到網際網路,請使用下列命令︰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 outlook.com

注意

對於次要 IP 組態,如果組態有與其相關聯的公用 IP 位址,您只可以 Ping 網際網路。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 網際網路。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