Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 2 ๐Ÿ“– 9 min read

How to set up Cloudflare Tunnel on an Ubuntu 24.04 VPS

This tutorial sets up Cloudflare Tunnel (cloudflared 2026.11.2) on an Ubuntu 24.04 VPS so you can expose web apps without opening ports 80 or 443 in the firewall and without publishing the server's IP address in DNS. It

How to set up Cloudflare Tunnel on an Ubuntu 24.04 VPS

This tutorial sets up Cloudflare Tunnel (cloudflared 2026.11.2) on an Ubuntu 24.04 VPS so you can expose web apps without opening ports 80 or 443 in the firewall and without publishing the server's IP address in DNS. It covers installing a pinned version from the official apt repository, preventing unexpected restarts (apt-mark hold and --no-update-service), configuring either a dashboard token or a validated config.yml, running the tunnel in Docker Compose and performing controlled upgrades. Package installation, version pinning, ingress rule validation and local port checks were tested on 2 December 2026 on a clean Ubuntu 24.04.5 LTS environment.

Diagram of the Cloudflare Tunnel tutorial on an Ubuntu 24.04 VPS: on the left, the VPS with ufw closed on ports 80 and 443, an application listening only on 127.0.0.1:8080 and cloudflared 2026.11.2 pinned with apt-mark hold and --no-update-service; in the center, an outbound-only connection to the Cloudflare edge without exposing the VPS IP; on the right, the Cloudflare edge serving HTTPS for the configured domains

Versions used

Component Version Source
cloudflared (.deb package) 2026.11.2 (23 Nov 2026) cloudflared releases
cloudflared (Docker image) cloudflare/cloudflared:2026.11.2 Docker Hub
Demo Docker service traefik/whoami:v1.12.0 whoami repository
Operating system Ubuntu 24.04.5 LTS (amd64 and arm64)

The /usr/bin/cloudflared binary installed by the official .deb package is 39 MB on amd64, and the cloudflare/cloudflared:2026.11.2 Docker image takes about 106 MB uncompressed.

When Cloudflare Tunnel makes sense

In a standard setup, a reverse proxy like Caddy or Traefik listens on ports 80 and 443, a DNS A record points to the VPS IPv4 address and the proxy issues the TLS certificate. Cloudflare Tunnel flips that direction: cloudflared opens outbound connections from the VPS to the Cloudflare edge, and visitor HTTPS traffic travels from the edge back to your local service.

That helps in three common cases:

  • Smaller attack surface on the VPS: ports 80 and 443 stay closed in ufw, and the server IP does not appear in public DNS records.
  • Internal dashboards behind edge authentication: you can place Cloudflare Access in front of admin panels that should never sit open on the internet.
  • No published Docker ports: when cloudflared runs on the same Compose network, the application container needs no ports: entry on the host at all.

The trade-off is that all traffic flows through Cloudflare, subject to edge upload limits and timeouts, and TLS terminates at Cloudflare rather than directly on your server. If you want direct control on the VPS, see Traefik vs Caddy on a Docker VPS.

What you will need

cloudflared itself uses very little memory and CPU. Size the VPS according to the applications you plan to run behind the tunnel.

1. Keep the firewall closed to HTTP and HTTPS

With a tunnel, the server receives no inbound HTTP or HTTPS connections. Allow only SSH in ufw and verify the rules:

sudo ufw allow 22/tcp
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status

cloudflared only needs outbound connectivity: by default it connects using QUIC over UDP port 7844 and falls back to HTTP/2 over TCP port 7844 (plus HTTPS on 443 for registration and APIs). With default allow outgoing, no extra outbound rules are needed. If an upstream firewall restricts outbound traffic, allow TCP and UDP on port 7844 to the Cloudflare edge.

A note on Docker and ufw. If you run Docker services on this same VPS, avoid publishing ports on 0.0.0.0 (such as "8080:8080"): Docker bypasses ufw. Bind to 127.0.0.1:8080:8080 or omit ports: altogether when cloudflared shares the Compose network. See The Docker port stays open with ufw for why.

2. Install a pinned version of cloudflared

