Skip to content
Log In Sign Up info@muvcloud.com

What cloud-init Does

A cloud image is a generic installation of Ubuntu, Debian, AlmaLinux, Rocky Linux, or another distribution. On first boot, cloud-init turns that generic system into your server. Typical tasks:

  • Network: writes the network configuration for the addresses assigned to the server.
  • Hostname: sets the hostname and, if told to, the entries in /etc/hosts.
  • Users and keys: creates users and installs SSH public keys.
  • Packages: updates the package lists and installs software.
  • Commands and files: writes files and runs commands you supply.

Some modules run once per server, others on every boot.

Where the Settings Come From

cloud-init looks for a datasource: the place where the platform publishes the server's details. It can be a small virtual disk attached to the server, a local metadata web address, or files on the disk. The datasource provides three kinds of data:

  • Meta data: facts about the instance, such as its ID and hostname.
  • User data: what you supply, usually a #cloud-config file or a script.
  • Vendor data: defaults supplied by the platform, which user data can override.

The command cloud-id prints the name of the datasource that was detected, in lowercase, or disabled when cloud-init is turned off.

A cloud-config Example

A cloud-config file is YAML and must start with the line #cloud-config. This one creates a user with an SSH key, sets the hostname, and installs two packages:

#cloud-config
hostname: web1
users:
  - default
  - name: deploy
    groups: sudo
    shell: /bin/bash
    sudo: "ALL=(ALL) NOPASSWD:ALL"
    ssh_authorized_keys:
      - ssh-ed25519 AAAA... your-key-comment
package_update: true
packages:
  - nginx
  - git

The default entry keeps the image's standard user; without it, only the users you list are created, and SSH keys the platform supplies for the standard user may not be installed. On AlmaLinux and Rocky Linux the administrator group is wheel, not sudo. Indentation matters: use spaces, never tabs. To check a file before you use it:

cloud-init schema --config-file user-data.yaml

Check Status and Read the Logs

cloud-init status --long

The output shows whether cloud-init is running, done, or ended with errors, and which datasource it used. In a script, cloud-init status --wait waits until it has finished. Two log files hold the details:

  • /var/log/cloud-init.log: what each stage and module did, with timestamps.
  • /var/log/cloud-init-output.log: the output of commands and package installs.

cloud-init analyze blame lists the modules that took the longest.

Files and Folders

PathContents
/etc/cloud/cloud.cfgMain settings and module lists
/etc/cloud/cloud.cfg.d/Your overrides
/var/lib/cloud/instance/Data of this instance
/var/log/The two log files

Put your own changes in a new file in /etc/cloud/cloud.cfg.d/, for example 99-local.cfg, rather than editing cloud.cfg, which package updates may replace. The user data the server received is stored in /var/lib/cloud/instance/user-data.txt.

When the Hostname or Hosts File Resets

If you change the hostname and it comes back after a reboot, cloud-init set it again from the meta data. Add this line to a file in /etc/cloud/cloud.cfg.d/:

preserve_hostname: true

If your edits to /etc/hosts disappear, the setting manage_etc_hosts is true, and cloud-init rebuilds the file from a template in /etc/cloud/templates/ at every boot. Either edit the template or set manage_etc_hosts: false. Then change the hostname as shown in how to change the hostname in Ubuntu.

Keep Your Own Network Configuration

On Ubuntu, cloud-init writes the network settings to /etc/netplan/50-cloud-init.yaml. To manage the network yourself, create /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with this content:

network: {config: disabled}

cloud-init then stops rewriting the network configuration but does not remove the file it wrote, so the existing settings stay in effect until you change them. Make sure you can reach the server console in your provider's control panel before you change network settings, and take a snapshot first, as explained in snapshot vs backup.

Run It Again or Turn It Off

sudo cloud-init clean removes the state in /var/lib/cloud, so the next boot is treated as a first boot; --logs also removes the log files, and --machine-id resets the machine ID for a template. The next boot can create users again and, by default, replaces the SSH host keys, so clients then warn as described in host key verification failed. Use it on test servers or templates. To stop cloud-init completely, create an empty file:

sudo touch /etc/cloud/cloud-init.disabled

Keep in mind that some platforms rely on cloud-init to apply a new IP address or password set from the control panel. More on first logins is in how to SSH into a server.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations