Add Dynamic NICs to an instance
This page describes how to add a Dynamic Network Interface (NIC) to an existing VM instance.For information about creating new instances, see Create VMs with multiple network interfaces.
Before you begin
Before adding Dynamic NICs to an instance, do the following:
- Make sure you are familiar with the properties and limitations of Dynamic NICs as described in the multiple network interfaces overview.
- If necessary, view your instance's existing network interfaces by following the instructions in View network interfaces for an instance.
Add a Dynamic NIC
This section describes how to add a Dynamic NIC to an instance.
When you add a Dynamic NIC, you might observe a propagation delay. This delay is typically a few seconds, but it can be, in rare cases, up to one minute.
gcloud
To add a Dynamic NIC to an existing instance, use the
gcloud beta compute instances network-interfaces add
command.
gcloud beta compute instances network-interfaces add INSTANCE_NAME \ --zone=ZONE \ --vlan=VLAN_ID \ --parent-nic-name=PARENT_VNIC_NAME \ --network=NETWORK \ --subnetwork=SUBNET
Replace the following:
INSTANCE_NAME
: the name of the instance to add the Dynamic NIC toZONE
: the zone of the instanceVLAN_ID
: the VLAN ID of the Dynamic NICPARENT_VNIC_NAME
: the name of the parent vNIC, such asnic0
NETWORK
: the network where the Dynamic NIC attachesSUBNET
: the subnet where the Dynamic NIC attaches
Alternatively, you can add a Dynamic NIC to an instance when updating instance properties. For more information, see Update instance properties.
To add Dynamic NICs to a managed instance group (MIG), see
the gcloud beta compute instance-groups managed set-instance-template
command.
API
To add a Dynamic NIC to an existing instance, use the
instances.addNetworkInterface
method.
POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/addNetworkInterface { "subnetwork": "regions/REGION/subnetworks/SUBNET", "parentNicName": "PARENT_VNIC_NAME", "vlan": "VLAN_ID" }
Replace the following:
PROJECT_ID
: the ID of the project that contains the instanceZONE
: the zone that contains the instanceINSTANCE_NAME
: the name of the instance to add a Dynamic NIC toREGION
: the region that contains the instanceSUBNET
: the subnet where the Dynamic NIC attachesPARENT_VNIC_NAME
: the name of the parent vNIC, such asnic0
VLAN_ID
: the VLAN ID of the Dynamic NIC
Alternatively, you can add a Dynamic NIC to an instance when updating instance properties. For more information, see Update instance properties.
To add Dynamic NICs to a managed instance group (MIG), see
the instanceGroupManagers.setInstanceTemplate
method.
Configure the guest OS for Dynamic NICs
This section describes how to complete the additional guest OS configuration that is required when you add Dynamic NICs to a new or existing instance.
Choose one of the following configuration options:
Configuration option | Description |
---|---|
Configure automatic management of Dynamic NICs (Recommended) | Use the Google guest agent for the automatic management of Dynamic NICs. The guest environment includes the Google guest agent, which automates the installation and management of VLAN interfaces in the root namespace on Linux distributions of Google Cloud OS images. This functionality is supported in Google guest agent version 20250204.02 and later. |
Configure the guest OS manually | Run commands in the guest OS to install the Dynamic NICs. If you use this option, your configuration doesn't automatically persist after a restart of the instance. To cause the configurations to persist after a restart, use the recommended method for configuring persistent network interfaces for your Linux distribution. For example, see NetworkConfiguration for Debian and interfaces for Ubuntu. |
Configure automatic management of Dynamic NICs
This section describes how to configure the automatic management of Dynamic NICs by using the Google guest agent.
You only need to complete these steps once per instance. If you added a Dynamic NIC to an instance for which you already completed these steps, then you don't need to repeat them.
To configure the automatic management of Dynamic NICs:
Connect to the instance by using SSH.
Ensure that the instance is running the Google guest agent at version 20250204.02 or later:
To determine whether the instance is running the Google guest agent, run the appropriate command listed in Installed packages for the guest environment.
If your instance isn't running the Google guest agent, install the guest environment.
If your instance is running the Google guest agent, update the guest environment.
Installing or updating the guest environment ensures that you have the latest version of the Google guest agent. You can confirm the version by following the steps in Validating the guest environment.
Configure the Google guest agent to manage Dynamic NICs:
Open or create your guest environment configuration file.
edit /etc/default/instance_configs.cfg
Add the following to the configuration file, then save your changes and exit the editor.
[NetworkInterfaces] vlan_setup_enabled = true manage_primary_nic = true
For information about these settings, see the following:
vlan_setup_enabled = true
: configures the Google guest agent to install and manage Dynamic NICsmanage_primary_nic = true
: an additional setting required for the Google guest agent to manage Dynamic NICs created undernic0
For more information about editing the guest agent configuration file, see Configuration in the Google guest agent documentation.
Restart the guest agent by running the following command. Alternatively, you can restart the instance.
sudo systemctl restart google-guest-agent.service
List the network interfaces on your instance to see that the Google guest agent installed your Dynamic NICs.
For example, the following command displays a name such as
a-gcp.ens4.11
for a Dynamic NIC with a VLAN ID of11
created as a child of theens4
interface.sudo ip -d addr
Configure the guest OS manually
To configure Dynamic NICs in the guest OS of an instance manually:
Connect to the instance by using SSH.
If the instance is running the Google guest agent, disable the automatic management of Dynamic NICs. You can determine whether the instance is running the Google guest agent by using the appropriate command listed in Installed packages for the guest environment.
To disable the automatic management of Dynamic NICs:
Open or create your guest environment configuration file.
edit /etc/default/instance_configs.cfg
Add the following to the configuration file, then save your changes and exit the editor.
[NetworkInterfaces] vlan_setup_enabled = false manage_primary_nic = false
For more information about editing the guest agent configuration file, see Configuration in the Google guest agent documentation.
Restart the guest agent by running the following command. Alternatively, you can restart the instance.
sudo systemctl restart google-guest-agent.service
For each Dynamic NIC that you created, do the following:
Get the MAC address of the parent vNIC. To find the MAC address, query the metadata server by running the following command:
curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/VNIC_NUMBER/mac -H "Metadata-Flavor: Google"
Replace
VNIC_NUMBER
with the number of the parent vNIC. For example,0
fornic0
or1
fornic1
.Configure a subinterface in the guest OS of the instance. For example, on a Debian VM, run the following commands:
sudo modprobe 8021q sudo ip link add link VNIC_NAME name VNIC_NAME.VLAN_ID type vlan id VLAN_ID sudo ip addr add IP_ADDRESS dev VNIC_NAME.VLAN_ID sudo ip link set dev VNIC_NAME.VLAN_ID address MAC_ADDRESS sudo ip link set up VNIC_NAME.VLAN_ID
Replace the following:
VNIC_NAME
: the name of the parent vNIC assigned by the operating system, such aseth1
orens4
IP_ADDRESS
: the IP address of the Dynamic NIC that you createdVLAN_ID
: the VLAN ID that you assigned to the Dynamic NICMAC_ADDRESS
: the MAC address that you got in the previous step