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:
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.
In this article, we will look at how to install, configure, and use cloud-init on Ubuntu.
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
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.
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 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:
Let’s test cloud-init locally, i.e., run it after the server has already booted. We will create two scenarios:
new-admin
, assign a password, and grant administrator rights.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.
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"
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:
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.
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
Verify installed packages:
dpkg -l | grep -E "mc|ncdu"
Verify hostname:
hostname
Verify file existence:
ls -lah /tmp/test-file.txt
cloud-init is a powerful tool for automating the initial setup of servers in Ubuntu. 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.