Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto

Automate setup with startup scripts

Save a shell script or cloud-config in the console, run it once as root on a new server's first boot, and check its output in the cloud-init log.

CloudSat teamUpdated Nov 18, 20263 min read

A startup script is a shell script or a cloud-config file that runs once, as root, on a new server's first boot. Use it to install packages, create users, write configuration files or join a server to your tooling, so every server you deploy starts the same way. You save scripts in the console and pick one when you deploy.

Write a script

  1. Click Startup scripts in the sidebar, under Access.
  2. On the right, in the New script card, give it a Name, for example harden-ubuntu.
  3. Paste the content into the Script field. The first line must say what it is: #!/bin/bash for a shell script, or #cloud-config for cloud-init configuration.
  4. Click Save script. The console answers "Script created. Pick it when you deploy a server." It now appears in Your scripts on the left.

A shell script example for Ubuntu or Debian:

#!/bin/bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade
apt-get -y install fail2ban unattended-upgrades
timedatectl set-timezone UTC

The same idea as cloud-config:

#cloud-config
package_update: true
package_upgrade: true
packages:
  - fail2ban
  - unattended-upgrades
runcmd:
  - timedatectl set-timezone UTC

Use it when you deploy

  1. Click Deploy server and fill in the region, size and image.
  2. In the Access step, choose your script in the Startup script menu. None is the default.
  3. Finish the form and click Deploy now.

The script runs once, when the new server boots for the first time. You cannot attach a script to a server that already exists: on a running server, connect over SSH and run the commands yourself.

Through the API, send the script's id as script_id in the deploy request. The id is the number at the end of the script's address in the console, for example /console/scripts/12. See the API reference.

Check that it ran

Connect to the server over SSH once it shows Running, then:

cloud-init status --long
tail -n 50 /var/log/cloud-init-output.log

The output of your script goes to /var/log/cloud-init-output.log. A long script can still be running a few minutes after the server is reachable: cloud-init status --wait returns when it is done.

Edit or delete a script

  1. On the Startup scripts page, click the script in Your scripts. The card on the right becomes Edit script.
  2. Change the name or the content and click Save script, or click Delete and confirm to remove it.
  3. To start a new script from there, click New at the top of Your scripts.

Servers that already ran a script keep what it did. Changing or deleting the script later does not touch them.

Limits and errors

  • Up to 64 KB per script. A larger one is refused with "Scripts are limited to 64 KB."
  • An empty script is refused with "The script is empty."
  • Windows line endings are converted when you save, so a script written on Windows still runs.
  • If you delete a script while it is selected in an open deploy form, the deploy is refused with "This startup script no longer exists."

Tips

  • The server keeps a reference to the script, not a copy. Leave a script unchanged until the servers deployed with it show Running.
  • Keep secrets out of scripts. A script is stored as you wrote it, and its beginning shows in your list. Fetch keys and passwords from a secret store at run time instead.
  • Make the first line right: a script without #!/bin/bash or #cloud-config may not run at all.
  • Test a new script on an hourly server, read the log, then delete the server.
  • SSH keys do not need a script: tick them in the deploy form. See Add and use SSH keys.
  • For a ready-made stack, start from a one-click app and add your own steps with a script: Deploy a one-click app.