Learning Center
Ubuntu

Installing and Configuring cloud-init on Ubuntu

4 Sep 2025
Hostman Team
Hostman Team

cloud-init is the de facto industry standard for automated initialization of virtual machines in cloud environments. This powerful configuration tool is activated at the first boot of an instance and allows execution of a predefined set of tasks without manual intervention.

Its key functions include:

  • Automating basic system setup, including assigning a hostname.
  • User account management: creating users, assigning permissions, and configuring authentication mechanisms.
  • Automatic deployment of SSH keys for secure access.
  • Configuration of network interfaces according to specified parameters.
  • Operations with disk storage, such as mounting and formatting volumes.
  • Execution of custom scripts for post-installation configuration, which may include installing software, deploying application code, and applying fine-tuned settings.

Although cloud-init is primarily designed for public clouds (AWS, Google Cloud, Azure, Hostman), it can also be used on local virtual machines and even on physical servers to standardize their initial setup.

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

In this article, we will look at how to install, configure, and use cloud-init on Ubuntu.

Installation
Copy link

In most Ubuntu images, cloud-init is already preinstalled. Canonical (the developer of Ubuntu) also releases images called Ubuntu Cloud Images, specially prepared and optimized for running in cloud environments.

In Hostman, all Ubuntu images already include cloud-init.

You can additionally check for cloud-init with the command:

cloud-init --version

Image4

If the command outputs a version (as shown in the screenshot above), then cloud-init is already installed in the system. If the response is Command cloud-init not found, install the utility with:

apt update && apt -y install cloud-init

After installation, cloud-init will automatically run at every system boot. Note that cloud-init runs before the server connects to the network.

Configuration File Structure
Copy link

All cloud-init configuration files are located in /etc/cloud/:

  • /etc/cloud/clean.d/ — directory for cleanup scripts. These scripts are executed when the command cloud-init clean is run.
  • /etc/cloud/cloud.cfg — the main configuration file. This sets the default settings for all initialization stages.
  • /etc/cloud/cloud.cfg.d/ — directory for user configuration files with the .cfg extension. Files are processed in alphabetical order and override settings from the main file. This is the preferred location for custom configurations.
  • /etc/cloud/templates/ — contains templates used by cloud-init to generate system files.
  • /var/lib/cloud/ — stores cache, data, and scripts generated during cloud-init execution.

Modules
Copy link

Modules in cloud-init are separate executable components that perform specific configuration tasks when a VM first boots. Each module is responsible for its own area: network configuration, user creation, package installation, etc.

An important feature of modules is their execution order: they do not run randomly, but in a strict sequence consisting of stages:

  • Init Stage (Initialization stage): Runs immediately after mounting the root filesystem. Modules needed to prepare the system for main configuration are executed here (e.g., mounting additional disks).
  • Config Stage (Configuration stage): The main stage where most modules run: network setup, package installation, SSH key setup, user creation.
  • Final Stage: Executes modules for tasks that should occur at the very end, such as sending system readiness notifications or running user scripts.

Local Usage of cloud-init
Copy link

Let’s test cloud-init locally, i.e., run it after the server has already booted. We will create two scenarios:

  1. The first scenario will create a new user named new-admin, assign a password, and grant administrator rights.
  2. The second scenario will install the packages atop, tree, net-tools.

Since we will use a password for the new user, we need to generate its hash, as all passwords (and other secrets) are specified in plain text by default. . To get a hash, install the whois package, which contains the mkpasswd utility:

apt -y install whois

Run the utility with the SHA-512 hashing algorithm:

mkpasswd -m sha-512 --stdin

Enter the password for the user and press Enter. The utility will generate a password hash.

Image11

Copy this hash for later use.

As noted earlier, user configuration files are stored in /etc/cloud/cloud.cfg.d. Create a new file 99-new-admin-config.cfg:

nano /etc/cloud/cloud.cfg.d/99-new-admin-config.cfg

Use the following content:

