Home / Azure

AzureRunbookBeginner

Cloud-init custom data didn't run on an Azure Linux VM: check, validate and rerun

The VM deployed but packages are missing, files weren't written or runcmd never ran. Read the cloud-init status and logs, find the failing module, fix the config and rerun safely.

Deepak Bhardwaj7 min read

Applies toAzure Linux VMs and scale sets using cloud-init (for example Ubuntu, RHEL, Azure Linux), Azure CLI 2.x. cloud-init subcommands vary by version; run cloud-init --help on the image.

The VM shows Succeeded in the portal, but the nginx package isn't there, /etc/myapp.conf was never written, or the runcmd that joins the VM to your config tool never ran. On Azure, the --custom-data you pass at creation is handed to the provisioning agent on first boot. If something went wrong, the answer is almost always in two log files on the VM.

The examples use resource group rg-app and VM vm-app01.

Quick map: symptom to cause

Symptom Likely cause Fix
Nothing in the config ran at all Missing #cloud-config header, or the image provisions with the Linux Agent, not cloud-init Add the header; use an image that supports cloud-init
Some modules ran, others didn't YAML error (tabs, bad indentation) dropped part of the config Validate with cloud-init schema
packages failed, runcmd ran No outbound internet or package repo access from the subnet Add egress (NAT Gateway, firewall rule, proxy)
runcmd script failed partway Script error, wrong path, CRLF line endings from Windows Check cloud-init-output.log, convert to LF
Deployment fails with OSProvisioningTimedOut Agent didn't report ready, often a broken custom image or failed early boot Check serial log and boot diagnostics
You changed custom data and nothing happened cloud-init runs per instance, on first boot Rebuild, or clean and rerun (see warning)

Step 1: check status without logging in

Run Command works even when SSH is blocked, as long as the VM agent is healthy:

az vm run-command invoke -g rg-app -n vm-app01 \
  --command-id RunShellScript \
  --scripts "cloud-init status --long; echo ---; tail -n 40 /var/log/cloud-init-output.log"

cloud-init status --long returns status: done, status: running or status: error, plus the error text from the failing stage when there is one. On recent versions status: done can still list "recoverable errors", which usually means one module failed and the others carried on.

If the VM never finished booting, read the serial output instead:

az vm boot-diagnostics get-boot-log -g rg-app -n vm-app01 | grep -i -E "cloud-init|error" | tail -n 50

Boot diagnostics must be enabled on the VM for this to return anything (az vm boot-diagnostics enable -g rg-app -n vm-app01).

Step 2: read the right log

File What it holds
/var/log/cloud-init-output.log stdout/stderr of your runcmd, package installs and scripts. Start here
/var/log/cloud-init.log cloud-init's own debug log: data source, module start/finish, tracebacks
/var/log/waagent.log Azure Linux Agent: provisioning handshake, extensions
/var/lib/cloud/instance/user-data.txt The config cloud-init actually received

Useful greps on the VM:

sudo grep -n -E "WARNING|ERROR|Traceback|Failed" /var/log/cloud-init.log | tail -n 40
sudo grep -n -i "running module" /var/log/cloud-init.log | tail -n 30

cloud-init analyze show lists each boot stage and module with its timing. It's a quick way to see which module ran last before a hang.

Tip

Always check /var/lib/cloud/instance/user-data.txt first. If it isn't what you meant to send (empty, base64-encoded twice, or a different file), the problem is in your pipeline or template, not on the VM.

Step 3: confirm the image provisions with cloud-init

Not every image uses cloud-init. Some older or custom images provision with the Azure Linux Agent alone. By default, the Linux Agent doesn't process custom data. Check:

grep -i "^Provisioning.Agent" /etc/waagent.conf
systemctl list-unit-files | grep cloud-init

Provisioning.Agent=auto or cloud-init, with the cloud-init units present, means cloud-init should run. If cloud-init isn't installed or the agent is set to waagent, use a marketplace image that supports cloud-init, or rebuild your custom image with cloud-init enabled. Microsoft lists supported distributions in the cloud-init on Azure documentation.

Step 4: validate the config before you redeploy

Most "it didn't run" cases are YAML problems. Validate locally or on any Linux box with cloud-init installed:

cloud-init schema --config-file cloud-init.yaml --annotate

Older cloud-init versions use cloud-init devel schema --config-file. On the affected VM, recent versions can check what they received with sudo cloud-init schema --system.

A minimal config that's easy to verify:

#cloud-config
package_update: true
packages:
  - nginx
write_files:
  - path: /etc/myapp/app.conf
    permissions: '0640'
    owner: root:root
    content: |
      env=prod
runcmd:
  - systemctl enable --now nginx
  - echo "cloud-init finished $(date -Is)" >> /var/log/myapp-bootstrap.log

Rules that catch people out:

  • The first line must be exactly #cloud-config (or #!/bin/bash for a plain script). A blank line or BOM before it changes how the file is handled.
  • Use spaces, never tabs.
  • Save with LF line endings. Files written on Windows with CRLF break shell scripts in runcmd and in #! scripts.
  • write_files runs before packages, so owner: for a user created by a package won't exist yet. Use root and fix ownership in runcmd, or use the users module.

Then deploy:

az vm create -g rg-app -n vm-app02 \
  --image Ubuntu2204 \
  --custom-data cloud-init.yaml \
  --admin-username azureuser --generate-ssh-keys \
  --vnet-name vnet-app --subnet snet-app --public-ip-address ""

The CLI base64-encodes the file for you. In Bicep, ARM or Terraform, check whether the field expects plain text or base64. Encoding it twice is a common reason user-data.txt contains garbage.

Warning

Custom data isn't secret. Anything in it can be read on the VM by root and through the deployment history. Pull secrets from Key Vault at runtime with the VM's managed identity instead.

Step 5: packages failed? Check egress

If cloud-init-output.log shows apt or dnf timeouts, the VM can't reach its package repositories. Check from the VM:

az vm run-command invoke -g rg-app -n vm-app01 --command-id RunShellScript \
  --scripts "curl -sS -m 10 -o /dev/null -w '%{http_code}\n' https://azure.archive.ubuntu.com/ubuntu/ || echo no-egress"

Private subnets, a forced tunnel to a firewall without the repository FQDNs allowed, or a missing proxy setting are the usual causes. Fix egress first, then rerun.

Step 6: rerun cloud-init (test VMs only)

cloud-init records that it has run for this instance and skips the per-instance modules on later boots. To force a full rerun:

sudo cloud-init clean --logs
sudo reboot

Caution

cloud-init clean makes the next boot behave like a first boot. Depending on your config and image, that can regenerate SSH host keys, reset users and passwords, rewrite network config and run every runcmd again. Don't do this on production VMs. Fix the config and replace the VM, or for scale sets update the model and reimage the instances.

To rerun only one module while debugging, use cloud-init single, for example sudo cloud-init single --name write_files --frequency always. Module names vary by version; take them from cloud-init.log.

Custom data on a single VM is set when you create it. Changing your template doesn't change what an existing VM received, so a rebuild is the clean fix.

Prevent it next time

  • Run cloud-init schema on every config in CI before any deployment.
  • End runcmd with a marker line (as above) and alert or gate on it in your pipeline using Run Command.
  • Keep secrets out of custom data; use managed identity and Key Vault.
  • Make sure build subnets have tested egress to package repositories, or use an internal mirror.
  • For anything heavier than a few packages and files, bake a golden image (Azure Image Builder or Packer) and keep cloud-init for small per-instance settings.