您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

在一个 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 CLI 通过 Azure 资源管理器部署模型创建虚拟机 (VM)。This article explains how to create a virtual machine (VM) through the Azure Resource Manager deployment model using the Azure CLI. 无法将多个 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 地址的示例虚拟机。The steps that follow explain how to create an example virtual machine with multiple IP addresses, as described in the scenario. 根据实现的需要,需要更改 "" 中的变量值和 IP 地址类型。Change variable values in "" and IP address types, as required, for your implementation.

  1. 安装 Azure CLI(如果尚未安装)。Install the Azure CLI if you don't already have it installed.
  2. 通过完成为 Linux VM 创建 SSH 公钥和私钥对中的步骤创建适用于 Linux VM 的 SSH 公钥和私钥对。Create an SSH public and private key pair for Linux VMs by completing the steps in the Create an SSH public and private key pair for Linux VMs.
  3. 从命令行界面使用命令 az login 登录,并选择要使用的订阅。From a command shell, login with the command az login and select the subscription you're using.
  4. 通过在 Linux 或 Mac 计算机上执行以下脚本创建 VM。Create the VM by executing the script that follows on a Linux or Mac computer. 该脚本创建 1 个资源组、1 个虚拟网络 (VNet)、1 个具有 3 个 IP 配置的 NIC,以及附加 2 个 NIC 的 VM。The script creates a resource group, one virtual network (VNet), one NIC with three IP configurations, and a VM with the two NICs attached to it. NIC、公共 IP 地址、虚拟网络和 VM 资源均必须位于同一位置和订阅。The NIC, public IP address, virtual network, and VM resources must all exist in the same location and subscription. 虽然资源不必都存在于同一资源组中,但是在以下脚本中资源都存在于同一资源组中。Though the resources don't all have to exist in the same resource group, in the following script they do.
    
#!/bin/sh
    
RgName="myResourceGroup"
Location="westcentralus"
az group create --name $RgName --location $Location
    
# Create a public IP address resource with a static IP address using the `--allocation-method Static` option. If you
# do not specify this option, the address is allocated dynamically. The address is assigned to the resource from a pool
# of IP addresses unique to each Azure region. Download and view the file from
# https://www.microsoft.com/en-us/download/details.aspx?id=41653 that lists the ranges for each region.

PipName="myPublicIP"

# This name must be unique within an Azure location.
DnsName="myDNSName"

az network public-ip create \
--name $PipName \
--resource-group $RgName \
--location $Location \
--dns-name $DnsName\
--allocation-method Static

# Create a virtual network with one subnet

VnetName="myVnet"
VnetPrefix="10.0.0.0/16"
VnetSubnetName="mySubnet"
VnetSubnetPrefix="10.0.0.0/24"

az network vnet create \
--name $VnetName \
--resource-group $RgName \
--location $Location \
--address-prefix $VnetPrefix \
--subnet-name $VnetSubnetName \
--subnet-prefix $VnetSubnetPrefix

# Create a network interface connected to the subnet and associate the public IP address to it. Azure will create the
# first IP configuration with a static private IP address and will associate the public IP address resource to it.

NicName="MyNic1"
az network nic create \
--name $NicName \
--resource-group $RgName \
--location $Location \
--subnet $VnetSubnet1Name \
--private-ip-address 10.0.0.4
--vnet-name $VnetName \
--public-ip-address $PipName
    
# Create a second public IP address, a second IP configuration, and associate it to the NIC. This configuration has a
# static public IP address and a static private IP address.

az network public-ip create \
--resource-group $RgName \
--location $Location \
--name myPublicIP2 \
--dns-name mypublicdns2 \
--allocation-method Static

az network nic ip-config create \
--resource-group $RgName \
--nic-name $NicName \
--name IPConfig-2 \
--private-ip-address 10.0.0.5 \
--public-ip-name myPublicIP2

# Create a third IP configuration, and associate it to the NIC. This configuration has  static private IP address and   # no public IP address.

az network nic ip-config create \
--resource-group $RgName \
--nic-name $NicName \
--private-ip-address 10.0.0.6 \
--name IPConfig-3

# Note: Though this article assigns all IP configurations to a single NIC, you can also assign multiple IP configurations
# to any NIC in a VM. To learn how to create a VM with multiple NICs, read the Create a VM with multiple NICs 
# article: https://docs.microsoft.com/azure/virtual-network/virtual-network-deploy-multinic-arm-cli.

# Create a VM and attach the NIC.

VmName="myVm"

# Replace the value for the following **VmSize** variable with a value from the
# https://docs.microsoft.com/azure/virtual-machines/sizes article. The script fails if the VM size
# is not supported in the location you select. Run the `azure vm sizes --location eastcentralus` command to get a full list
# of VMs in US West Central, for example.

VmSize="Standard_DS1"

# Replace the value for the OsImage variable value with a value for *urn* from the output returned by entering the
# `az vm image list` command.

OsImage="credativ:Debian:8:latest"

Username="adminuser"

# Replace the following value with the path to your public key file. If you're creating a Windows VM, remove the following
# line and you'll be prompted for the password you want to configure for the VM.

SshKeyValue="~/.ssh/id_rsa.pub"

az vm create \
--name $VmName \
--resource-group $RgName \
--image $OsImage \
--location $Location \
--size $VmSize \
--nics $NicName \
--admin-username $Username \
--ssh-key-value $SshKeyValue

