Hetzner CX22/CX23 Setup 2026: Cloud Firewall, Port 22, cloud-init

Published · Updated

Hetzner CX22/CX23 Setup 2026: Cloud Firewall, Port 22, cloud-init

Most VPS tutorials are generic: install Nginx, install Node, done. This one covers the parts that are specific to Hetzner Cloud — the things that trip people up on their first CX22 or CX23:

  • Why SSH times out after you attach a Hetzner Cloud Firewall (and how to open port 22 properly)
  • Using cloud-init so the server is hardened before you ever log in
  • Creating the whole setup reproducibly with the hcloud CLI
  • IPv4 vs IPv6-only servers, and the GitHub gotcha with IPv6-only
  • Backups vs snapshots, and how to get back in when SSH is locked out
  • Why outgoing email on port 25/465 doesn’t work on a new account

If you’re still deciding on the plan, start with Hetzner CX22 pricing and CX22 vs CX23. Everything below works identically on both — CX22 is being phased out, so new servers should use CX23.


Step 1: Pick the Location and Image

In the Hetzner Console, create a project, then Add Server.

  • Location: pick the one closest to your users. Not every server type is offered in every location — if CX23 isn’t listed where you want it, the console simply won’t show it. In that case pick a different location or a CPX type.
  • Image: Ubuntu 24.04 LTS.
  • Type: Shared vCPU → Cost-Optimized → CX23.

Networking — IPv4 or IPv6-only? Hetzner lets you create a server without a public IPv4 to save the IPv4 charge. It works, but be aware:

  • github.com is not reachable over IPv6, so git clone from GitHub fails on an IPv6-only server unless you add a proxy or NAT64.
  • Some of your users (and some APIs you call) may still be IPv4-only.

For a first production server, keep IPv4. Use IPv6-only for internal workers you control.


Step 2: Add Your SSH Key (Never Use a Root Password)

Under SSH keys in the create dialog, paste your public key:

cat ~/.ssh/id_ed25519.pub
# no key yet? ssh-keygen -t ed25519 -C "you@laptop"

When a key is selected, Hetzner doesn’t email you a root password and password login is never needed. Save the key to the project so every future server can reuse it.


Step 3: Harden the Server with cloud-init

The Cloud config field in the create dialog takes a cloud-init file that runs on first boot. This means the server comes up already hardened — no window where it sits on the internet with default settings.

#cloud-config
users:
  - name: deploy
    groups: sudo
    shell: /bin/bash
    sudo: "ALL=(ALL) NOPASSWD:ALL"
    ssh_authorized_keys:
      - ssh-ed25519 AAAA...your-public-key... you@laptop

package_update: true
package_upgrade: true
packages:
  - ufw
  - fail2ban

write_files:
  - path: /etc/ssh/sshd_config.d/99-hardening.conf
    content: |
      PermitRootLogin no
      PasswordAuthentication no

runcmd:
  - ufw allow OpenSSH
  - ufw allow 80/tcp
  - ufw allow 443/tcp
  - ufw --force enable
  - fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
  - echo '/swapfile none swap sw 0 0' >> /etc/fstab
  - systemctl restart ssh

Replace the key line with your own public key. NOPASSWD sudo is a convenience trade-off for a single-developer box; remove it if more people have access.

After the server boots, log in as deploy and confirm cloud-init finished:

ssh deploy@YOUR_SERVER_IP
cloud-init status --wait      # should print: status: done
sudo tail -n 30 /var/log/cloud-init-output.log

If cloud-init status reports an error, the output log tells you which line failed — usually a YAML indentation mistake.


Step 4: Hetzner Cloud Firewall (and Opening Port 22)

Hetzner has two layers of firewall, and mixing them up is the #1 reason people get locked out:

Hetzner Cloud Firewall UFW (on the server)
Where it runs Hetzner’s network, before traffic reaches the VM Inside Ubuntu
Configured in Console / hcloud / API sudo ufw ...
Survives a broken OS config Yes No
Default with no rules Attached firewall with no inbound rules drops all inbound traffic Disabled until enabled

The Cloud Firewall is stateful, so replies to outbound connections (apt, npm, API calls) are allowed automatically.

Recommended inbound rules for a web server:

Protocol Port Source Why
TCP 22 Your IP (or 0.0.0.0/0, ::/0) SSH
TCP 80 0.0.0.0/0, ::/0 HTTP + Let’s Encrypt challenge
TCP 443 0.0.0.0/0, ::/0 HTTPS
ICMP — 0.0.0.0/0, ::/0 Ping, useful for debugging

Leave outbound empty — no outbound rules means all outbound traffic is allowed.

If you have a static home/office IP, restricting port 22 to it removes nearly all SSH brute-force noise. If your IP changes, keep 22 open to all and rely on key-only login plus fail2ban.