#cloud-config
users:
  - name: new-admin
    passwd: $6$BSAzGG4SFvsn//vD$ds8oM53OIs6qXiCIhMTl10bwQfe9u5WxGKADzwyPsODniGhYAXCUOAoyUkJLs.H9z0PxqLr7BxEJ18hT2VEyR/
    sudo: ALL=(ALL) ALL
    shell: /bin/bash
    groups: sudo

Check syntax for errors:

cloud-init schema --config-file /etc/cloud/cloud.cfg.d/99-new-admin-config.cfg

If there are no errors, the command will return Valid schema.

Before running the script, clear the previous configuration:

cloud-init clean

Run the configuration:

cloud-init single --name users-groups --file /etc/cloud/cloud.cfg.d/99-new-admin-config.cfg

After the new configuration is applied, check for the new-admin user:

id new-admin

Next, install the packages. Create a new file:

nano /etc/cloud/cloud.cfg.d/99-install-packages.cfg

Use the following content:

#cloud-config
package_update: true
package_upgrade: true
packages:
  - atop
  - tree
  - net-tools

Check syntax:

cloud-init schema --config-file /etc/cloud/cloud.cfg.d/99-install-packages.cfg

Clear configuration: 

cloud-init clean

Run the script to install the packages:

cloud-init single --name package_update_upgrade_install --file /etc/cloud/cloud.cfg.d/99-install-packages.cfg

Verify the installed packages:

dpkg -l | grep -E "atop|tree|net-tools"

Image6

Using cloud-init in Hostman
Copy link

Hostman cloud servers running Linux support cloud-init via the control panel. Scenarios can be configured both during server ordering and later during usage. Let’s look at the practical use of cloud-init.

We will create a scenario that will:

  • Create a new user named new-usr;

  • Configure SSH key authentication for new-usr;

  • Install two packages: mc, ncdu;

  • Change the hostname to hostman-server;

  • Create a file test-file.txt in the /tmp directory.

If cloud-init scripts have already been run on the server, run cloud-init clean before applying the configuration below.

Our script will run when creating a virtual server; we can add it at step 7:

Image5

Since SSH key authentication will be used for the new user, generate keys in advance. On another device (Windows, macOS, Linux), run the command:

ssh-keygen

Save the keys in the default directory (.ssh in the home directory). Then obtain the public key value (.pub file):

cat ~/.ssh/id_ed25519.pub

Replace id_ed25519.pub with your own filename if different.

In the control panel, in the cloud-init block, enter the following syntax:

#cloud-config
packages:
  - mc
  - ncdu

users:
  - name: "new-usr"
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh_authorized_keys:
      - ssh-rsa AAAAC3NzaC1lZDI1NTE5AAAAIFoUTI5BKDBDgKLIMpM71m/YI7dTtFKQiSIivRk9pUbs alex@DESKTOP-VTUJHJ9
    lock_passwd: true

hostname: hostman-server
preserve_hostname: false

runcmd:
  - [touch, /tmp/test-file.txt]

In the ssh_authorized_keys field, enter your own public key.

Image4

Complete the server order by clicking “Order.”

Once the server is created, connect via SSH with the new user and verify that all specified actions were completed.

Verify the user:

id new-usr

Image2

Verify installed packages:

dpkg -l | grep -E "mc|ncdu"

Image1

Verify hostname:

hostname

Image3

Verify file existence:

ls -lah /tmp/test-file.txt

Image6

Conclusion
Copy link

cloud-init is a powerful tool for automating the initial setup of servers in Ubuntu, even ones with storage included. With its capabilities, you can deploy fully configured servers in seconds, minimize human error, and easily scale infrastructure.

The main strength of cloud-init lies in its ability to transform a virtual machine template into a fully configured, production-ready server instance without manual intervention. Automating network configuration, security updates, user creation, and software deployment are the advantages that make it indispensable for DevOps engineers and system administrators.

Don't forget to check our low-latency servers in the US.