Opening ports on your router to expose a home server or a self-hosted app is the old way to do it — and it is also the riskiest. Every open port is a door that bots, scanners, and attackers can knock on, day and night. Cloudflare Tunnel does away with that entirely. A small daemon called cloudflared runs on your Ubuntu server, dials out to Cloudflare's edge over an encrypted connection, and your service becomes reachable at a public hostname — with no port forwarding, no public IP address, and no firewall holes at all.
In this guide you will install cloudflared on Ubuntu 24.04, create a tunnel, map a hostname such as app.example.com to a service running on your server, and run the tunnel as a systemd service so it survives reboots. It takes about 15 minutes, and the whole setup is free on Cloudflare's free plan.
Prerequisites
- An Ubuntu 22.04 or 24.04 server (a VPS, a home server, or even a Raspberry Pi running Ubuntu) with sudo access.
- A domain name whose DNS is managed by Cloudflare. If your domain is still at your registrar, add it to Cloudflare first (free plan) and point your nameservers at Cloudflare — the dashboard walks you through it, and the zone must show as Active.
- A service already running on the server that you want to expose — for example a web app on port 3000, a dashboard on port 8080, or a plain Nginx site on port 80. The tunnel forwards traffic to it; it does not host anything itself.
- A browser on your own computer for the one-time Cloudflare login. If your server has no desktop, you will open the login link on your laptop.
Step 1: Update the server and check your local service
Start with the usual update, then confirm the service you want to expose is actually listening. There is no point building a tunnel to a service that is not running.
sudo apt update && sudo apt upgrade -y
This refreshes the package lists and installs pending security updates. Next, check what is listening locally. Replace 3000 with your app's port:
ss -tlnp | grep 3000
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:3000
The first command shows whether something is listening on port 3000. The second sends a real request to it and prints the HTTP status code — you want a 200, 301, or 302, anything except a connection error. If curl fails with "Connection refused", start your app first and come back here.
Step 2: Install cloudflared from Cloudflare's official apt repository
Cloudflare ships a signed apt repository, which is the right way to install cloudflared — you get verified packages and automatic updates with the rest of the system. Add the signing key, add the repository for your Ubuntu release, then install.
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 noble main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update
sudo apt install -y cloudflared
The key is stored in /usr/share/keyrings and referenced by the repository line, so apt can verify every package. The noble codename is Ubuntu 24.04 — on Ubuntu 22.04, use jammy instead. Confirm the install worked:
cloudflared --version
You should see a version string such as cloudflared version 2025.x.x. If apt reports the package as not found, double-check the repository file — a typo in the codename is the usual cause.
Step 3: Authenticate cloudflared with your Cloudflare account
Run the login command. It prints a URL — open it in a browser, pick the Cloudflare account and domain you want the tunnel to live under, and authorise. The credentials are saved on the server as a certificate file.
cloudflared tunnel login
On a headless server, copy the URL from the terminal and open it on your laptop; the authorisation still completes because the certificate is written back to the server. After success you will see a message like "You have successfully logged in" and a file appears at ~/.cloudflared/cert.pem. That file is tied to your account — keep it private, and never commit it to a git repository.
Step 4: Create the tunnel
Give the tunnel a name you will recognise later. The name is only for you; visitors never see it.
cloudflared tunnel create home-lab
Cloudflared prints two things you need: the tunnel's UUID (a long string like 6ff42ae2-765d-4adf-a3d2-4d4b6d3e2b0c) and the path of the credentials file, ~/.cloudflared/<uuid>.json. Note the UUID down — you will use it in the config file in the next step. You can list your tunnels at any time with:
cloudflared tunnel list
Step 5: Write the tunnel configuration
The config file maps public hostnames to local services. Create the system config directory, copy the credentials file there so the systemd service does not depend on a user home directory, and write the config. Replace the UUID and the hostname with your own values.
sudo mkdir -p /etc/cloudflared
sudo cp ~/.cloudflared/6ff42ae2-765d-4adf-a3d2-4d4b6d3e2b0c.json /etc/cloudflared/
sudo chmod 600 /etc/cloudflared/6ff42ae2-765d-4adf-a3d2-4d4b6d3e2b0c.json
sudo nano /etc/cloudflared/config.yml
Paste the following into the file, adjusting the UUID, credentials filename, hostname, and port:
tunnel: 6ff42ae2-765d-4adf-a3d2-4d4b6d3e2b0c
credentials-file: /etc/cloudflared/6ff42ae2-765d-4adf-a3d2-4d4b6d3e2b0c.json
ingress:
- hostname: app.example.com
service: http://localhost:3000
- service: http_status:404
Ingress rules are matched top to bottom, and the last rule must be a catch-all — cloudflared refuses to start without one. The catch-all above answers 404 to any hostname that has no rule, instead of leaking anything. You can add more hostnames to the same tunnel later, each pointing at a different local port, and services can also be ssh://, tcp://, or point at other machines on your LAN. Before going further, validate the file:
cloudflared tunnel ingress validate --config /etc/cloudflared/config.yml
A clean config prints the resolved ingress rules. If it complains about YAML, the usual culprit is indentation — YAML needs exactly two spaces per level, no tabs.
Step 6: Route DNS to the tunnel
One command creates the DNS record for your hostname, pointing it at the tunnel. You do not need to touch the Cloudflare dashboard for this.
cloudflared tunnel route dns home-lab app.example.com
This adds a CNAME record for app.example.com that targets your tunnel's UUID. Because DNS is served by Cloudflare itself, the record is live within seconds. Check it with:
dig +short app.example.com
You should get back Cloudflare edge addresses. If dig returns nothing, confirm the zone is Active in the Cloudflare dashboard and that you ran the route command for the right tunnel name.
Step 7: Test the tunnel in the foreground
Before installing anything as a service, run the tunnel once in the foreground so you can watch its logs. Leave this terminal open and test from another window or your browser.
sudo cloudflared tunnel --config /etc/cloudflared/config.yml run home-lab
Within a few seconds you should see log lines showing the tunnel registering connections to Cloudflare's edge. Now open https://app.example.com in a browser — your local app should load over HTTPS, with a valid Cloudflare certificate, even though nothing on your network is listening on the public internet. Press Ctrl+C to stop the tunnel when you are happy.
Step 8: Install the tunnel as a systemd service
The foreground test proves it works; the service makes it permanent. The install command registers a systemd unit that starts the tunnel at boot and restarts it if it crashes.
sudo cloudflared --config /etc/cloudflared/config.yml service install
sudo systemctl enable --now cloudflared
sudo systemctl status cloudflared
The first command writes /etc/systemd/system/cloudflared.service. The second enables it at boot and starts it immediately. The status output should show active (running). From now on, if you ever change /etc/cloudflared/config.yml — for example to add a second hostname — apply it with sudo systemctl restart cloudflared.
Verify it worked
Run these four checks. All four should pass:
systemctl is-active cloudflared
cloudflared tunnel info home-lab
curl -s -o /dev/null -w "%{http_code}\n" https://app.example.com
sudo journalctl -u cloudflared --since "10 minutes ago" --no-pager
The first confirms the service is active. The second shows the tunnel's connections and the hostnames routed to it. The third proves the public hostname answers over HTTPS — expect 200 or your app's normal status code. The journal lines should show steady connections to the edge with no repeated errors. You can also open the Cloudflare dashboard under Zero Trust, then Networks, then Tunnels, where your tunnel should show as Healthy.
Common issues & fixes
Error 1033 in the browser
Error 1033 means Cloudflare received the request but the tunnel is not connected or the hostname has no route. Check that the service is running with systemctl status cloudflared and that you ran the route dns command for this exact hostname. A typo in the hostname anywhere — config, route command, or browser — produces this error.
Service fails to start: "cannot find config.yml"
This is the classic $HOME trap. When the install runs under sudo, $HOME points to /root, so a config sitting in your user's home directory is invisible to the service. Keep the config and credentials under /etc/cloudflared as this guide does, and pass --config /etc/cloudflared/config.yml explicitly, and the problem disappears. Check the exact error with sudo journalctl -u cloudflared -n 50 --no-pager.
502 Bad Gateway or "connection refused" from the tunnel
The tunnel is up, but the local service is not answering on the configured port. Verify with curl -v http://localhost:3000 on the server itself. If that fails, your app is down or listening on a different port — fix the app or the port in config.yml, then restart the service. Also confirm the app binds to localhost (127.0.0.1), not only to a container's internal network.
"tunnel credentials file not found"
The credentials-file path in config.yml does not match the real JSON file. The filename is the tunnel UUID, so it changes with every tunnel you create. Run ls /etc/cloudflared/, compare the filename with the config, and correct the path. Also check permissions — the file should be 600 and readable by root.
Login fails on a headless server
cloudflared tunnel login needs a browser exactly once. If the server has no browser, run the command, copy the URL it prints, and open it on your laptop — the certificate is written to the server regardless of which machine's browser you use. Alternatively, run cloudflared tunnel login on a machine that does have a browser and copy cert.pem to ~/.cloudflared/ on the server.
Conclusion
You now have a Cloudflare Tunnel on Ubuntu exposing your service at a real HTTPS hostname without a single open port. The tunnel dials out, so it works behind NAT, behind CGNAT, and on a home connection where your ISP gives you no public IP at all. Adding another service is just one more ingress rule and one more route dns command. If the app should not be public, the next step is Cloudflare Access in front of the hostname — a login wall with no code changes to your app.
Need help exposing a self-hosted app, hardening an Ubuntu server, or moving services behind Cloudflare the right way? XpertBees sets up and manages servers, tunnels, and DNS for businesses — reach out through xpertbees.in and we will take care of it.



