متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 167IaC · cloud-init

How to set up a new VM with cloud-init

Every new cloud VM runs cloud-init at its first boot: it reads your user-data and sets the hostname, users and SSH keys, installs packages, writes files and runs commands — so the VM is ready without anyone logging in. This how-to writes a small, safe #cloud-config, passes it in AWS, Azure and a local KVM lab, checks that it ran, and debugs it when it did not.

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

167.1

The quick answer

user-data.yaml
#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 nginx
cloud-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.yaml

167.2

Step by step

  1. **The first line must be #cloud-config** — otherwise the file is treated as a script (or ignored).
  2. Check the file before you use it (quick answer): cloud-init schema catches YAML and key mistakes on your laptop.
  3. Pass it to the VM:
PlatformHow
AWS--user-data file://user-data.yaml (or user_data in Terraform).
Azureaz vm create … --custom-data user-data.yaml.
Proxmox / KVM labA NoCloud seed: cloud-localds seed.iso user-data.yaml and attach it, or virt-install --cloud-init user-data=user-data.yaml.
  1. Order of the modules (simplified): write files and users → packages → runcmd at the end. Use write_files for configs and runcmd only for what has no module.
  2. 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 | head

status: error → read /var/log/cloud-init.log and the output log.

  1. 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.
  2. 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.

167.3

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.

167.4

Common problems

ProblemCauseFix
Nothing from the file was appliedFirst line is not #cloud-config, or YAML error.Fix the header; cloud-init schema --annotate.
User exists but SSH key login failsKey in the wrong field or broken line.ssh_authorized_keys list, one full key per line.
runcmd failed, VM "done" anywayErrors in commands do not stop cloud-init.Read /var/log/cloud-init-output.log; make commands idempotent.
Changed user-data, no effectRuns once per instance.New instance, or cloud-init clean in labs.
Package install failsNo network/DNS yet, or repo issue.Check routes/NAT; package_update first.
A password visible in the console's user-dataSecret in user-data.Never put secrets there; rotate it.

167.5

Go further

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.

كل الأوامر في هذا الفصل