The official Cloudflare package repository recommends the any main suite with a dedicated GPG key on Debian-based distributions. Add the key and repository:

sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt-get update

Check the version available in the repository before installing:

apt-cache madison cloudflared

On 2 December 2026 on Ubuntu 24.04.5 LTS (amd64), the output was:

cloudflared |  2026.11.2 | https://pkg.cloudflare.com/cloudflared any/main amd64 Packages

Install that exact version, hold the package so apt upgrade does not restart the tunnel unexpectedly, and verify the binary:

sudo apt-get install -y cloudflared=2026.11.2
sudo apt-mark hold cloudflared
apt-mark showhold
cloudflared --version

apt-mark showhold should print cloudflared, and --version confirms:

cloudflared version 2026.11.2 (built 2026-11-23-13:03 UTC)

3. Bind your application to loopback only

Before connecting the tunnel, make sure the local service listens only on 127.0.0.1. For a quick check without extra packages, you can start a temporary HTTP server on 127.0.0.1:8080:

python3 -m http.server 8080 --bind 127.0.0.1 &

Verify the bound address with ss and check the local response:

ss -tulpn | grep 8080
curl -fsSI http://127.0.0.1:8080

ss should show 127.0.0.1:8080, never 0.0.0.0:8080 or [::]:8080. Configure your real application (Node, Go, Python or Docker) to bind to 127.0.0.1 the same way.

4. Run the tunnel as a system service

Cloudflare supports two ways to manage routes: remotely managed through the dashboard, where you pass a token and edit hostnames in Zero Trust, or locally managed with config.yml, where routing stays in a versioned file on the VPS. Pick one of the two options below.

Option A: remotely managed tunnel (fastest path)

  1. In Cloudflare Zero Trust, open Networks > Tunnels and select Create a tunnel (type Cloudflared).
  2. Name the tunnel (for example, vps-prod-01) and copy the token from the install step.
  3. Under Public Hostname, point app.example.com to HTTP at 127.0.0.1:8080. Cloudflare creates the DNS CNAME record automatically.
  4. On the VPS, install the system service with auto-updates turned off:
sudo cloudflared service install --no-update-service PASTE_YOUR_TOKEN_HERE

Do not omit --no-update-service. In cloudflared 2026.11.2 installed from the .deb package, --no-update-service defaults to false. Without it, service install adds an auto-update timer that restarts the tunnel on its own, even when apt-mark hold is active.

In version 2026.11.2, passing a token to cloudflared service install writes the secret to a file inside the service configuration directory and configures the systemd unit to read it with --token-file, keeping the token out of ps arguments. Clear the command from shell history (or prefix it with a leading space when HISTCONTROL=ignorespace is set) and check the service:

sudo systemctl status cloudflared --no-pager
sudo journalctl -u cloudflared -n 30 --no-pager

Option B: locally managed tunnel (config.yml)

If you prefer to keep routing rules in a file on the server, create the tunnel from the CLI. tunnel login prints a URL to authorize your domain in Cloudflare:

cloudflared tunnel login
cloudflared tunnel create vps-prod-01

create prints the tunnel UUID and saves its credentials at ~/.cloudflared/<UUID>.json. Copy the credentials to /etc/cloudflared/ with strict permissions and register the DNS routes:

sudo install -d -m 750 /etc/cloudflared
sudo install -m 600 ~/.cloudflared/<UUID>.json /etc/cloudflared/<UUID>.json
cloudflared tunnel route dns vps-prod-01 app.example.com
cloudflared tunnel route dns vps-prod-01 api.example.com

Create /etc/cloudflared/config.yml, replacing <UUID> with your tunnel ID:

/etc/cloudflared/config.yml

tunnel: <UUID>
credentials-file: /etc/cloudflared/<UUID>.json

ingress:
  - hostname: app.example.com
    service: http://127.0.0.1:8080
  - hostname: api.example.com
    service: http://127.0.0.1:3000
  - service: http_status:404

Before installing the service, validate the configuration and test how individual URLs match:

sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress validate
sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress rule https://app.example.com
sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress rule https://other.example.com

In our test, validation returned:

Validating rules from  /etc/cloudflared/config.yml
OK

