$ run as a normal user# run as root (or with sudo)
Know it before you install
At boot, cloud-init finds a datasource (the cloud's metadata service, or a local "NoCloud" disk), reads meta data (instance ID, host name) and user data, and runs its modules in stages (network → config → final). Most modules run once per instance — a new instance ID means "first boot" again.
| Item | Value |
|---|---|
| Package | cloud-init (already in cloud images) |
| Config | /etc/cloud/cloud.cfg and /etc/cloud/cloud.cfg.d/*.cfg |
| Logs | /var/log/cloud-init.log (details), /var/log/cloud-init-output.log (command output) |
| State | /var/lib/cloud/ (instance data, which modules ran) |
| Tools | cloud-init status, cloud-init schema, cloud-init query, cloud-init clean |
| User data formats | #cloud-config (YAML), shell scripts (#!/bin/bash), multi-part |
Install (own images, RHEL and Ubuntu)
Cloud images already have it. For your own templates (VMware, Proxmox, KVM):
dnf -y install cloud-init cloud-utils-growpart
apt-get install -y cloud-init cloud-guest-utils
systemctl enable cloud-init-local cloud-init cloud-config cloud-final
cloud-init --versionFirst configuration: a solid #cloud-config
The first line must be #cloud-config:
#cloud-config
hostname: web1
fqdn: web1.corp.example.com
timezone: Asia/Riyadh
locale: en_US.UTF-8
users:
- name: ops
groups: [wheel]
shell: /bin/bash
sudo: "ALL=(ALL) NOPASSWD:ALL"
lock_passwd: true
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyOnly ops-laptop
ssh_pwauth: false
disable_root: true
package_update: true
package_upgrade: true
packages:
- chrony
- firewalld
- nginx
write_files:
- path: /etc/ssh/sshd_config.d/10-hardening.conf
permissions: "0600"
content: |
PermitRootLogin no
PasswordAuthentication no
MaxAuthTries 3
- path: /usr/share/nginx/html/index.html
content: |
<h1>web1 is ready</h1>
runcmd:
- systemctl enable --now chronyd firewalld nginx
- firewall-cmd --permanent --add-service=http
- firewall-cmd --reload
- systemctl restart sshd
final_message: "cloud-init finished after $UPTIME seconds"| Part | Why |
|---|---|
users with SSH keys, lock_passwd | Key-only login for a named admin user. |
ssh_pwauth: false, disable_root: true | No password login, no root login. |
package_update/upgrade, packages | A patched server with your packages from the first minute. |
write_files | Config files before services start. |
runcmd | Commands at the end of the first boot (runs once). |
(On Ubuntu the admin group is sudo and the time service chrony.)
Check the file before you use it:
cloud-init schema --config-file user-data.yaml --annotate
Valid schema user-data.yamlUse it
Give the file as user data:
aws ec2 run-instances --image-id ami-0abc… --instance-type t3.small --key-name ops --user-data file://user-data.yaml
az vm create -g rg-lab -n web1 --image Ubuntu2404 --custom-data user-data.yaml --generate-ssh-keys
gcloud compute instances create web1 --metadata-from-file user-data=user-data.yaml
multipass launch --name web1 --cloud-init user-data.yamlIn Terraform: user_data = file("user-data.yaml") (AWS) or custom_data = base64encode(file(...)) (Azure). For local VMs (KVM, Proxmox, VMware) use a NoCloud seed disk:
printf 'instance-id: web1-001\nlocal-hostname: web1\n' > meta-data
cloud-localds seed.iso user-data.yaml meta-dataAttach seed.iso as a CD-ROM to the VM (Proxmox can also set cloud-init settings in its UI).
Check the result on the VM
cloud-init status --wait --long
status: done
boot_status_code: enabled-by-generator
detail: DataSourceEc2Local
sudo cloud-init query userdata | head
sudo cloud-init query ds.meta_data.instance_id
sudo tail -n 30 /var/log/cloud-init-output.log
sudo grep -iE 'warn|error|fail' /var/log/cloud-init.log | tail
sudo cloud-init analyze show | tailReal uses at work:
- Every cloud VM: users, keys, packages and hardening at first boot — with Terraform.
- Join new servers to config management (install the Ansible/Salt/Puppet agent or call a playbook).
- VM templates in Proxmox/VMware that become unique servers when cloned.
Prepare golden images correctly
Before you save a VM as an image (Packer, a template), clean cloud-init so clones run it again and get new identities:
cloud-init clean --logs --seed
rm -f /etc/ssh/ssh_host_*
truncate -s 0 /etc/machine-idMake it safe
Security checklist:
- No secrets in user data — any process on the VM (and anyone with read access to the cloud instance data) can read it. Fetch secrets at runtime from a secret manager (Vault, AWS Secrets Manager) with the VM's role.
- Key-only SSH, no root login, a named admin user.
- Patch at first boot (
package_upgrade), or better: start from a fresh golden image. - Validate with
cloud-init schemain CI before use. - Clean images with
cloud-init cleanso clones get new host keys and machine IDs.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| User data was ignored | The first line is not #cloud-config, or YAML errors. | Add the header; cloud-init schema --config-file FILE. |
runcmd did not run after a change | Modules run once per instance. | It is a new instance only with a new instance ID; for tests: cloud-init clean --logs and reboot. |
status: error | A module failed (package not found, script error). | Read /var/log/cloud-init.log and cloud-init-output.log. |
| SSH key does not work | Wrong user or key format in users. | cloud-init query userdata; check the user's authorized_keys. |
No datasource found on local VMs | No metadata service and no seed disk. | Attach a NoCloud seed (cloud-localds) or configure the datasource. |
| Cloned VMs have the same host keys / machine ID | The template was not cleaned. | Run the clean steps before saving the image. |
Next steps
- Quick how-tos: Set up a new VM with cloud-init, Change the hostname (and keep it).
- Command page: cloud-init.
- At work: cloud-init — the first boot, Images and first boot.
- Together with: Terraform, Packer, Ansible, OpenSSH hardening.
1What must the first line of cloud-init YAML user data be?
#cloud-config.
2Why should you never put passwords or API keys in user data?
User data is readable on the VM (and in the cloud instance settings); fetch secrets at runtime instead.