هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
#cloud-config
hostname: web1
timezone: Asia/Riyadh
users:
- name: ops
groups: [wheel]
sudo: "ALL=(ALL) NOPASSWD:ALL"
lock_passwd: true
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyOnly ops-laptop
ssh_pwauth: false
package_update: true
packages: [chrony, nginx]
runcmd:
- systemctl enable --now nginxcloud-init schema --config-file user-data.yaml --annotate
aws ec2 run-instances --image-id ami-0abc1234def567890 --instance-type t3.small --subnet-id subnet-0123 --user-data file://user-data.yamlStep by step
- **The first line must be
#cloud-config** — otherwise the file is treated as a script (or ignored). - Check the file before you use it (quick answer):
cloud-init schemacatches YAML and key mistakes on your laptop. - Pass it to the VM:
| Platform | How |
|---|---|
| AWS | --user-data file://user-data.yaml (or user_data in Terraform). |
| Azure | az vm create … --custom-data user-data.yaml. |
| Proxmox / KVM lab | A NoCloud seed: cloud-localds seed.iso user-data.yaml and attach it, or virt-install --cloud-init user-data=user-data.yaml. |
- Order of the modules (simplified): write files and users → packages →
runcmdat the end. Usewrite_filesfor configs andruncmdonly for what has no module. - Did it run? Check on the VM:
cloud-init status --wait --long
status: done
tail -30 /var/log/cloud-init-output.log
cloud-init query userdata | headstatus: error → read /var/log/cloud-init.log and the output log.
- It runs once per instance — changing user-data later does nothing on an existing VM (by design). For lasting configuration use Ansible; to test again in a lab:
cloud-init clean --logs --reboot. - No secrets in user-data — it can be read from the metadata service by processes on the VM, and is stored in the cloud console. Fetch secrets at boot from a secret manager with the VM's role instead.
Check that it worked
You can log in as ops with your key (no password), the hostname and time zone are right, nginx is active, and cloud-init status says done.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Nothing from the file was applied | First line is not #cloud-config, or YAML error. | Fix the header; cloud-init schema --annotate. |
| User exists but SSH key login fails | Key in the wrong field or broken line. | ssh_authorized_keys list, one full key per line. |
runcmd failed, VM "done" anyway | Errors in commands do not stop cloud-init. | Read /var/log/cloud-init-output.log; make commands idempotent. |
| Changed user-data, no effect | Runs once per instance. | New instance, or cloud-init clean in labs. |
| Package install fails | No network/DNS yet, or repo issue. | Check routes/NAT; package_update first. |
| A password visible in the console's user-data | Secret in user-data. | Never put secrets there; rotate it. |
Go further
- Command pages: cloud-init, aws, az.
- Next how-tos: Build a VM image with Packer, Change the hostname (and keep it), Log in with SSH keys.
- Full guide: cloud-init. At work: cloud-init.
1What must the first line of a cloud-config file be?
#cloud-config.
2You changed the user-data of a running VM. Why does nothing happen?
cloud-init applies user-data once per instance, at the first boot.