[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eStartup scripts in a virtual machine (VM) are used to perform tasks during the VM's start-up process, and can be either bash or non-bash scripts by including the \u003ccode\u003e#!/bin/...\u003c/code\u003e script interpreter indicator.\u003c/p\u003e\n"],["\u003cp\u003eStartup scripts can be defined as clear text for scripts up to 2048 bytes, or as a Kubernetes secret for larger scripts, which requires matching \u003ccode\u003escriptSecretRef\u003c/code\u003e names with the \u003ccode\u003emetadata.name\u003c/code\u003e field within the Kubernetes secret.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize startup scripts, the \u003ccode\u003estartupScripts\u003c/code\u003e field must be added to the VM's \u003ccode\u003espec\u003c/code\u003e field, allowing for the specification of multiple scripts as either clear text or Kubernetes secrets.\u003c/p\u003e\n"],["\u003cp\u003eTo create a VM with a startup script, you must have the Project VirtualMachine Admin role, create the script (as clear text or a Kubernetes secret), and then add it to the \u003ccode\u003espec\u003c/code\u003e field when creating or updating the VM.\u003c/p\u003e\n"],["\u003cp\u003eStartup script logs can be viewed inside the guest VM by running the \u003ccode\u003esudo journalctl -u cloud-final\u003c/code\u003e command.\u003c/p\u003e\n"]]],[],null,["# Use a startup script on Linux VMs\n\nA startup script performs tasks during the start-up process for a virtual machine\n(VM). This page provides you with the steps to follow for using startup scripts\non VM instances.\n\n\nBefore you begin\n----------------\n\nTo use `gdcloud` command-line interface (CLI) commands, ensure that you have downloaded, installed,\nand configured the [`gdcloud` CLI](/distributed-cloud/hosted/docs/latest/gdch/resources/gdcloud-overview).\nAll commands for Distributed Cloud use the `gdcloud` or\n`kubectl` CLI, and require an operating system (OS) environment.\n\n### Get the kubeconfig file path\n\nTo run commands against the Management API server, ensure you have the following\nresources:\n\n1. [Sign in and generate](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/iam/sign-in#cli) the\n kubeconfig file for the Management API server if you don't have one.\n\n2. Use the path to the kubeconfig file of the Management API server to replace\n \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e in these instructions.\n\n### Request permissions and access\n\nTo perform the tasks listed in this page, you must have the Project\nVirtualMachine Admin role. Follow the steps to either\n[verify](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation#verify-user-access)\nyour access or have your Project IAM Admin\n[assign](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation)\nyou the Project VirtualMachine Admin (`project-vm-admin`) role in the namespace\nof the project where the VM resides.\n\nPass a startup script\n---------------------\n\nYou can use either bash or non-bash scripts as startup scripts. To do so,\ninclude `#!/bin/...` at the beginning of the script to indicate the script interpreter.\nFor example, to use a Python 3 startup script, add `#! /usr/bin/python3` to the\nbeginning of the script.\n\nGoogle Distributed Cloud (GDC) air-gapped runs startup scripts in alphabetical order,\nbased on the name of each startup script.\n\nThe following table shows the script format to use based on your script size:\n\n### Define a startup script\n\nTo use a startup script you must add the `startupScripts` field to the VM `spec`\nfield. Within this field you can specify multiple startup scripts as clear\ntext or as a Kubernetes secret.\n\nThe following example specifies the startup scripts as clear text and a\nKubernetes secret: \n\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \"my-vm\"\n spec:\n ...\n startupScripts:\n - name: hello-world\n script: |\n #!/bin/bash\n echo hello\n - name: add-user\n scriptSecretRef:\n name: add-user\n ---\n\n apiVersion: v1\n kind: Secret\n type: Opaque\n metadata:\n name: add-user\n data: \n script:\n IyEvYmluL2Jhc2gKYWRkdXNlciB1c2VyCg==\n\nReview the following considerations:\n\n- The startup script runs on *every boot*.\n- The startup script has default root privileges.\n- In the Kubernetes secret, the name of the `scriptSecretRef` in the VM `spec` must match the `metadata.name` field.\n- In the Kubernetes secret, specify the startup script content by adding a key `script` to the `data` field.\n\nCreate a new VM with a startup script\n-------------------------------------\n\nThese instructions apply regardless of what you use as the image to create your\nVM. Complete the following:\n\n1. To create a startup script as a Kubernetes secret, run:\n\n cat \u003c\u003cEOF \u003e\u003e\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n \u003cvar translate=\"no\"\u003eSTARTUP_SCRIPT_CONTENT\u003c/var\u003e\n EOF\n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e create secret -n \u003cvar translate=\"no\"\u003ePROJECT\u003c/var\u003e generic \u003cvar translate=\"no\"\u003eSECRET_NAME\u003c/var\u003e --from-file=script=\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n\n rm \u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e\n\n2. Reference the steps to create a VM as described on the\n [create a VM](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/create-a-vm/create-and-start) page.\n At [step one](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/create-a-vm/create-and-start#create-vm-from-provided-image) add the startup script or scripts to the `spec` field before you run the command to\n create a VM.\n\n This example defines a startup script with both clear text and a Kubernetes\n secret: \n\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eVM_NAME\u003c/span\u003e\u003c/var\u003e\n namespace: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003ePROJECT\u003c/span\u003e\u003c/var\u003e\n spec:\n ...\n startupScripts:\n - name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eCLEAR_TEXT_SCRIPT_NAME\u003c/span\u003e\u003c/var\u003e\n script: |\n #!/bin/bash\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-no\"\u003eCLEAR_TEXT_SCRIPT\u003c/span\u003e\u003c/var\u003e\n - name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eSECRET_SCRIPT_NAME\u003c/span\u003e\u003c/var\u003e\n scriptSecretRef:\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eSECRET_NAME\u003c/span\u003e\u003c/var\u003e\n\n These variables are defined as follows:\n\n3. Proceed with the following steps to create a VM.\n\n The following is an example of how to create a VM with startup scripts\n that adds a new user using a Kubernetes secret and clear text. \n\n kubectl --kubeconfig \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e \\\n apply -n \u003cvar translate=\"no\"\u003ePROJECT\u003c/var\u003e -f - \u003c\u003cEOF\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachineDisk\n metadata:\n name: \u003cvar translate=\"no\"\u003eVM_BOOT_DISK_NAME\u003c/var\u003e\n spec:\n source:\n image:\n name: \u003cvar translate=\"no\"\u003eBOOT_DISK_IMAGE_NAME\u003c/var\u003e\n namespace: vm-system\n size: \u003cvar translate=\"no\"\u003eBOOT_DISK_SIZE\u003c/var\u003e\n ---\n apiVersion: v1\n kind: Secret\n type: Opaque\n metadata:\n name: add-user\n data: \n script:\n IyEvYmluL2Jhc2gKYWRkdXNlciB1c2VyCg==\n ---\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e\n spec:\n compute:\n virtualMachineType: \u003cvar translate=\"no\"\u003eMACHINE_TYPE\u003c/var\u003e\n disks:\n - virtualMachineDiskRef:\n name: \u003cvar translate=\"no\"\u003eVM_BOOT_DISK_NAME\u003c/var\u003e\n boot: true\n autoDelete: \u003cvar translate=\"no\"\u003eBOOT_DISK_AUTO_DELETE\u003c/var\u003e\n startupScripts:\n - name: add-user\n scriptSecretRef:\n name: add-user\n - name: add-to-sudoers\n script: |\n #!/bin/bash\n usermod -aG sudo user\n EOF\n\n In the example the variables are defined as follows.\n\nUpdate an existing VM with a startup script\n-------------------------------------------\n\nYou can also update an existing VM with a startup script. The VM must be shut\ndown before you perform the update.\n\nFollow the steps to [update VM properties](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/manage-vms/operations-and-lifecycle/update-vm-properties#update-properties)\nand update the `spec` field with the startup script that you would like to run.\n\nView the output of a startup script\n-----------------------------------\n\n1. Follow the steps to [connect to a VM](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/connect-to-vm/connect-to-a-vm).\n2. Run the following command inside the guest VM to get the logs of the\n startup script that you ran:\n\n sudo journalctl -u cloud-final\n\n The startup script logs begin with the following: \n\n Started to run the command: /var/lib/google/startup-scripts/\u003cscript-name\u003e ..."]]