使用 Azure 门户将多个 IP 地址分配给虚拟机Assign multiple IP addresses to virtual machines using the Azure portal

在一个 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.

本文介绍如何使用 Azure 门户通过 Azure Resource Manager 部署模型创建虚拟机 (VM)。This article explains how to create a virtual machine (VM) through the Azure Resource Manager deployment model using the Azure portal. 无法将多个 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 地址或一个静态专用 IP 地址的 VM,则必须使用 PowerShell 或 Azure CLI 来创建。If you want to create a VM with multiple IP addresses, or a static private IP address, you must create it using PowerShell or the Azure CLI. 相关操作步骤,请单击本文顶部的 PowerShell 或 CLI 选项。To learn how, click the PowerShell or CLI options at the top of this article. 可使用单个动态专用 IP 地址和(可选)单个公共 IP 地址创建 VM。You can create a VM with a single dynamic private IP address and (optionally) a single public IP address. 请按照创建 Windows VM创建 Linux VM 文章中的步骤使用门户。Use the portal by following the steps in the Create a Windows VM or Create a Linux VM articles. 创建 VM 后,可以按照本文向 VM 添加 IP 地址部分中的以下步骤,可使用门户将 IP 地址类型从动态更改为静态并添加其他 IP 地址。After you create the VM, you can change the IP address type from dynamic to static and add additional IP addresses using the portal by following steps in the Add IP addresses to a VM section of this article.

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

完成以下步骤可将专用和公共 IP 地址添加到 Azure 网络接口。You can add private and public IP addresses to an 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, but it's not required.

