默认情况下,每个计算实例至少有一个网络接口 (vNIC),用于确定实例使用的虚拟私有云网络。您可以创建具有多个网络接口的实例。除了使用 RDMA 网络配置文件创建的 VPC 网络,每个网络接口都必须分配到唯一 VPC 网络中的子网。网络接口的堆栈类型和子网的堆栈类型协同工作,如堆栈类型和 IP 地址中所述
每个计算实例都使用 DHCP 选项 121(如 RFC 3442 中所定义)接收默认路由。默认路由与 nic0
相关联。除非手动配置,否则从某个实例流向任何目的地(直接关联的子网除外)的所有流量都将使用 nic0
上的默认路由离开该实例。
在 Linux 系统上,您可以使用 /etc/iproute2/rt_tables
文件以及 ip rule
和 ip route
命令在客户机操作系统中配置自定义规则和路由。如需了解详情,请参阅客户机操作系统文档。如需查看示例,请参阅以下教程:为其他接口配置路由。
如果您不需要多个网络接口,请按照创建和启动实例的过程创建具有默认网络配置的实例。
准备工作
- 在创建计算实例之前,请验证是否已创建所有必需的网络。如果您需要创建网络或子网,请参阅创建和管理 VPC 网络。
- 验证每个网络是否具有适当的防火墙规则,以允许数据进出具有多个接口的实例。如果您需要创建防火墙规则,请参阅使用 VPC 防火墙规则。
-
如果您使用 IPv6 地址将实例连接到多个网络,请安装
google-guest-agent
版本 20220603.00 或更高版本。如需了解详情,请参阅我无法连接到次要接口的 IPv6 地址。 -
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Trusted Cloud by S3NS 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安装 Google Cloud CLI,然后使用联合身份登录 gcloud CLI。 登录后,运行以下命令来初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。
安装 Google Cloud CLI,然后使用联合身份登录 gcloud CLI。 登录后,运行以下命令来初始化 Google Cloud CLI:
gcloud init
如需了解详情,请参阅 Trusted Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获得使用特定子网创建实例所需的权限,请让您的管理员为您授予项目的 Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色包含使用特定子网创建实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
您需要具备以下权限才能使用特定子网创建实例:
-
针对项目的
compute.instances.create
权限 -
为实例指定子网:针对项目或所选子网的
compute.subnetworks.use
权限 -
在使用 VPC 网络时为实例分配外部 IP 地址:针对项目或所选子网的
compute.subnetworks.useExternalIp
权限
要求
在创建具有多个网络接口的实例之前,请先查看以下要求:
- 除了使用 RDMA 网络配置文件创建的 VPC 网络之外,还适用以下限制:如果您创建具有多个网络接口的实例,则每个接口都必须连接到不同的 VPC 网络。这些网络可以是独立的 VPC 网络,也可以是共享 VPC 网络。
- 每个网络接口都必须使用唯一的子网 IP 地址范围。
- 对于独立项目中的多 NIC 计算实例,每个网络接口都必须使用与实例位于同一项目中的子网。对于共享 VPC 宿主项目或服务项目中的实例,请参阅 Virtual Private Cloud 文档中的共享 VPC 。Private Service Connect 接口可让多 NIC 实例在不同项目的子网中拥有网络接口。如需了解详情,请参阅网络连接简介。
创建具有多个网络接口的虚拟机实例
大多数 Compute Engine 实例允许您在创建实例时配置多个 vNIC。
如需了解如何创建具有多个网络接口的计算实例,请参阅创建具有多个网络接口的虚拟机实例。
向现有实例添加网络接口
您只能在创建实例时为实例配置虚拟 NIC (vNIC)。不过,您可以向现有实例添加或移除动态网卡,而无需重启实例。
Dynamic NIC 是父级 vNIC 的子接口。配置动态网卡时,您需要指定一个 VLAN ID,用于为动态网卡标记网络流量:
- 动态网卡的 VLAN ID 必须是介于 2 到 255 之间的整数。
- 动态网卡的 VLAN ID 在父级 vNIC 中必须是唯一的。不过,属于不同父级 vNIC 的动态网卡可以使用相同的 VLAN ID。
Trusted Cloud by S3NS 使用以下格式来命名动态网卡:
PARENT_NIC_NUMBER.VLAN_ID
PARENT_NIC_NUMBER
是父 vNIC 的名称,例如nic0
。VLAN_ID
是您在配置动态网卡时指定的数字。动态网卡名称示例为nic0.4
。如需了解如何向实例添加动态网卡,请参阅向实例添加动态网卡。
后续步骤
- 了解如何使用 SSH 连接连接到您的实例。
- 使用内部 DNS 访问虚拟机
- 为虚拟机实例创建 PTR 记录
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Create VMs with multiple network interfaces\n\n*** ** * ** ***\n\nBy default, every compute instance has at least one network interface (vNIC)\nthat determines the Virtual Private Cloud network that the instance uses. You can\ncreate an instance with multiple network interfaces. Except for\nVPC networks that are created with an RDMA network profile, each\nnetwork interface must be assigned to a subnet in a unique VPC\nnetwork. The stack type of the network interface and the stack type of the\nsubnet work together as described in\n[Stack types and IP addresses](/vpc/docs/multiple-interfaces-concepts#stack-types)\n\nEach compute instance receives a default route using DHCP Option 121, as defined\nby [RFC 3442](https://datatracker.ietf.org/doc/html/rfc3442). The default route\nis associated with `nic0`. Unless manually configured otherwise, any traffic\nleaving an instance for any destination other than a directly connected subnet\nleaves the instance using the default route on `nic0`.\n\nOn Linux systems, you can configure custom rules and routes within the guest OS\nusing the `/etc/iproute2/rt_tables` file and the `ip rule` and `ip route`\ncommands. For more information, consult the guest OS documentation. For an\nexample, see the tutorial\n[Configure routing for an additional interface](/vpc/docs/configure-routing-additional-interface).\n\nIf you don't require multiple network interfaces, follow the procedure for\n[creating and starting an instance](/compute/docs/instances/create-start-instance)\nto create instances with the default network configuration.\n\nBefore you begin\n----------------\n\n- Verify that all required networks and subnets are created before you create the compute instance. If you need to create networks or subnets, see [Create and manage\n VPC networks](/vpc/docs/create-modify-vpc-networks).\n- Verify that each network has appropriate firewall rules that let data transfer to and from the instance that has multiple interfaces. If you need to create firewall rules, see [Use VPC firewall rules](/firewall/docs/using-firewalls).\n- If you are [connecting an\n instance to multiple networks by using IPv6 addresses](/vpc/docs/create-use-multiple-interfaces#example-dual), then install the `google-guest-agent` version [20220603.00](https://github.com/GoogleCloudPlatform/guest-agent/releases/tag/20220603.00) or later. For more information, see [I can't\n connect to a secondary interface's IPv6 address](/vpc/docs/troubleshoot-multiple-interfaces#connect-ipv6).\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n ### REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\n### Required roles\n\n\nTo get the permissions that\nyou need to create an instance with a specific subnet,\n\nask your administrator to grant you the\n\n\n[Compute Instance Admin (v1)](/iam/docs/roles-permissions/compute#compute.instanceAdmin.v1) (`roles/compute.instanceAdmin.v1`)\nIAM role on the project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nThis predefined role contains\n\nthe permissions required to create an instance with a specific subnet. To see the exact permissions that are\nrequired, expand the **Required permissions** section:\n\n\n#### Required permissions\n\nThe following permissions are required to create an instance with a specific subnet:\n\n- ` compute.instances.create` on the project\n- To specify a subnet for your instance: ` compute.subnetworks.use` on the project or on the chosen subnet\n- To assign an external IP address to the instance when using a VPC network: ` compute.subnetworks.useExternalIp` on the project or on the chosen subnet\n\n\nYou might also be able to get\nthese permissions\nwith [custom roles](/iam/docs/creating-custom-roles) or\nother [predefined roles](/iam/docs/roles-overview#predefined).\n\nRequirements\n------------\n\nBefore you create an instance with multiple network interfaces, review the\nfollowing requirements:\n\n- Except for VPC networks that are created with an [RDMA network profile](/vpc/docs/rdma-network-profiles), the following limitation applies: If you create an instance with multiple network interfaces, each interface must connect to a different VPC network. The networks can be standalone [VPC networks](/vpc/docs/vpc) or [Shared VPC networks](/vpc/docs/shared-vpc).\n- Each network interface must use a unique subnet IP address range.\n- For multi-NIC compute instances in standalone projects, each network interface must use a subnet located in the same project as the instance. For instances in Shared VPC host or service projects, see [Shared VPC](/vpc/docs/multiple-interfaces-concepts#multiple-network-interfaces-in-shared-vpc) in the Virtual Private Cloud documentation. [Private Service Connect interfaces](/vpc/docs/about-private-service-connect-interfaces) provide a way for a multi-NIC instance to have network interfaces in subnets in different projects. For more information, see [About network attachments](/vpc/docs/about-network-attachments).\n\nCreate VM instances with multiple network interfaces\n----------------------------------------------------\n\nMost Compute Engine instances let you configure multiple vNICs when\ncreating the instance.\n\n- For information about the number of network interfaces you can create for an instance, see [Maximum number of network interfaces](/vpc/docs/multiple-interfaces-concepts#max-interfaces).\n- You can also configure [Dynamic NICs](/vpc/docs/multiple-interfaces-concepts#nic-types) when creating an instance.\n\nFor information about how to create a compute instance with multiple network\ninterfaces, see\n[Create VM instances with multiple network interfaces](/vpc/docs/create-use-multiple-interfaces#creating_virtual_machine_instances_with_multiple_network_interfaces).\n\nAdd network interfaces to an existing instance\n----------------------------------------------\n\nYou can configure virtual NICs (vNICs) for an instance only when creating the\ninstance. However, you can add or remove\n[Dynamic NICs](/vpc/docs/multiple-interfaces-concepts#nic-types)\nto an existing instance, and you don't have to restart the instance.\n\nDynamic NICs are subinterfaces of a parent vNIC.\nWhen you configure a Dynamic NIC, you specify\na VLAN ID that is used to tag network traffic for the\nDynamic NIC:\n\n- The VLAN ID of a Dynamic NIC must be an integer from 2 to 255.\n- The VLAN ID of a Dynamic NIC must be unique within a parent vNIC. However, Dynamic NICs that belong to different parent vNICs can use the same VLAN ID.\n\nGoogle Cloud uses the following format for the name of a\nDynamic NIC:\n\u003cvar translate=\"no\"\u003ePARENT_NIC_NUMBER\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eVLAN_ID\u003c/var\u003e\n\n\u003cvar translate=\"no\"\u003ePARENT_NIC_NUMBER\u003c/var\u003e is the name of the parent vNIC, for example\n`nic0`. The \u003cvar translate=\"no\"\u003eVLAN_ID\u003c/var\u003e is the number you specified when\nconfiguring the Dynamic NIC. An example\nDynamic NIC name is `nic0.4`.\n\nFor information about how to add Dynamic NICs to an\ninstance, see\n[Add Dynamic NICs to an instance](/vpc/docs/add-dynamic-nics).\n\nWhat's next?\n------------\n\n- Learn how to use [SSH connections](/compute/docs/instances/ssh) to connect to your instance.\n- [Access VMs using internal DNS](/compute/docs/networking/using-internal-dns)\n- [Create a PTR record for a VM instance](/compute/docs/instances/create-ptr-record)"]] -