除了创建具有附带 3 个 IP 配置的 NIC 的 VM,该脚本还创建:In addition to creating a VM with a NIC with 3 IP configurations, the script creates:

  • 单个高级托管磁盘(默认情况下),但对于可以创建的磁盘类型,可以有其他选择。A single premium managed disk by default, but you have other options for the disk type you can create. 有关详细信息,请阅读使用 Azure CLI 创建 Linux VM 一文。Read the Create a Linux VM using the Azure CLI article for details.
  • 一个包含 1 个子网和 2 个公共 IP 地址的虚拟网络。A virtual network with one subnet and two public IP addresses. 或者,可以使用现有虚拟网络、子网、NIC 或公共 IP 地址资源。Alternatively, you can use existing virtual network, subnet, NIC, or public IP address resources. 若要了解如何使用现有网络资源,而不是创建其他资源,请输入 az vm create -hTo learn how to use existing network resources rather than creating additional resources, enter az vm create -h.

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

创建 VM 后,输入 az network nic show --name MyNic1 --resource-group myResourceGroup 命令查看 NIC 配置。After the VM is created, enter the az network nic show --name MyNic1 --resource-group myResourceGroup command to view the NIC configuration. 输入 az network nic ip-config list --nic-name MyNic1 --resource-group myResourceGroup --output table ,查看与 NIC 关联的 IP 配置的列表。Enter the az network nic ip-config list --nic-name MyNic1 --resource-group myResourceGroup --output table to view a list of the IP configurations associated to the NIC.

将专用 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 地址添加到 VMAdd IP addresses to a VM

完成以下步骤可将其他专用和公共 IP 地址添加到现有 Azure 网络接口。You can add additional private and public IP addresses to an existing Azure network interface by completing the steps that follow. 示例根据本文所述的 方案 生成。The examples build upon the scenario described in this article.

  1. 打开命令行界面,并在单个会话中完成本部分的剩余步骤。Open a command shell and complete the remaining steps in this section within a single session. 如果尚未安装并配置 Azure CLI,请完成 Azure CLI 安装一文中的步骤,并使用 az-login 命令登录到 Azure 帐户。If you don't already have Azure CLI installed and configured, complete the steps in the Azure CLI installation article and login to your Azure account with the az-login command.

  2. 根据要求完成以下部分之一中的步骤: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 using the command that follows. 静态 IP 地址必须是未使用的子网地址。The static IP address must be an unused address for the subnet.

    az network nic ip-config create \
    --resource-group myResourceGroup \
    --nic-name myNic1 \
    --private-ip-address 10.0.0.7 \
    --name IPConfig-4
    

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

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

    将公共 IP 地址关联到新 IP 配置或现有 IP 配置即可添加它。A public IP address is added by associating it 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 limits(Azure 限制)一文。To learn more about the limits, read the Azure limits article.

    • 将资源关联到新 IP 配置Associate the 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:

      az network public-ip create \
      --resource-group myResourceGroup \
      --location westcentralus \
      --name myPublicIP3 \
      --dns-name mypublicdns3
      

      若要新建具有静态专用 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:

      az network nic ip-config create \
      --resource-group myResourceGroup \
      --nic-name myNic1 \
      --name IPConfig-5 \
      --private-ip-address 10.0.0.8
      --public-ip-address myPublicIP3
      
    • 将资源关联到现有 IP 配置 公共 IP 地址资源仅可关联到尚未关联公共 IP 地址资源的 IP 配置。Associate the resource to an existing IP configuration 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:

      az network nic ip-config list \
      --resource-group myResourceGroup \
      --nic-name myNic1 \
      --query "[?provisioningState=='Succeeded'].{ Name: name, PublicIpAddressId: publicIpAddress.id }" --output table
      

      返回的输出:Returned output:

      Name        PublicIpAddressId
      
      ipconfig1   /subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP1
      IPConfig-2  /subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP2
      IPConfig-3
      

      由于输出中 IpConfig-3PublicIpAddressId 列空白,因此其当前未关联公共 IP 地址资源。Since the PublicIpAddressId column for IpConfig-3 is blank in the output, 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:

      az network public-ip create \
      --resource-group  myResourceGroup
      --location westcentralus \
      --name myPublicIP3 \
      --dns-name mypublicdns3 \
      --allocation-method Static
      

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

      az network nic ip-config update \
      --resource-group myResourceGroup \
      --nic-name myNic1 \
      --name IPConfig-3 \
      --public-ip myPublicIP3
      
  3. 输入以下命令,查看分配给 NIC 的专用 IP 地址和公共 IP 地址资源 ID:View the private IP addresses and the public IP address resource Ids assigned to the NIC by entering the following command:

    az network nic ip-config list \
    --resource-group myResourceGroup \
    --nic-name myNic1 \
    --query "[?provisioningState=='Succeeded'].{ Name: name, PrivateIpAddress: privateIpAddress, PrivateIpAllocationMethod: privateIpAllocationMethod, PublicIpAddressId: publicIpAddress.id }" --output table
    

    返回的输出:Returned output:

    Name        PrivateIpAddress    PrivateIpAllocationMethod   PublicIpAddressId
    
    ipconfig1   10.0.0.4            Static                      /subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP1
    IPConfig-2  10.0.0.5            Static                      /subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP2
    IPConfig-3  10.0.0.6            Static                      /subscriptions/[Id]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP3
    
  4. 根据本文将 IP 地址添加到 VM 操作系统部分中的说明,将添加到 NIC 的专用 IP 地址添加到 VM 操作系统。Add the private IP addresses you added to the NIC to the VM operating system by following the instructions 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 地址添加到 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.

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