在虚拟机中添加或删除网络接口。Add network interfaces to or remove network interfaces from virtual machines

了解在创建 Azure 虚拟机 (VM) 时如何添加现有的网络接口。Learn how to add an existing network interface when you create an Azure virtual machine (VM). 还将了解如何在现有 VM 中添加或删除处于已停止(已解除分配)状态的网络接口。Also learn to add or remove network interfaces from an existing VM in the stopped (deallocated) state. Azure VM 通过网络接口与 Internet、Azure 及本地资源通信。A network interface enables an Azure VM to communicate with internet, Azure, and on-premises resources. VM 有一个或多个网络接口。A VM has one or more network interfaces.

如果需要为网络接口添加、更改或删除 IP 地址,请参阅管理网络接口 IP 地址If you need to add, change, or remove IP addresses for a network interface, see Manage network interface IP addresses. 若要创建、更改或删除网络接口,请参阅管理网络接口To create, change, or delete network interfaces, see Manage network interfaces.

准备阶段Before you begin

备注

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

如果你没有 Azure 帐户,请使用有效的订阅设置一个帐户。If you don't have one, set up an Azure account with an active subscription. 免费创建帐户Create an account for free. 在开始学习本文的余下内容之前,请完成以下任务之一:Complete one of these tasks before starting the remainder of this article:

  • 门户用户:使用 Azure 帐户登录到 Azure 门户Portal users: Sign in to the Azure portal with your Azure account.

  • PowerShell 用户:在计算机中运行 PowerShell。PowerShell users: Run PowerShell from your computer.

    如果在本地运行 PowerShell,请使用 Azure PowerShell 模块 1.0.0 或更高版本。If you're running PowerShell locally, use Azure PowerShell module version 1.0.0 or later. 运行 Get-Module -ListAvailable Az.Network 查找已安装的版本。Run Get-Module -ListAvailable Az.Network to find the installed version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 运行 Connect-AzAccount -Environment AzureChinaCloud,创建与 Azure 的连接。Run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

  • Azure 命令行接口 (CLI) 用户:在计算机中运行 CLI。Azure Command-line interface (CLI) users: Run the CLI from your computer.

    如果在本地运行 Azure CLI,请使用 Azure CLI 2.0.26 或更高版本。Use Azure CLI version 2.0.26 or later if you're running the Azure CLI locally. 运行 az --version 查找已安装的版本。Run az --version to find the installed version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI. 运行 az login,创建与 Azure 的连接。Run az login to create a connection with Azure.

将现有网络接口添加到新 VMAdd existing network interfaces to a new VM

通过门户创建虚拟机时,门户会使用默认设置创建一个网络接口,并将其附加到 VM。When you create a virtual machine through the portal, the portal creates a network interface with default settings and attaches the network interface to the VM for you. 无法使用门户将现有网络接口添加到新的 VM,或创建具有多个网络接口的 VM。You can't use the portal to add existing network interfaces to a new VM, or to create a VM with multiple network interfaces. 可以使用 CLI 或 PowerShell 执行这两项操作。You can do both by using the CLI or PowerShell. 请确保熟悉约束Be sure to familiarize yourself with the constraints. 如果要创建具有多个网络接口的 VM,还必须将操作系统配置为在创建 VM 后正确使用它们。If you create a VM with multiple network interfaces, you must also configure the operating system to use them properly after you create the VM. 了解如何将 LinuxWindows 配置为使用多个网络接口。Learn how to configure Linux or Windows for multiple network interfaces.

命令Commands

在创建 VM 之前,请创建网络接口Before you create the VM, Create a network interface.

工具Tool 命令Command
CLICLI az network nic createaz network nic create
PowerShellPowerShell New-AzNetworkInterfaceNew-AzNetworkInterface

将网络接口添加到现有 VMAdd a network interface to an existing VM

若要向虚拟机添加网络接口,请执行以下操作:To add a network interface to your virtual machine:

  1. 转到 Azure 门户来查找现有虚拟机。Go to the Azure portal to find an existing virtual machine. 搜索并选择“虚拟机”。Search for and select Virtual machines.

  2. 选择你的 VM 的名称。Select the name of your VM. 该 VM 必须支持要添加的网络接口的数量。The VM must support the number of network interfaces you want to add. 若要了解每个 VM 大小支持的网络接口数量,请参阅 Azure 中适用于 Linux VMWindows VM 的大小。To find out how many network interfaces each VM size supports, see the sizes in Azure for Linux VMs or Windows VMs.

  3. 在 VM 命令栏中,选择“停止”,然后在确认对话框中选择“确定”。In the VM command bar, select Stop, and then OK in the confirmation dialog box. 然后,等待 VM 的“状态”更改为“已停止(已解除分配)” 。Then wait until the Status of the VM changes to Stopped (deallocated).

  4. 从 VM 菜单栏中,选择“网络” > “附加网络接口”。 From the VM menu bar, choose Networking > Attach network interface. 然后,在“附加现有网络接口”中,选择要附加的网络接口,并选择“确定”。Then in Attach existing network interface, choose the network interface you'd like to attach, and select OK.

    备注

    选择的网络接口无法启用加速网络,无法获得 IPv6 地址,并且必须位于包含当前附加到 VM 的网络接口的虚拟网络中。The network interface you select can't have accelerated networking enabled, can't have an IPv6 address assigned to it, and must exist in the same virtual network with the network interface currently attached to the VM.

    如果没有现有的网络接口,必须先创建一个。If you don't have an existing network interface, you must first create one. 为此,请选择“创建网络接口”。To do so, select Create network interface. 若要详细了解如何创建网络接口,请参阅创建网络接口To learn more about how to create a network interface, see Create a network interface. 若要详细了解将网络接口添加到虚拟机时的约束,请参阅约束To learn more about additional constraints when adding network interfaces to virtual machines, see Constraints.

  5. 从 VM 菜单栏中,选择“概览” > “启动”来重启虚拟机。 From the VM menu bar, choose Overview > Start to restart the virtual machine.

