Skip to main content

Cloud and deployment · Beginner · 17 min

Create a Hetzner Cloud server with the hcloud CLI, a firewall and cloud-init

By Padding · Updated 26 Nov 2026
Ubuntu 26.04 · hcloud 1.71.0

You will create an Ubuntu 26.04 server on Hetzner Cloud entirely from the hcloud command line, behind a cloud firewall and set up on first boot by your cloud-init file, then snapshot it and delete everything so billing stops.

On this page

Clicking through a cloud console works once. The second time you need a server, you have to remember every choice you made. The hcloud command line tool turns those choices into commands you can keep in a text file, read back later and run again.

In this tutorial you work from your own Ubuntu machine and build one small server on Hetzner Cloud, a German provider with data centres in Germany, Finland, the United States and Singapore. By the end you will have:

  • hcloud 1.71.0 installed from a checksum-verified download, with your API token stored in a named context
  • your public SSH key uploaded to the project
  • a cloud firewall that lets SSH in from your address only, and HTTP and HTTPS in from anywhere
  • an Ubuntu 26.04 server that set itself up on first boot from the user-data.yaml you wrote in the cloud-init user data tutorial
  • a snapshot of that server, and then nothing at all, because the last step deletes every resource so the bill stops

Every command runs on your own machine unless a step says it runs on the server. Everything Hetzner creates is tagged with the label tutorial=hcloud-demo, so you can find it and delete it without touching anything else in your project.

Step 1 · Install the hcloud CLI

Hetzner publishes hcloud as a single program in a release archive on GitHub, with a checksums.txt file listing the SHA-256 of every archive. Download the archive for your machine and check it before you run anything from it. dpkg --print-architecture prints amd64 or arm64, which matches Hetzner's archive names:

Bash
sudo apt update
sudo apt install -y curl ca-certificates jq
VERSION=1.71.0
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://github.com/hetznercloud/cli/releases/download/v${VERSION}/hcloud-linux-${ARCH}.tar.gz"
curl -fsSLO "https://github.com/hetznercloud/cli/releases/download/v${VERSION}/checksums.txt"
sha256sum --ignore-missing -c checksums.txt

The last line must say hcloud-linux-<arch>.tar.gz: OK. If it says FAILED, delete the folder and download again. Then unpack the program and install it:

Bash
tar -xzf "hcloud-linux-${ARCH}.tar.gz" hcloud
sudo install -m 0755 hcloud /usr/local/bin/hcloud
hcloud version

A program you unpack yourself gets no updates from apt. When Hetzner releases a new version, repeat this step with the new number in VERSION. On macOS, brew install hcloud gives you the same tool, kept up to date by Homebrew.

Check it worked

hcloud version prints hcloud v1.71.0. which hcloud prints /usr/local/bin/hcloud.

Step 2 · Create a project API token and store it in a context

hcloud talks to the Hetzner Cloud API, and the API needs a token. Tokens belong to one project, so everything the CLI creates lands in that project. Make a project only for this tutorial in the Hetzner Console if you have not already; it keeps the demo apart from anything real.

In the Hetzner Console, open the project, choose Security in the left menu, then API tokens, then Generate API token. Give it a description such as hcloud laptop, choose Read & Write (a read-only token cannot create servers) and copy it. Hetzner shows the token once only.

Now store it in a context. A context is a name that points at one token, so you can keep several projects apart and switch between them:

Bash
hcloud context create hcloud-demo

The command asks for the token and does not echo it, so it never lands in your shell history. Paste it and press Enter. The new context becomes the active one.

The CLI saves the token in ~/.config/hcloud/cli.toml. Anyone who can read that file can create servers on your bill, so check that only you can read it:

Bash
ls -l ~/.config/hcloud/cli.toml

If the permissions are anything other than -rw-------, run chmod 600 ~/.config/hcloud/cli.toml.

Check it worked

Bash
hcloud context list
hcloud location list

hcloud context list shows hcloud-demo with a marker in the ACTIVE column. hcloud location list prints a table of locations such as fsn1, nbg1, hel1, ash, hil and sin. An unauthorized error means the token was pasted wrongly; delete the context with hcloud context delete hcloud-demo and create it again.

Step 3 · Upload your SSH key

When you create a server, Hetzner can put an SSH key on it. It needs a copy of the public half in your project first.

Check the key is where this tutorial expects it, and make a folder for the files you write next:

Bash
ls -l ~/.ssh/id_ed25519.pub
mkdir -p ~/hcloud-demo
cd ~/hcloud-demo

Upload the public key under a name you will recognise:

Bash
hcloud ssh-key create --name hcloud-demo-key \
  --public-key-from-file ~/.ssh/id_ed25519.pub \
  --label tutorial=hcloud-demo

Only the .pub file leaves your machine. The private key stays in ~/.ssh and is what proves who you are when you log in.

Check it worked