核心步骤Core steps

  1. 根据需要浏览到 Azure 门户(网址为 https://portal.azure.cn )并登录。Browse to the Azure portal at https://portal.azure.cn and sign into it, if necessary.

  2. 在门户中单击“更多服务”> 在筛选框中键入“虚拟机”,并单击“虚拟机”**********。In the portal, click More services > type virtual machines in the filter box, and then click Virtual machines.

  3. 在“虚拟机”窗格中,单击要向其添加 IP 地址的 VM****。In the Virtual machines pane, click the VM you want to add IP addresses to. 导航到“网络”**** 选项卡。单击页面上的“网络接口”****。Navigate to Networking Tab. Click Network interface on the page. 如下图所示:As shown in the picture below:

    将公共 IP 地址添加到 VM

  4. 在“网络接口”**** 窗格中,单击“IP 配置”****。In the Network interface pane, click the IP configurations.

  5. 在针对所选 NIC 显示的窗格中,单击“IP 配置”****。In the pane that appears for the NIC you selected, click IP configurations. 单击“添加”****,根据要添加的 IP 地址的类型完成以下某个部分的步骤,然后单击“确定”****。Click Add, complete the steps in one of sections that follow, based on the type of IP address you want to add, and then click OK.

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

完成以下步骤,添加新的专用 IP 地址:Complete the following steps to add a new private IP address:

  1. 完成本文 核心步骤 部分的步骤。Complete the steps in the Core steps section of this article.

  2. 单击“添加” ****。Click Add. 在显示的“添加 IP 配置”窗格中,创建名为“IPConfig-4”的 IP 配置,以“10.0.0.7”作为“静态”专用 IP 地址,并单击“确定”****** ** ******。In the Add IP configuration pane that appears, create an IP configuration named IPConfig-4 with 10.0.0.7 as a Static private IP address, then click OK.

    备注

    在添加静态 IP 地址时,必须在 NIC 连接到的子网中指定未使用的有效地址。When adding a static IP address, you must specify an unused, valid address on the subnet the NIC is connected to. 如果所选地址不可用,门户会对 IP 地址显示一个 X,而你必须选择其他地址。If the address you select is not available, the portal displays an X for the IP address and you must select a different one.

  3. 单击“确定”后,窗格将关闭且列出新的 IP 配置。Once you click OK, the pane closes and you see the new IP configuration listed. 单击“确定”关闭“添加 IP 配置”窗格**** ****。Click OK to close the Add IP configuration pane.

  4. 可以单击“添加”添加其他 IP 配置,也可以关闭所有打开的边栏选项卡,完成添加 IP 地址的操作****。You can click Add to add additional IP configurations, or close all open blades to finish adding IP addresses.

  5. 通过完成本文内将 IP 地址添加到 VM 操作系统部分中的步骤,将专用 IP 地址添加到 VM 操作系统。Add the private IP addresses to the VM operating system by completing the steps 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.

备注

公共 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.

创建公共 IP 地址资源Create a public IP address resource

公共 IP 地址是针对公共 IP 地址资源的一种设置。A public IP address is one setting for a public IP address resource. 如果公共 IP 地址资源目前所关联到的 IP 配置不是你想要关联到的 IP 配置,可跳过以下步骤,根据需要完成后面某个部分的步骤。If you have a public IP address resource that is not currently associated to an IP configuration that you want to associate to an IP configuration, skip the following steps and complete the steps in one of the sections that follow, as you require. 如果没有可用的公共 IP 地址资源,可通过以下步骤创建一个:If you don't have an available public IP address resource, complete the following steps to create one:

  1. 根据需要浏览到 Azure 门户(网址为 https://portal.azure.cn )并登录。Browse to the Azure portal at https://portal.azure.cn and sign into it, if necessary.

  2. 在门户中,单击“创建资源” > “网络” > “公共 IP 地址”**** **** ****。In the portal, click Create a resource > Networking > Public IP address.

  3. 在显示的“创建公共 IP 地址”窗格中,再输入名称,选择“IP 地址分配”类型、“订阅”、“资源组”和“位置”,然后单击“创建”,如下图所示**** **** **** **** **** **** ****:In the Create public IP address pane that appears, enter a Name, select an IP address assignment type, a Subscription, a Resource group, and a Location, then click Create, as shown in the following picture:

    创建公共 IP 地址资源

  4. 完成下面某个部分的步骤,将公共 IP 地址资源关联到某个 IP 配置。Complete the steps in one of the sections that follow to associate the public IP address resource to an IP configuration.

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

  1. 完成本文核心步骤部分的步骤。Complete the steps in the Core steps section of this article.

  2. 单击“添加” ****。Click Add. 在显示的“添加 IP 配置”窗格中,创建名为“IPConfig-4”的 IP 配置******。In the Add IP configuration pane that appears, create an IP configuration named IPConfig-4. 启用“公共 IP 地址”****,并从显示的“选择公共 IP 地址”窗格中选择一个现有的可用公共 IP 地址资源****。Enable the Public IP address and select an existing, available public IP address resource from the Choose public IP address pane that appears.

    在选择公共 IP 地址资源后单击“确定”,窗格随即关闭****。Once you've selected the public IP address resource, click OK and the pane closes. 如果还没有公共 IP 地址,则可通过完成本文创建公共 IP 地址资源部分的步骤创建一个。If you don't have an existing public IP address, you can create one by completing the steps in the Create a public IP address resource section of this article.

  3. 查看新 IP 配置。Review the new IP configuration. 即使没有向 IP 配置显式分配专用 IP 地址,也会自动分配一个,因为所有 IP 配置都必须有一个专用 IP 地址。Even though a private IP address wasn't explicitly assigned, one was automatically assigned to the IP configuration, because all IP configurations must have a private IP address.

  4. 可以单击“添加”添加其他 IP 配置,也可以关闭所有打开的边栏选项卡,完成添加 IP 地址的操作****。You can click Add to add additional IP configurations, or close all open blades to finish adding IP addresses.

  5. 将专用 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 地址资源关联到现有 IP 配置Associate the public IP address resource to an existing IP configuration

  1. 完成本文核心步骤部分的步骤。Complete the steps in the Core steps section of this article.
  2. 单击其中要添加公共 IP 地址资源的 IP 配置。Click the IP configuration you want to add the public IP address resource to.
  3. 在出现的“IPConfig”窗格中,单击“IP 地址”****。In the IPConfig pane that appears, click IP address.
  4. 在出现的“选择公共 IP 地址”窗格中,选择公共 IP 地址****。In the Choose public IP address pane that appears, select a public IP address.
  5. 单击“保存”,窗格随即关闭****。Click Save and the panes close. 如果还没有公共 IP 地址,则可通过完成本文创建公共 IP 地址资源部分的步骤创建一个。If you don't have an existing public IP address, you can create one by completing the steps in the Create a public IP address resource section of this article.
  6. 查看新 IP 配置。Review the new IP configuration.
  7. 可以单击“添加”添加其他 IP 配置,也可以关闭所有打开的边栏选项卡,完成添加 IP 地址的操作****。You can click Add to add additional IP configurations, or close all open blades to finish adding IP addresses. 请勿向操作系统添加公共 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. 只能看到主要专用 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. 双击“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. (对于备用 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 地址添加到分配有分配给 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。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 访问 InternetSee No Internet access from Azure Windows VM that has multiple IP addresses for details.

验证 (Windows Server)Validation (Windows Server)

要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请在通过上述步骤将其正确添加以后,使用以下命令(使用辅助专用 IP 地址替换 10.0.0.7):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 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)

展开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 连接到 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 outlook.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

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.

  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
    

验证 (Ubuntu 18.04+)Validation (Ubuntu 18.04+)

要确保能够从辅助 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 outlook.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

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 连接到 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 outlook.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