“Hetzner port 22 not working” — quick diagnosis

Error from ssh Most likely cause Fix
Connection timed out Cloud Firewall attached without a TCP 22 rule, or UFW enabled without allow OpenSSH Add the rule in the console; if UFW is the problem, use the web console (Step 7)
Connection refused sshd not running or listening on another port Check systemctl status ssh via web console
Permission denied (publickey) Wrong key, or logging in as root after PermitRootLogin no ssh -i ~/.ssh/id_ed25519 deploy@IP
REMOTE HOST IDENTIFICATION HAS CHANGED You rebuilt the server on the same IP ssh-keygen -R YOUR_SERVER_IP

Step 5: Do It All with the hcloud CLI (Optional)

Clicking through the console is fine once. For a second server — or rebuilding after a mistake — the hcloud CLI makes it repeatable. Create an API token under Security → API tokens first.

hcloud context create my-project          # paste the API token

hcloud ssh-key create --name laptop \
  --public-key-from-file ~/.ssh/id_ed25519.pub

hcloud firewall create --name web
hcloud firewall add-rule web --direction in --protocol tcp --port 22  --source-ips 0.0.0.0/0 --source-ips ::/0
hcloud firewall add-rule web --direction in --protocol tcp --port 80  --source-ips 0.0.0.0/0 --source-ips ::/0
hcloud firewall add-rule web --direction in --protocol tcp --port 443 --source-ips 0.0.0.0/0 --source-ips ::/0

hcloud server create --name app-1 --type cx23 --image ubuntu-24.04 \
  --location fsn1 --ssh-key laptop --firewall web \
  --user-data-from-file cloud-init.yaml

Use hcloud location list and hcloud server-type list to see what’s available to your account.


Step 6: Backups vs Snapshots

Hetzner offers two different things, and they’re not interchangeable:

  • Backups — automatic, daily, Hetzner keeps the last 7. Enabled per server and billed as a percentage of the server price (20%). Good as a safety net.
  • Snapshots — manual, kept until you delete them, billed per GB stored. Take one right before risky changes (OS upgrade, resizing from CX22 to CX23, big deploys).
hcloud server enable-backup app-1
hcloud server create-image --type snapshot --description "before-node-upgrade" app-1

Neither replaces a database dump stored off the server. A VM image of a database that was mid-write can restore into a corrupted state.


Step 7: Locked Out? Use the Web Console or Rescue System

If you broke SSH (UFW without port 22, bad sshd_config), you don’t need to rebuild:

  1. Web console: in the Hetzner Console, open the server and click the console icon (>_). This is a direct VNC-style terminal that doesn’t use SSH or the network firewall.
  2. Because you used an SSH key, root has no password. Use Rescue → Reset root password first, then log in through the web console.
  3. Fix the problem (sudo ufw allow OpenSSH, or correct /etc/ssh/sshd_config.d/), then restart ssh.
  4. For deeper problems (unbootable system), enable the Rescue system, reboot, and mount the disk from the rescue environment.

Step 8: Know the Email Port Block

New Hetzner Cloud accounts have outgoing ports 25 and 465 blocked to limit spam. If your Node.js app sends mail directly over SMTP on those ports, it will hang or time out.

  • Use a transactional email provider over port 587 (not blocked) or its HTTP API — this is the better setup anyway.
  • Hetzner can unblock the ports on request once the account has some history (typically after the first paid invoice), but they decide case by case.

Step 9: Deploy the Node.js App

The server is now ready. The application side isn’t Hetzner-specific, so instead of repeating it here:

  1. Node.js + PM2 cluster mode sized for 2 vCPU / 4GB: PM2 VPS optimization guide
  2. Nginx reverse proxy + free SSL with Certbot: Nginx reverse proxy for Node.js
  3. Automated deploys with rollback: Zero-downtime deploy with GitHub Actions

Before pointing DNS at the server, set reverse DNS for the primary IP (server → Networking) to your domain. It’s optional for a web app but required if you ever send mail from this IP.


FAQ

Do I need UFW if I use the Hetzner Cloud Firewall?

Not strictly, but running both is cheap insurance. The Cloud Firewall protects you if the OS is misconfigured; UFW protects you if the Cloud Firewall gets detached or you move the server to a project without it.

Why can I ping the server but not SSH into it?

ICMP is allowed but TCP 22 isn’t. Check the Cloud Firewall rules first, then UFW via the web console.

Can I change CX22 to CX23 without reinstalling?

Resizing is done from the server’s Rescale tab while the server is powered off. Take a snapshot first. See CX22 vs CX23 for the migration checklist.

How does CX23 compare to other providers for Node.js?

See the Hetzner vs DigitalOcean benchmarks and the full Hetzner VPS review for Node.js.