现在,你可以配置 VM 操作系统以正确地使用多个网络接口。Now you can configure the VM operating system to use multiple network interfaces properly. 了解如何将 LinuxWindows 配置为使用多个网络接口。Learn how to configure Linux or Windows for multiple network interfaces.

命令Commands

工具Tool 命令Command
CLICLI az vm nic add(参考);详细步骤az vm nic add (reference); detailed steps
PowerShellPowerShell Add-AzVMNetworkInterface(参考);详细步骤Add-AzVMNetworkInterface (reference); detailed steps

查看 VM 的网络接口View network interfaces for a VM

可以查看当前附加到 VM 的网络接口,了解每个网络接口的配置,以及分配给每个网络接口的 IP 地址。You can view the network interfaces currently attached to a VM to learn about each network interface's configuration, and the IP addresses assigned to each network interface.

  1. 转到 Azure 门户来查找现有虚拟机。Go to the Azure portal to find an existing virtual machine. 搜索并选择“虚拟机”。Search for and select Virtual machines.

    备注

    使用分配有订阅“所有者”、“参与者”或“网络参与者”角色的帐户登录。Sign in using an account that is assigned the Owner, Contributor, or Network Contributor role for your subscription. 若要详细了解如何向帐户分配角色,请参阅针对 Azure 基于角色的访问控制的内置角色To learn more about how to assign roles to accounts, see Built-in roles for Azure role-based access control.

  2. 选择你要查看其附加的网络接口的 VM 的名称。Select the name of the VM for which you want to view attached network interfaces.

  3. 在 VM 菜单栏中,选择“网络”。In the VM menu bar, select Networking.

若要了解网络接口的设置及其更改方法,请参阅管理网络接口To learn about network interface settings and how to change them, see Manage network interfaces. 若要了解如何添加、更改或删除分配到网络接口的 IP 地址,请参阅管理网络接口 IP 地址To learn about how to add, change, or remove IP addresses assigned to a network interface, see Manage network interface IP addresses.

命令Commands

工具Tool 命令Command
CLICLI az vm nic listaz vm nic list
PowerShellPowerShell Get-AzVMGet-AzVM

从 VM 中删除网络接口Remove a network interface from a VM

  1. 转到 Azure 门户来查找现有虚拟机。Go to the Azure portal to find an existing virtual machine. 搜索并选择“虚拟机”。Search for and select Virtual machines.

  2. 选择你要查看其附加的网络接口的 VM 的名称。Select the name of the VM for which you want to view attached network interfaces.

  3. 在 VM 工具栏中,选择“停止”。In the VM toolbar, pick Stop.

  4. 等到 VM 的“状态”更改为“已停止(已解除分配)” 。Wait until the Status of the VM changes to Stopped (deallocated).

  5. 从 VM 菜单栏中,选择“网络” > “分离网络接口”。 From the VM menu bar, choose Networking > Detach network interface.

  6. 在“分离网络接口”对话框中,选择要分离的网络接口。In the Detach network interface dialog box, select the network interface you'd like to detach. 然后选择“确定”。Then select OK.

    备注

    如果只列出了一个网络接口,则无法将它分离,因为虚拟机上必须始终至少附加一个网络接口。If only one network interface is listed, you can't detach it, because a virtual machine must always have at least one network interface attached to it.

命令Commands

工具Tool 命令Command
CLICLI az vm nic remove(参考);详细步骤az vm nic remove (reference); detailed steps
PowerShellPowerShell Remove-AzVMNetworkInterface(参考);详细步骤Remove-AzVMNetworkInterface (reference); detailed steps

