الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

cloud-init

Set up a cloud VM on its first boot from user-data: users, SSH keys, packages, files and commands.

المستوى
خدمة DevSecOps
المجموعة
البنية التحتية ككود والسحابة
يحتاج root
نعم
الحزمة
cloud-init

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

ماذا يفعل

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 -c FILE / --system
Validate user-data.
analyze show|blame
Time per stage and module.
query userdata|ds
Show user-data / datasource data.
clean [--logs] [--reboot]
Reset so it runs again at next boot (for images).
single --name MODULE
Run one module again.
collect-logs
Make a tarball of logs for support.

الخيارات 9

الأكثر استخدامًا

cloud-config keys (user-data)

users
Users, groups, sudo rules, SSH keys.
ssh_authorized_keys
Keys for the default user.
packages / package_update / package_upgrade
Install and update packages.
write_files
Create files (content, path, permissions).
runcmd
Commands at the end of first boot.
bootcmd
Commands very early on every boot.
timezone / ntp
Time settings.
disk_setup / fs_setup / mounts
Disks.
ssh_pwauth: false
Disable password login.

أمثلة 11

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

  1. 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 --reload
  2. 2Validate before use

    cloud-init schema -c user-data.yaml --annotate
    Valid schema user-data.yaml
  3. 3Write 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 = 2
  4. 4Pass it from Terraform (Azure)

    cat vm.tf
    resource "azurerm_linux_virtual_machine" "web" {
      custom_data = base64encode(file("user-data.yaml"))
    }
  5. 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.pub
  6. 6Check that it finished

    cloud-init status --wait --long
    status: done
  7. 7Read the logs when something failed

    tail -50 /var/log/cloud-init-output.log
    grep -i -E "error|warn" /var/log/cloud-init.log
  8. 8Find slow steps

    cloud-init analyze blame | head
  9. 9See the user-data the VM got

    cloud-init query userdata
  10. 10Prepare a VM to become an image

    cloud-init clean --logs
  11. 11Install 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).

رموز الخروج

0Success (status: done).
1Error (unrecoverable).
2Done with recoverable errors (status).