Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 95cloud-init · Cloud

Use cloud-init (first-boot setup of VMs)

cloud-init sets up a new VM at its first boot: users and SSH keys, host name, packages, files, time zone, disks and any commands you want — from a small YAML file called user data. It runs on almost every cloud image (AWS, Azure, Google Cloud, OpenStack, Proxmox, VMware, Multipass) and is how Terraform and other tools prepare servers. This guide shows how cloud-init works, writes a solid #cloud-config, tests it locally, checks the result on the VM, and prepares images correctly.

  • 10 Lessons
  • 13 Commands

$ run as a normal user# run as root (or with sudo)

95.1

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.

ItemValue
Packagecloud-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)
Toolscloud-init status, cloud-init schema, cloud-init query, cloud-init clean
User data formats#cloud-config (YAML), shell scripts (#!/bin/bash), multi-part

95.2

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 --version

95.3

First configuration: a solid #cloud-config

The first line must be #cloud-config:

user-data.yaml
#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"
PartWhy
users with SSH keys, lock_passwdKey-only login for a named admin user.
ssh_pwauth: false, disable_root: trueNo password login, no root login.
package_update/upgrade, packagesA patched server with your packages from the first minute.
write_filesConfig files before services start.
runcmdCommands 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.yaml

95.4

Use 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.yaml

In 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-data

Attach seed.iso as a CD-ROM to the VM (Proxmox can also set cloud-init settings in its UI).

95.5

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 | tail

Real 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.

95.6

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-id

95.7

Make 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 schema in CI before use.
  • Clean images with cloud-init clean so clones get new host keys and machine IDs.

95.8

Update and remove

dnf -y upgrade cloud-init
apt-get install --only-upgrade -y cloud-init
touch /etc/cloud/cloud-init.disabled

The cloud-init.disabled file turns cloud-init off on a server that should never run it again.

95.9

Fix common problems

ProblemCauseFix
User data was ignoredThe first line is not #cloud-config, or YAML errors.Add the header; cloud-init schema --config-file FILE.
runcmd did not run after a changeModules run once per instance.It is a new instance only with a new instance ID; for tests: cloud-init clean --logs and reboot.
status: errorA module failed (package not found, script error).Read /var/log/cloud-init.log and cloud-init-output.log.
SSH key does not workWrong user or key format in users.cloud-init query userdata; check the user's authorized_keys.
No datasource found on local VMsNo metadata service and no seed disk.Attach a NoCloud seed (cloud-localds) or configure the datasource.
Cloned VMs have the same host keys / machine IDThe template was not cleaned.Run the clean steps before saving the image.

95.10

Next steps

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.

All commands in this chapter