User data (cloud-init)
Configure a server on its first boot with a #cloud-config document or a shell script – install Docker, add users, mount a disk.
User data runs once, as root, after the server's first boot (and after a one-click app, if you chose one).
Use it to install packages, add users or start your application. The output is shown on the server page.
Give it in the console (New server → Access → User data), as user_data in POST /servers, or as
user_data in the Terraform server resource (changing it there replaces the server).
Two formats are accepted, at most 32 KB:
- a script whose first line is
#!(for example#!/bin/bash); - a
#cloud-configYAML document.
Scripts
Anything that starts with #! is run as a program. Use set -e so a failing step stops the script:
#!/bin/bash
set -euo pipefail
apt-get update
apt-get install -y nginx
echo "Hello from $(hostname)" > /var/www/html/index.html
systemctl enable --now nginx
cloud-config
A document that starts with #cloud-config is applied by cloud-init inside the server, with cloud-init's own
modules. It is checked when you create the server (YAML, no tabs) and validated again in the server with
cloud-init schema before it runs.
Supported top-level keys:
| Key | Module |
|---|---|
write_files |
write files |
users, groups |
users and groups |
timezone, locale |
time zone and locale |
apt, yum_repos |
package repositories |
package_update, package_upgrade, package_reboot_if_required, packages |
packages |
runcmd |
commands, run last |
Some keys are not available because the platform sets these things itself: hostname, fqdn,
manage_etc_hosts, network, ssh_authorized_keys and ssh_keys at the top level (use the server's SSH keys;
inside users they are fine), ssh_deletekeys, chpasswd, password, disable_root, bootcmd,
power_state, mounts, disk_setup and fs_setup. Use runcmd or a script for those. Unknown keys are refused
with a list of the supported ones.
The image must have cloud-init installed (all standard images do).
Example: install Docker
#!/bin/bash
set -euo pipefail
curl -fsSL https://get.docker.com | sh
mkdir -p /opt/app && cd /opt/app
cat > compose.yaml <<'YAML'
services:
web:
image: nginx:alpine
ports: ["80:80"]
restart: unless-stopped
YAML
docker compose up -d
Remember to allow port 80 with a security group.
Example: add users
#cloud-config
users:
- name: deploy
groups: sudo
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAA... deploy@ci
- name: alice
shell: /bin/bash
ssh_authorized_keys:
- ssh-ed25519 AAAA... alice@laptop
timezone: Europe/Ljubljana
Example: format and mount an extra disk
Extra disks are added after the server exists (Server → Storage → Add disk, or POST /servers/{id}/disks) and
appear in the server as /dev/sdb, /dev/sdc, ... (check with lsblk). mounts, disk_setup and fs_setup
are not available, so let user data install a small helper that formats the disk only when it is empty and
mounts it on /data; it runs at first boot (does nothing without the disk) and you run it again after adding one:
#cloud-config
write_files:
- path: /usr/local/sbin/mount-data
permissions: '0755'
content: |
#!/bin/sh
set -e
[ -b /dev/sdb ] || { echo "no /dev/sdb yet"; exit 0; }
blkid /dev/sdb >/dev/null || mkfs.ext4 -L data /dev/sdb
mkdir -p /data
grep -q "LABEL=data" /etc/fstab || echo "LABEL=data /data ext4 defaults,nofail 0 2" >> /etc/fstab
mount -a
runcmd:
- /usr/local/sbin/mount-data
After adding the disk: ssh <user>@<server> sudo /usr/local/sbin/mount-data.
Example: automatic security updates
#cloud-config
package_update: true
package_upgrade: true
packages:
- unattended-upgrades
Troubleshooting
- The output of the script (or of each cloud-init module) is on the server page.
- Your cloud-config is kept in the server at
/var/lib/hfvps/user-data.yaml. - Inside the server,
cloud-init status --longand/var/log/cloud-init-output.logshow cloud-init's own first boot.