And the rule check confirmed both the matching hostname and the fallback 404:

Using rules from /etc/cloudflared/config.yml
Matched rule #0
    hostname: app.example.com
    service: http://127.0.0.1:8080
Using rules from /etc/cloudflared/config.yml
Matched rule #2
    service: http_status:404

Once the rules validate, install the systemd unit with --no-update-service and check its status:

sudo cloudflared service install --no-update-service
sudo systemctl enable --now cloudflared
sudo systemctl status cloudflared --no-pager

Whenever you edit /etc/cloudflared/config.yml, run ingress validate first and restart the unit with sudo systemctl restart cloudflared.

5. Docker Compose alternative (nothing installed on the host)

If your workloads already run in Docker Compose, you can add cloudflared directly to the project's internal network. The application needs no ports: entry on the host, and the tunnel connects directly to the service name (such as http://whoami:80).

Create the directory, .env and docker-compose.yml:

mkdir -p ~/apps/tunnel-demo && cd ~/apps/tunnel-demo

.env

CLOUDFLARED_VERSION=2026.11.2
TUNNEL_TOKEN=paste-your-dashboard-token-here
chmod 600 .env

docker-compose.yml

services:
  whoami:
    image: traefik/whoami:v1.12.0
    restart: unless-stopped

  cloudflared:
    image: cloudflare/cloudflared:${CLOUDFLARED_VERSION}
    restart: unless-stopped
    command: tunnel --no-autoupdate --metrics 0.0.0.0:2000 run
    environment:
      - TUNNEL_TOKEN=${TUNNEL_TOKEN}
    depends_on:
      - whoami

Validate the Compose file and start the stack:

docker compose config -q
docker compose up -d
docker compose logs -f cloudflared

In the Zero Trust dashboard, set the Public Hostname service URL to http://whoami:80 (the Compose service name, not 127.0.0.1). The official cloudflare/cloudflared:2026.11.2 image already defaults --no-autoupdate to true, though keeping the flag in command makes the behavior explicit. Passing the secret through TUNNEL_TOKEN avoids exposing it in command arguments.

6. Controlled upgrades

When a new version appears in cloudflared releases, upgrade inside a maintenance window. For the apt install:

sudo apt-get update
apt-cache madison cloudflared
sudo apt-mark unhold cloudflared
sudo apt-get install -y cloudflared=NEW_VERSION
sudo apt-mark hold cloudflared
sudo systemctl restart cloudflared
cloudflared --version

For Docker Compose, update CLOUDFLARED_VERSION in .env and recreate the container:

cd ~/apps/tunnel-demo
docker compose pull cloudflared
docker compose up -d

Common issues

  • Validation failed: The last ingress rule must match all URLs (i.e. it should not have a hostname or path filter): the catch-all rule is missing from config.yml. Add - service: http_status:404 as the last entry under ingress:.
  • 502 Bad Gateway while the service is running locally: check that the service URL uses http://127.0.0.1:PORT rather than http://localhost:PORT. On Ubuntu, localhost can resolve to ::1 (IPv6) while the app listens only on IPv4 (127.0.0.1). In Docker Compose, target the Compose service name (http://whoami:80), never 127.0.0.1.
  • Domain returns error 1033 (Argo Tunnel error): the cloudflared daemon stopped or lost all edge connections. Inspect sudo journalctl -u cloudflared -n 50 --no-pager for credential or network errors.
  • QUIC handshake fails (UDP 7844 blocked): if an upstream firewall blocks outbound UDP, test forcing HTTP/2 by adding --protocol http2 to the tunnel run command.
  • cloudflared listed under kept back during apt upgrade: expected behavior from apt-mark hold.

What was left out of the test

The tests on 2 December 2026 ran in clean Ubuntu 24.04.5 LTS containers (amd64 and arm64) and with the cloudflare/cloudflared:2026.11.2 image. Real Cloudflare account login (cloudflared tunnel login), public DNS CNAME creation, an active connection to the Cloudflare edge and running under a full systemd VM with an active ufw firewall were left out of the test.

Next steps

Originally published at https://nuvits.net/en/blog/install-cloudflare-tunnel-ubuntu-vps

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.