hcloud ssh-key list shows hcloud-demo-key with a fingerprint. Compare it with ssh-keygen -l -E md5 -f ~/.ssh/id_ed25519.pub; the hex part after MD5: is the same.

Step 4 · Write the firewall rules

A Hetzner cloud firewall filters traffic before it reaches the server. Once a firewall is attached, any inbound traffic that no rule allows is dropped, and outbound traffic is allowed because you add no outbound rules. Port 22 should only be open to you, so you need your public IPv4 address.

Find it by opening any "what is my IP" page in a browser on the machine you will log in from. Then set it in your shell. Replace the example address below with your own; 203.0.113.10 is a documentation address that belongs to nobody:

Bash
cd ~/hcloud-demo
MY_IP=203.0.113.10

Write the rules as JSON, in the shape the Hetzner API uses. The heredoc marker has no quotes, so the shell fills in ${MY_IP}:

Bash
cat > firewall-rules.json <<JSON
[
  {
    "description": "SSH from my address only",
    "direction": "in",
    "protocol": "tcp",
    "port": "22",
    "source_ips": ["${MY_IP}/32"]
  },
  {
    "description": "HTTP from anywhere",
    "direction": "in",
    "protocol": "tcp",
    "port": "80",
    "source_ips": ["0.0.0.0/0", "::/0"]
  },
  {
    "description": "HTTPS from anywhere",
    "direction": "in",
    "protocol": "tcp",
    "port": "443",
    "source_ips": ["0.0.0.0/0", "::/0"]
  }
]
JSON
jq -r '.[] | "\(.port)\t\(.source_ips | join(","))\t\(.description)"' firewall-rules.json

/32 means exactly one address. 0.0.0.0/0 and ::/0 mean every IPv4 and every IPv6 address.

If you work over mobile data, your public address can change when you reconnect, and many mobile networks share one address between many customers. If SSH stops connecting later, find your new address, change MY_IP, rewrite the file and apply it with hcloud firewall replace-rules --rules-file firewall-rules.json web-firewall. You can also log in through the server console in the Hetzner Console while you fix it.

Now create the firewall from the file:

Bash
hcloud firewall create --name web-firewall \
  --rules-file firewall-rules.json \
  --label tutorial=hcloud-demo

Check it worked

jq prints three lines: port 22 with your address and /32, then ports 80 and 443 with 0.0.0.0/0,::/0. After the create, hcloud firewall describe web-firewall lists the same three rules and shows no servers under Applied To yet.

Step 5 · Check the cloud-init file before you use it

The server sets itself up on first boot from the user-data.yaml you wrote in the cloud-init user data tutorial. It creates a key-only deploy user with sudo, blocks root and password logins, upgrades packages and turns on ufw. Copy it into the folder. Replace the path with wherever you kept the file:

Bash
cp ~/path/to/user-data.yaml ~/hcloud-demo/user-data.yaml

