Setting and querying guest attributes Stay organized with collections Save and categorize content based on your preferences.
Guest attributes are a specific type of custom metadata that your applicationscan write to while running on your virtual machine (VM) instance. Any applicationor user on your VM instance can both read and write data to these guestattribute metadata values.
Before you begin
- For Windows Server VMs, usePowerShell 3.0 or later. We recommend that you use
ctrl+vto paste the copied code blocks. - If you haven't already, set upauthentication. 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:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Required roles
The required permissions or roles depend on whether you are completing tasksfrom within or outside the VM.
Within the VM
If you are setting, querying or deleting the guest attributes from within the VM,you only need the roles and permissions for connecting to the VM.Any process running in the VM instance can write to the guestattributes values including scripts and applications that don't have sudo oradministrator level privileges.
For reads and writes from within an VM, the metadata server providesautomatic instance-level authentication and authorization. Each VM canread or write only to its own metadata server. Other VMs cannot access themetadata server of another VM.
Outside the VM
The following roles and permissions are needed to enable guest attributes,or view guest attributes from outside the VM by using the Google Cloud CLIor REST. You can't set or delete guest attributes from outside a VM.
To ensure that your user or service account has the necessary permissions to enable or view guest attributes, ask your administrator to grant your user or service account theCompute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM role on the VM or project. For more information about granting roles, seeManage access to projects, folders, and organizations.
This predefined role contains the permissions required to enable or view guest attributes. To see the exact permissions that are required, expand theRequired permissions section:
Required permissions
The following permissions are required to enable or view guest attributes:
- To enable guest attributes:
on the VM or projectcompute.instances.setMetadata - To view guest attributes:
on the VM or projectcompute.instances.getGuestAttributes
Your administrator might also be able to give your user or service account these permissions withcustom roles or otherpredefined roles.
When to use guest attributes
Caution: All applications and users on your VM can both read and write data toand from guest attribute metadata values. Users and service accounts fromoutside the VM with certain roles can read the guest attributes metadata values.Don't include sensitive information such as personally identifiable information(PII), or security data such as private keys or passwords in yourguest attributes. Guest attributes aren't designed for use cases that requireapplication or user authentication and authorization.Use guest attributes only for use cases that require small amounts of data thatdon't change frequently. The best use cases for guest attributes have thefollowing characteristics:
- The number of queries are limited to a maximum of 10 queriesper minute per VM instance.
- Queries don't exceed a burst of 3 queries per second. If this maximumrate is exceeded, Compute Engine might arbitrarily remove guestattributes that are in the process of being written. This data removal isneeded to ensure that other critical system data can be written to the server.
Guest attributes work well for situations where you must publish infrequent andlow volume data. For example, guest attributes work well for the following usecases:
- Startup scripts that can signal successful initialization by setting a customstatus value in guest attributes.
- Configuration management agents that can publish a guest OS name and versionto guest attributes.
- Inventory management agents that can publish list of packages installed inthe VM instance to guest attributes.
- Workload orchestration software that can signal completion of an operationin the guest to the software control plane by setting a custom status valuein guest attributes.
Guest attributes aren't a replacement for event streaming,Pub/Sub, or other forms of datastorage and configuration repositories.
Guest attributes and other Google Cloud services
Guest attributes are used by other Google Cloud services as follows:
- SSH security: If guest attributes is enabled and OS Login is disabled, theguest environment andgcloud CLI use guest attributes to improve SSH securityby using the Google API to retrieve host keys before you can connect to yourVM using SSH.
- VM Manager: TheOS config agent publishesoperating system data to guest attributes.
To review the metadata entries stored by these services, seePredefined guest attribute metadata keys.
Enable guest attributes on your VM
By default, guest attributes are disabled. To enable guest attributes, setthe necessary metadata values on either your individualVMs or in project-wide metadata:
Console
Setenable-guest-attributes in instance metadata when you create a VM:
In the Google Cloud console, go to theCreate an instance page.
Specify the VM details.
Expand theAdvanced options section, and do the following:
- Expand theManagement section.
In theMetadata section, clickAdd item add the following metadata entries:
- Key:
enable-guest-attributes - Value:
TRUE
- Key:
To create the VM, clickCreate.
Setenable-guest-attributes in project-wide metadata so that it applies toall of the VMs in your project:
In the Google Cloud console, go to theMetadata page.
ClickEdit.
Add a metadata entry where the key is
enable-guest-attributesand thevalue isTRUE. Alternatively, set the value toFALSEto disable thefeature.ClickSave to apply the changes.
Setenable-guest-attributes in metadata of an existing VM:
- In the Google Cloud console, go to theVM instances page.
- Click the name of the VM on which you want to set the metadatavalue.
- At the top of the instance details page, clickEdit to edit theinstance settings.
- UnderCustom metadata, add a metadata entry where the key is
enable-guest-attributesand the value isTRUE. Alternatively, set thevalue toFALSEto exclude the VM from the feature. - At the bottom of the instance details page, clickSave to apply yourchanges to the VM.
gcloud
Setenable-guest-attributes in instance metadata when you create an VM:
Use thegcloud compute instances createcommand in the Google Cloud CLI and setenable-guest-attributes=TRUE to enable guest attributes. ReplaceVM_NAME with the name of your VM.
gcloud compute instances createVM_NAME \ --metadata=enable-guest-attributes=TRUE
Setenable-guest-attributes in project-wide metadata so that itapplies to all of the VMs in your project:
Use theproject-info add-metadatacommand in the Google Cloud CLI and setenable-guest-attributes=TRUE to enable guest attributes:
gcloud compute project-info add-metadata \ --metadata=enable-guest-attributes=TRUE
Alternatively, you can setenable-guest-attributes toFALSE to disableguest attributes.
Setenable-guest-attributes in metadata of an existing VM:
Use theinstances add-metadatacommand in the Google Cloud CLI and setenable-guest-attributes=TRUE to enable guest attributes. ReplaceVM_NAME with the name of your VM.
gcloud compute instances add-metadataVM_NAME \ --metadata=enable-guest-attributes=TRUE
Alternatively, you can setenable-guest-attributes toFALSE toexclude your VM from using guest attributes.
Set guest attributes
Any process running in the VM instance can write to the guestattributes values including scripts and applications that don't have sudo oradministrator level privileges. Users or service accounts outside of theVM cannot write to guest attributes metadata values.
Linux VMs
For example, you might use acurl requestfrom within your VM to write a value to theguest-attributes metadatapath:
curl -X PUT --data "VALUE" http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY -H "Metadata-Flavor: Google"
Replace the following:
NAMESPACE: a logical grouping for yourKEY. Guest attributes must have a namespace.VALUE: the value that you want to write.KEY: the metadata path withinguest-attributeswherethe value is stored.
Use only letters, numerals, underscores (_), and hyphens (-) for theNAMESPACE andKEY fields.
Windows VMs
For example, you might use anInvoke-RestMethod requestfrom within your VM to write a value to theguest-attributes metadatapath:
PS C:\>$value = (Invoke-RestMethod ` -Method PUT -Body "VALUE" ` -Headers @{'Metadata-Flavor' = 'Google'} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY")$valueReplace the following:
NAMESPACE: a logical grouping for yourKEY. Guest attributes must have a namespace.VALUE: the value that you want to write.KEY: the metadata path withinguest-attributeswherethe value is stored.
Use only letters, numerals, underscores (_), and hyphens (-) for theNAMESPACE andKEY fields.
Get guest attributes
Users or service accounts can read guest attributes from outside a VM ifthey have therequired role.Alternatively, any user or application within the VM can readthe metadata values for that specific VM.
Any process running in the virtual machine can write to the guest attributesvalue, which include scripts and applications that don't have sudo oradministrator level privileges.
Query the metadata server
Use the following instructions to query guest attributes from within a VM.
Connect to the VM.
Query the guest attributes.
Linux VMs
For example, you might use a
curlrequestfrom within your VM to read a value from theguest-attributesmetadatapath:curl http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY -H "Metadata-Flavor: Google"
Replace the following:
NAMESPACE: the namespace for theguest-attributeskey that you want to query.KEY: the path withinguest-attributesfrom which youwant to read the metadata value.
Alternatively, you can return all of the guest attribute values in one request.Replace
NAMESPACEwith the namespace for theguest-attributeskey that you want to query.curl http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/ -H "Metadata-Flavor: Google"
Windows VMs
For example, you might use an
Invoke-RestMethodrequestfrom within your VM to read a value from theguest-attributesmetadatapath:PS C:\>$value = (Invoke-RestMethod ` -Headers @{'Metadata-Flavor' = 'Google'} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY")$valueReplace the following:
NAMESPACE: the namespace for theguest-attributeskey that you want to query.KEY: the path withinguest-attributesfrom which youwant to read the metadata value.
Alternatively, you can return all of the guest attribute values in one request.Replace
NAMESPACEwith the namespace for theguest-attributeskey that you want to query.PS C:\>$value = (Invoke-RestMethod ` -Headers @{'Metadata-Flavor' = 'Google'} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/")$value
Use Google Cloud CLI or REST
Use the following instructions to view guest attributes from outside a VM.
gcloud
Use the Google Cloud CLI to read guest attribute metadatavalues for a VM. For example, you can retrieve all of the valuesfor the VM as follows:
gcloud compute instances get-guest-attributesVM_NAME \ --zone=ZONE
To retrieve all of the values under a specific namespace, include the--query-path flag and the namespace that you defined:
gcloud compute instances get-guest-attributesVM_NAME \ --query-path=NAMESPACE \ --zone=ZONE
To retrieve all of the values under a specific namespace, include the--query-path flag, the namespace, and the key for the value that youdefined:
gcloud compute instances get-guest-attributesVM_NAME \ --query-path=NAMESPACE/KEY \ --zone=ZONE
Replace the following:
VM_NAME: the name of the VM fromwhich you want to read the guest attribute metadata valueNAMESPACE: the namespace for theguest-attributeskey that you want to queryKEY: the path withinguest-attributesmetadatawhere the value is storedZONE: the zone where the VM is located
REST
Use thecompute.instances.getguestattributes method:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/getGuestAttributes?queryPath=NAMESPACE/KEY
Replace the following:
PROJECT_ID: your project IDZONE: the zone where your VM is locatedVM_NAME: the name of the VM from whichyou want to read the guest attribute metadata valueNAMESPACE: the namespace for theguest-attributeskey that you want to queryKEY: the path withinguest-attributesmetadatawhere the value is stored
To retrieve all of the keys for aNAMESPACE, omit theKEY:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/getGuestAttributes?queryPath=NAMESPACE
To retrieve all of the keys in each namespace on the VM, omit theNAMESPACE:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/getGuestAttributes?queryPath=
Alternatively, if you have an OAuth token, you can usecurl:
curl -H "Authorization: BearerOAUTH_TOKEN" https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/getGuestAttributes?queryPath=NAMESPACE/KEY
Replace the following:
OAUTH_TOKEN: your OAuth tokenPROJECT_ID: your project IDZONE: the zone where your VM is locatedVM_NAME: the name of the VM from whichyou want to read the guest attribute metadata valueNAMESPACE: the namespace for theguest-attributeskey that you want to queryKEY: the path withinguest-attributesmetadatawhere the value is stored
Deleting guest attributes
Use the following instructions to remove guest attributes from within a VM.
Connect to the VM.
Delete the guest attributes.
Linux VMs
You can also delete guest attributes. For example, use
curlto delete aspecific key:curl -X DELETE http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY -H "Metadata-Flavor: Google"
Replace the following:
NAMESPACE: the namespace for theguest-attributeskey that you want to deleteKEY: the path withinguest-attributeswhere thevalue is stored
Windows VMs
You can also delete guest attributes. For example, use
Invoke-RestMethodto delete a specific key:PS C:\>$value = (Invoke-RestMethod ` -Method DELETE ` -Headers @{'Metadata-Flavor' = 'Google'} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/NAMESPACE/KEY")$valueReplace the following:
NAMESPACE: the namespace for theguest-attributeskey that you want to deleteKEY: the path withinguest-attributeswhere thevalue is stored
Disabling guest attributes on your organization or folder
If you don't want any of the VMs in your organization or folder toenable guest attributes, you can override and disable the feature completely.
Set theconstraints/compute.disableGuestAttributesAccess constraint on yourorganization or folder, replacingPROJECT_ID with thename of your project:
gcloud resource-manager org-policies enable-enforce \ constraints/compute.disableGuestAttributesAccess \ --project=PROJECT_ID
ReadUsing constraintsto learn more about how to set and manage constraints on your organizations.
Note: Disabling guest attributes on your organization or folder prevents you fromdeploying new apps or updating existing apps in App Engine flexible environment. To continue deploying apps in the flexible environment, you must disable the organizational policy constraintconstraints/compute.disableGuestAttributesAccess.What's next?
- Learn more aboutVM metadata.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.