What Is cloud-init? Configure a Linux VM Automatically on First Boot
cloud-init is open-source provisioning software that applies initial settings to a new Linux virtual machine (VM) using instance metadata and user data. The image must include cloud-init configured for that cloud’s datasource. At VM creation, you can pass `#cloud-config` YAML or a script to configure users, SSH keys, packages, files, and commands. Because images, agents, and execution timing vary by distribution and cloud, passing user data alone does not prove that setup fin
cloud-init is open-source provisioning software that applies initial settings to a new Linux virtual machine (VM) using instance metadata and user data. The image must include cloud-init configured for that cloud’s datasource. At VM creation, you can pass #cloud-config YAML or a script to configure users, SSH keys, packages, files, and commands. Because images, agents, and execution timing vary by distribution and cloud, passing user data alone does not prove that setup finished or succeeded.
What does cloud-init receive, and how does it process it?
Cloud platforms have metadata for a specific instance, such as its name, network, and SSH public keys. A cloud-init datasource detects or knows which platform is running and retrieves its metadata or user data. Even when the same cloud-init format is used, the image needs the right datasource; an Azure image, for example, must be configured for Azure.
At boot, cloud-init runs through five ordered stages: platform detection, local datasource discovery, network setup, configuration, and final tasks. For example, a command that tries to reach a remote package repository before networking is ready can fail. Work that can happen later, such as installing a service, can go in runcmd, which is run in the final stage.
User data has multiple formats. A header often tells cloud-init how to interpret the input.
| Format | Header | Typical use |
|---|---|---|
| cloud-config | #cloud-config | Declare packages, users, files, and module settings in YAML |
| Shell script | #!/bin/bash or another interpreter | Run commands in order |
| MIME multipart | MIME boundaries and content types | Combine cloud-config, scripts, and other parts |
If the header is missing or YAML syntax/indentation is wrong, the intended task may not run. Treat #cloud-config as a format marker, not an ordinary comment.
How does it differ from user data or startup scripts?
cloud-init is provisioning software inside a Linux image. User data or custom data is input that a platform passes to a VM. The agent that processes similar-looking input depends on the image and cloud.
| Mechanism | Input and processing | Important difference |
|---|---|---|
| cloud-init user data | cloud-init parses supported formats and runs modules | The image needs cloud-init enabled with the correct datasource |
| AWS EC2 Linux user data | EC2 passes input; cloud-init or another image agent processes it | By default it runs during the first launch boot cycle; repeat runs need separate configuration |
| Azure Linux custom data | Provision-time input processed by cloud-init or the Linux Agent (WALA) | Check which agent provisions the image. cloud-init can signal VM readiness before user configuration finishes |
| Google Compute Engine startup script | A metadata-driven startup-script handler runs the script | This is a separate feature, not cloud-init itself; Google documents its own boot execution behavior |
On Azure, WALA handles Azure extensions; cloud-init does not process them. Both tools can be present in an image, so check how provisioning roles are configured. Azure custom data and user data are also separate features: Microsoft describes custom data as a provisioning-time payload and user data as a distinct value that can be retrieved from the metadata service and updated without stopping or reimaging a VM.
How do you write first-boot configuration?
The example below assumes an Ubuntu-family image with cloud-init enabled and access to its package repository. Other Linux distributions may use different package, path, or service names.
#cloud-config
packages:
- nginx
write_files:
- path: /var/www/html/index.html
owner: root:root
permissions: '0644'
content: |
<h1>Server configured with cloud-init</h1>
runcmd:
- [systemctl, enable, --now, nginx]packages declares a package to install, write_files creates a file on the VM, and runcmd schedules a command for the final stage. To check the page from outside the VM, the network path and firewall rules must also allow it; this config does not make the server public by itself.
Before using a config in production:
Confirm that the target image contains cloud-init and is configured for the cloud datasource. Azure Marketplace support can vary by distribution and SKU.
Save the config and run `cloud-init schema -c cloud-init.yml --annotate` in an environment with cloud-init installed. This checks schema and supported keys; it does not prove cloud delivery or command success.
Create a test VM in an isolated environment and pass the config as startup data. After boot, use `cloud-init status --wait` to wait for processing to finish.
Review errors with `cloud-init status --long`, then inspect `/var/log/cloud-init.log` and `/var/log/cloud-init-output.log` for the failed module and command output. Check the application separately.
Create a second fresh test instance to confirm the image and config reproduce the expected result before widening rollout.
What can fail, and how should you recover?
Common problems include invalid YAML, a missing format header, an unsupported module, unavailable network access to package repositories, or an unexpected datasource or provisioning agent. Overriding early network setup or running a command that never finishes can delay VM initialization. Some platforms may leave the VM creation successful even when a user configuration failed, so distinguish “VM is running” from “initialization succeeded.”
runcmd and some modules are designed to run once per instance. Forcing cloud-init to run again can repeat account creation, disk changes, or service restarts. If a task must repeat, make it idempotent or use a service manager, configuration-management tool, or deployment pipeline designed for repeat runs. Rerunning cloud-init can change instance state; inspect logs and distribution documentation, and try it in a test environment first.
Do not put passwords, API tokens, or private keys in user data. Cloud providers differ in how instance data can be retrieved and retained. Fetch credentials at runtime from an approved secret manager using a least-privilege service identity, and ensure values do not remain in scripts, logs, or images.
Sources
[cloud-init: Boot stages](https://docs.cloud-init.io/en/latest/explanation/boot.html)
[cloud-init: User-data formats](https://docs.cloud-init.io/en/latest/explanation/format/index.html)
[cloud-init: Validate user data](https://docs.cloud-init.io/en/latest/howto/debug_user_data.html)
[cloud-init: Wait for processing to finish](https://docs.cloud-init.io/en/latest/howto/wait_for_cloud_init.html)
[cloud-init: Debugging](https://docs.cloud-init.io/en/latest/howto/debugging.html)
[AWS EC2: Run commands with Linux user data](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)
[Microsoft Learn: cloud-init support for Azure Linux VMs](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init)
[Microsoft Learn: Custom data on Azure VMs](https://learn.microsoft.com/en-us/azure/virtual-machines/custom-data)
[Google Cloud: Startup scripts on Linux VMs](https://docs.cloud.google.com/compute/docs/instances/startup-scripts/linux)
Based on official cloud-init and cloud-provider documentation reviewed on November 19, 2026. Behavior varies by cloud-init version, Linux image, datasource, and platform agent; verify on the target image and environment before production use.