The file is a template (its first line is ## template: jinja), so the checker needs some sample server data to fill it in. Install cloud-init on your machine if you do not have it, write the sample and check the file:

Bash
sudo apt install -y cloud-init
cd ~/hcloud-demo
cat > sample-instance-data.json <<'JSON'
{
  "v1": {
    "cloud_name": "hetzner",
    "local_hostname": "web-1",
    "instance_id": "demo-0001"
  }
}
JSON
cloud-init schema --config-file user-data.yaml --instance-data sample-instance-data.json
grep -c ssh-ed25519 user-data.yaml
wc -c user-data.yaml

Hetzner's cloud config field takes up to 32 KiB, and wc -c shows the file is a small fraction of that.

The file's ufw rules allow SSH only. The cloud firewall allows HTTP and HTTPS, but ufw on the server still blocks them until you add a rule there, for example sudo ufw allow 80,443/tcp once a web server is installed. Traffic has to pass both layers.

Check it worked

cloud-init schema prints Valid schema user-data.yaml, grep -c prints 1 (your key is in the file) and wc -c prints a number in the low thousands at most.

Step 6 · Create the server

Read this before you run the create command: a Hetzner server is billed from the moment it exists until you delete it, whether it is running or switched off. Billing is hourly, any part of an hour counts as a full hour, and the total never goes over the plan's monthly price. On 26 November 2026, Hetzner's Regular Performance plan CPX12 (1 vCPU, 2 GB RAM, 40 GB disk) cost €0.0160 an hour, capped at €9.99 a month in Helsinki, excluding VAT and including an IPv4 address. A smaller CPX02 plan was listed as new but could not yet be created; when it becomes available, it is a cheaper choice for this tutorial. Prices differ by location and change, so check the current figure on Hetzner's site. Working through this tutorial in an afternoon costs a few cents.

Pick a location, a server type and the Ubuntu 26.04 image:

Bash
hcloud location list
hcloud server-type list
hcloud image list --type system | grep -i ubuntu

server-type list shows each type's cores, memory, disk and architecture; the image list shows the image name to use, such as ubuntu-26.04. Not every type is offered in every location. To see what you can order in Helsinki right now, run hcloud datacenter describe hel1-dc2 and look under Server Types for the Available list (hcloud datacenter list shows the data centre names for each location). Hetzner has no data centre in Africa; choosing a cloud region from Africa explains how to weigh the distance, and from most of the continent the German and Finnish locations are the nearest.

Create the server. Change the type, image or location if your lists differ:

Bash
hcloud server create --name web-1 \
  --type cpx12 \
  --image ubuntu-26.04 \
  --location hel1 \
  --ssh-key hcloud-demo-key \
  --firewall web-firewall \
  --user-data-from-file user-data.yaml \
  --label tutorial=hcloud-demo

Each flag maps to one decision from the earlier steps: the key you uploaded, the firewall you created and the file you checked. The command waits until the server is created and prints its ID and IPv4 address.

If server create stops with an error saying the server type is unavailable or not supported in that location, nothing has been created and nothing is billed. Pick a type from the Available list of that location's data centre, or keep the type and change --location to one that offers it, then run the command again.

On Hetzner's Ubuntu images, --ssh-key puts your key on root. Your cloud-init file gives the same key to deploy and refuses root logins: disable_root: true installs the root key behind a command that rejects the login, and PermitRootLogin no blocks root in the SSH server as well. Keep the flag anyway. If cloud-init ever fails to apply your file, logging in as root with your key still works, and without a key Hetzner emails you a root password instead.

Check it worked

Bash
hcloud server list --selector tutorial=hcloud-demo
hcloud firewall describe web-firewall

web-1 shows the status running with a public IPv4 address. The firewall's Applied To section now lists web-1.

Step 7 · Log in and confirm cloud-init finished

Give the server a minute or two to upgrade its packages. Then log in as deploy; hcloud server ip looks up the address for you:

Bash
ssh deploy@"$(hcloud server ip web-1)"

hcloud server ssh -u deploy web-1 does the same in one command. The first time, SSH asks you to confirm the server's fingerprint; type yes.

On the server, wait for cloud-init and check the firewall it turned on:

Bash
cloud-init status --wait
sudo ufw status
exit

Back on your own machine, try the logins that should fail:

Bash
ssh root@"$(hcloud server ip web-1)"

Check it worked

cloud-init status --wait ends with status: done, and ufw status shows Status: active with OpenSSH allowed. The ssh root@ attempt is refused with Permission denied (publickey). If SSH as deploy hangs rather than being refused, your address has probably changed since you wrote the firewall rules; see the mobile data note in the firewall step.

Step 8 · Take a snapshot

A snapshot is a full copy of the server's disk, stored by Hetzner as an image you can create new servers from. It is billed per gigabyte per month for as long as it exists, separately from the server. Snapshots of a running server work, but if the server is busy writing data, shut it down first with hcloud server shutdown web-1 for a cleaner copy.

Bash
hcloud server create-image --type snapshot \
  --description "web-1 after first boot" \
  --label tutorial=hcloud-demo \
  web-1

The command waits until the snapshot is finished, which takes a minute or two for a small disk. List it:

Bash
hcloud image list --type snapshot --selector tutorial=hcloud-demo

To build a new server from it later, pass its ID to --image in hcloud server create. The new server already has your packages and settings. cloud-init records the instance ID, so on a new server it runs your first-boot steps again; that is normally what you want.

Check it worked

The image list shows one snapshot with the description web-1 after first boot, a DISK SIZE and the status available.

Step 9 · Delete everything so billing stops

This step deletes the server and the snapshot. Anything on the server that is not in the snapshot is gone after the first command, and the snapshot is gone after the second. Copy off anything you want to keep first.

Delete the server, then the snapshot. The snapshot is found by its label, so other images in the project are not touched:

Bash
hcloud server delete web-1
hcloud image list --type snapshot --selector tutorial=hcloud-demo -o noheader -o columns=id
hcloud image delete $(hcloud image list --type snapshot --selector tutorial=hcloud-demo -o noheader -o columns=id)

The middle command shows the ID that is about to be deleted. If it prints nothing, there is no snapshot to delete, and you can skip the last command.

The firewall and SSH key cost nothing, but remove them too, then check that nothing billable remains:

Bash
hcloud firewall delete web-firewall
hcloud ssh-key delete hcloud-demo-key
hcloud server list
hcloud image list --type snapshot
hcloud primary-ip list

If you are done with the CLI for this project, remove the context with hcloud context delete hcloud-demo, and in the Hetzner Console, under Security and API tokens, delete the token so a copy of it can never be used. Finally, remove the demo folder on your machine:

Bash
rm -rf ~/hcloud-demo

Check it worked

hcloud server list, hcloud image list --type snapshot and hcloud primary-ip list each print only a header row. In the Hetzner Console, the project shows no servers and no snapshots, and Usage stops growing.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.