约束Constraints

  • VM 上必须至少附加一个网络接口。A VM must have at least one network interface attached to it.

  • VM 上附加的网络接口数量不能超过 VM 大小支持的数量。A VM can only have as many network interfaces attached to it as the VM size supports. 若要详细了解每个 VM 大小支持的网络接口数量,请参阅 Azure 中适用于 Linux VMWindows VM 的大小。To learn more about how many network interfaces each VM size supports, see the sizes in Azure for Linux VMs or Windows VMs. 所有大小至少支持两个网络接口。All sizes support at least two network interfaces.

  • 目前,添加到一个 VM 的网络接口不能附加到另一个 VM。The network interfaces you add to a VM can't currently be attached to another VM. 若要详细了解如何创建网络接口,请参阅创建网络接口To learn more about how to create network interfaces, see Create a network interface.

  • 过去,只能向支持多个网络接口且至少是使用两个网络接口创建的 VM 中添加网络接口。In the past, you could add network interfaces only to VMs that supported multiple network interfaces and were created with at least two network interfaces. 不能向使用一个网络接口创建的 VM 添加网络接口,即使 VM 大小支持多个网络接口。You couldn't add a network interface to a VM that was created with one network interface, even if the VM size supported more than one network interface. 相反,只能从至少有三个网络接口的 VM 中删除网络接口,因为使用至少两个网络接口创建的 VM 必须始终有至少两个网络接口。Conversely, you could only remove network interfaces from a VM with at least three network interfaces, because VMs created with at least two network interfaces always had to have at least two network interfaces. 这些约束将不再应用。These constraints no longer apply. 现在,可以创建包含任意数量(但不能超过 VM 大小支持的数量)的网络接口的 VM。You can now create a VM with any number of network interfaces (up to the number supported by the VM size).

  • 默认情况下,VM 上附加的第一个网络接口是主网络接口。By default, the first network interface attached to a VM is the primary network interface. VM 中的所有其他网络接口为辅助网络接口。All other network interfaces in the VM are secondary network interfaces.

  • 你可以控制要将出站流量发送到的网络接口。You can control which network interface you send outbound traffic to. 但是,VM 默认情况下会将所有出站流量都发送到分配给主网络接口的主 IP 配置的 IP 地址。However, a VM by default sends all outbound traffic to the IP address that's assigned to the primary IP configuration of the primary network interface.

  • 过去,同一个可用性集中的所有 VM 都需要有一个或多个网络接口。In the past, all VMs within the same availability set were required to have a single, or multiple, network interfaces. 现在,同一个可用性集中可以存在具有任意数目的网络接口的 VM,只要 VM 大小支持该数目。VMs with any number of network interfaces can now exist in the same availability set, up to the number supported by the VM size. 只能在创建 VM 时将其添加到可用性集。You can only add a VM to an availability set when it's created. 若要详细了解可用性集,请参阅在 Azure 中管理 VM 的可用性To learn more about availability sets, see Manage the availability of VMs in Azure.

  • 可以将同一 VM 中的网络接口连接到虚拟网络中的不同子网。You can connect network interfaces in the same VM to different subnets within a virtual network. 但是,网络接口必须全部连接到同一个虚拟网络。However, the network interfaces must all be connected to the same virtual network.

  • 可将任何主要或辅助网络接口的任何 IP 配置的任何 IP 地址添加到 Azure 负载均衡器后端池。You can add any IP address for any IP configuration of any primary or secondary network interface to an Azure Load Balancer back-end pool. 过去,只能将主要网络接口的主要 IP 地址添加到后端池。In the past, only the primary IP address for the primary network interface could be added to a back-end pool. 若要详细了解 IP 地址和配置,请参阅添加、更改或删除 IP 地址To learn more about IP addresses and configurations, see Add, change, or remove IP addresses.

  • 删除 VM 不会删除附加到它的网络接口。Deleting a VM doesn't delete the network interfaces that are attached to it. 删除 VM 时,网络接口将与该 VM 分离。When you delete a VM, the network interfaces are detached from the VM. 可将那些网络接口添加到不同的 VM,也可将其删除。You can add those network interfaces to different VMs or delete them.

  • 若要实现所记录的最佳性能,需要使用“加速网络”。Achieving the optimal performance documented requires Accelerated Networking. 在某些情况下,必须为 WindowsLinux 虚拟机显式启用“加速网络”。In some cases, you must explicitly enable Accelerated Networking for Windows or Linux virtual machines.

后续步骤Next steps

若要创建具有多个网络接口或 IP 地址的 VM,请参阅:To create a VM with multiple network interfaces or IP addresses, see:

任务Task 工具Tool
创建具有多个 NIC 的 VMCreate a VM with multiple NICs CLIPowerShellCLI, PowerShell
创建具有多个 IPv4 地址的单 NIC VMCreate a single NIC VM with multiple IPv4 addresses CLIPowerShellCLI, PowerShell
创建具有专用 IPv6 地址的单 NIC VM(在 Azure 负载均衡器后)Create a single NIC VM with a private IPv6 address (behind an Azure Load Balancer) CLIPowerShellAzure 资源管理器模板CLI, PowerShell, Azure Resource Manager template