هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
cloud-init runs on almost every cloud Linux image (Azure, AWS, GCP, OpenStack, Proxmox). At first boot it reads metadata (host name, network) and your user-data — usually a #cloud-config YAML — and applies it: create users, add SSH keys, install packages, write files, run commands. Terraform passes user-data with custom_data (Azure) or user_data (AWS).
It runs in stages (local, network, config, final); logs are in /var/log/cloud-init.log and /var/log/cloud-init-output.log.
طريقة الكتابة
cloud-init status [--wait] [--long]
cloud-init schema --config-file FILEالأجزاء الرئيسية
Commands
status[--wait] [--long]- Is cloud-init done? Errors?
schema-cFILE/--system- Validate user-data.
analyzeshow|blame- Time per stage and module.
queryuserdata|ds- Show user-data / datasource data.
clean[--logs] [--reboot]- Reset so it runs again at next boot (for images).
single--nameMODULE- Run one module again.
collect-logs- Make a tarball of logs for support.
الخيارات 9
الأكثر استخدامًا
cloud-config keys (user-data)
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 11
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1A secure first-boot config
cat user-data.yaml #cloud-config users: - name: admin groups: wheel sudo: "ALL=(ALL) NOPASSWD:ALL" shell: /bin/bash ssh_authorized_keys: - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyOnly admin@laptop ssh_pwauth: false disable_root: true package_upgrade: true packages: [firewalld, chrony, fail2ban] runcmd: - systemctl enable --now firewalld chronyd - firewall-cmd --permanent --remove-service=cockpit && firewall-cmd --reload2Validate before use
cloud-init schema -c user-data.yaml --annotate Valid schema user-data.yaml3Write a file at first boot
cat files.yaml #cloud-config write_files: - path: /etc/sysctl.d/90-hardening.conf permissions: "0644" content: | net.ipv4.conf.all.rp_filter = 1 kernel.kptr_restrict = 24Pass it from Terraform (Azure)
cat vm.tf resource "azurerm_linux_virtual_machine" "web" { custom_data = base64encode(file("user-data.yaml")) }5Pass it with the Azure CLI
az vm create -g rg-lab -n web1 --image RedHat:RHEL:9-lvm-gen2:latest --custom-data user-data.yaml --ssh-key-values ~/.ssh/id_ed25519.pub6Check that it finished
cloud-init status --wait --long status: done7Read the logs when something failed
8Find slow steps
cloud-init analyze blame | head9See the user-data the VM got
cloud-init query userdata10Prepare a VM to become an image
cloud-init clean --logs11Install a container runtime at first boot
cat containers.yaml #cloud-config packages: [podman, container-tools] runcmd: - loginctl enable-linger admin
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- User-data is readable from the metadata service inside the VM — never put secrets in it.
- The first line must be exactly
#cloud-config. - Most modules run only once per instance; changing user-data later does nothing without
cloud-init clean.
الملفات
/etc/cloud/cloud.cfg- Main config and module list.
/etc/cloud/cloud.cfg.d/- Extra config files.
/var/lib/cloud/- State, the user-data copy, per-instance data.
/var/log/cloud-init.log, /var/log/cloud-init-output.log- Logs.
نصائح للحفظ
- Keep user-data small; put the big setup into Ansible or into the image (Packer).
رموز الخروج
0 | Success (status: done). |
1 | Error (unrecoverable). |
2 | Done with recoverable errors (status). |