Cloudflared Tunneling

Cloudflared Tunneling

 You want to expose your laptop’s localhost (like a web app running on http://127.0.0.1:3000 or http://localhost:8000) to the internet using Cloudflare, right?

The easiest and most secure way is using Cloudflare Tunnel (Cloudflared).


🔹 Steps to expose localhost with Cloudflare

  1. Install Cloudflared

    • On Linux/macOS:

      curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb sudo dpkg -i cloudflared.deb
    • On Windows:
      Download from Cloudflared GitHub releases and install.

  2. Login to Cloudflare

    cloudflared tunnel login

    This opens a browser, asks you to log in to your Cloudflare account, and select a domain you own.

  3. Create a Tunnel

    cloudflared tunnel create my-tunnel cloudflared tunnel --url http://localhost:8000 cloudflared tunnel --url https://localhost:10000/ --no-tls-verify
  4. Configure Tunnel to expose localhost
  5. Example: expose http://localhost:8000
    Create a config file:

    tunnel: my-tunnel credentials-file: /home/user/.cloudflared/my-tunnel.json ingress: - hostname: app.yourdomain.com service: http://localhost:8000 - service: http_status:404
  6. Route traffic
    Point your Cloudflare DNS hostname (app.yourdomain.com) to the tunnel:

    cloudflared tunnel route dns my-tunnel app.yourdomain.com
  7. Run the Tunnel

    cloudflared tunnel run my-tunnel

Now your localhost app is available on the internet at
👉 https://app.yourdomain.com


🔹 If you don’t own a domain

Cloudflare Tunnel can still give you a random .trycloudflare.com URL:

cloudflared tunnel --url http://localhost:8000

It will output something like:

https://random-subdomain.trycloudflare.com

Share that link and people can access your local app.


If you start with the random .trycloudflare.com URL now, you can later switch to your own domain without problems.

Here’s how it works:

  • Random mode (cloudflared tunnel --url http://localhost:8000)

    • No domain required.

    • You instantly get a temporary link like https://xyz123.trycloudflare.com.

    • Great for testing or quick sharing.

    • The link changes every time you restart.

  • Domain mode (with your Cloudflare-managed domain)

    • Permanent link like https://app.yourdomain.com.

    • Requires creating a named tunnel + DNS record.

    • You can later “upgrade” your setup by creating a tunnel and pointing it to your domain, without changing your app.

Suppose you want:

  • main.sumanadhikary.com.np → your local website on port 80

  • blog.sumanadhikary.com.np → your local app on port 8080

  • test.sumanadhikary.com.np → your local service on port 10000


🔹 Config file (~/.cloudflared/config.yml)

tunnel: my-tunnel credentials-file: /home/your-username/.cloudflared/my-tunnel.json ingress: - hostname: main.sumanadhikary.com.np service: http://localhost:80 - hostname: blog.sumanadhikary.com.np service: http://localhost:8080 - hostname: test.sumanadhikary.com.np service: http://localhost:10000 - service: http_status:404

🔹 Steps

  1. Login once (to get cert.pem):

    cloudflared tunnel login
  2. Create a tunnel:

    cloudflared tunnel create my-tunnel
  3. Add DNS records (in Cloudflare dashboard or via command):

    cloudflared tunnel route dns my-tunnel main.sumanadhikary.com.np cloudflared tunnel route dns my-tunnel blog.sumanadhikary.com.np cloudflared tunnel route dns my-tunnel test.sumanadhikary.com.np

    (This automatically creates CNAME records pointing to your tunnel.)

  4. Run the tunnel:

    cloudflared tunnel run my-tunnel

✅ Now:

  • https://main.sumanadhikary.com.nplocalhost:80

  • https://blog.sumanadhikary.com.nplocalhost:8080

  • https://test.sumanadhikary.com.nplocalhost:10000

 the software just uses the IP 192.168.19.15, and you don’t know which port it’s running on. That’s common with proprietary intranet apps. No problem — here’s how you can find it:


Step 1: Check if it’s a browser-based app

  • On your office laptop, open a browser and type:

    http://192.168.19.15

    or

    https://192.168.19.15
  • If it loads, the port is default:

    • http → port 80

    • https → port 443


Step 2: Scan the server for open ports

If it doesn’t load in a browser:

Option A: On office laptop (Windows)

  1. Open CMD as Administrator

  2. Run:

netstat -a -n | findstr 192.168.19.15
  • This lists all ports the software/server is listening on.

  • Look for LISTENING entries on that IP.

Option B: On office laptop (Linux)

sudo netstat -tulpn | grep 192.168.19.15
  • Shows which ports are active and which program uses them.


Step 3: Test the port

Once you find a port (e.g., 5000):

cloudflared tunnel --url http://192.168.19.15:5000
  • Cloudflare will give you a public URL.

  • Open that URL from home to access the intranet software fast, no AnyDesk lag.

For windows

Here’s a step-by-step guide to install and use it:


🔹 1. Download Cloudflared for Windows

  1. Go to the official release page: Cloudflared GitHub Releases.

  2. Download the latest cloudflared-windows-amd64.exe (if your system is 64-bit).

  3. Rename it to cloudflared.exe for convenience.

  4. Move it to a folder like:

    • C:\Program Files\cloudflared\

    • or add it directly into a folder already in your PATH (like C:\Windows\System32).


🔹 2. Add to PATH (optional but recommended)

  1. Press Win + R → type sysdm.cpl → Enter.

  2. Go to AdvancedEnvironment Variables.

  3. Under System variables, find Path → Edit → Add new entry:

    C:\Program Files\cloudflared\
  4. Click OK → OK.

Now you can run cloudflared from any terminal (PowerShell or Command Prompt).


🔹 3. Basic Commands

  • Check installation:

    cloudflared --version
  • Run a tunnel to a local web server (example on port 80):

    cloudflared tunnel --url http://localhost:80

    → This gives you a temporary Cloudflare URL to access your local server.

  • Login to Cloudflare (for persistent tunnels):

    cloudflared tunnel login

    → This opens your browser to authenticate with your Cloudflare account.

  • Create a named tunnel:

    cloudflared tunnel create my-tunnel
  • Route a domain (requires Cloudflare-managed domain):

    cloudflared tunnel route dns my-tunnel sub.example.com
  • Run tunnel (persistent):

    cloudflared tunnel run my-tunnel

🔹 4. Run as a Service (Windows)

To make the tunnel always run in background:

cloudflared service install

This installs it as a Windows Service.

if you already own your domain (and it’s managed through Cloudflare DNS), then you can make cloudflared serve your site securely under your own domain instead of the temporary trycloudflare.com links.

Here’s how you’d do it:


🔹 1. Prerequisites

  • Your domain must be added to Cloudflare (via their dashboard).

  • DNS must be managed by Cloudflare (nameservers pointing to Cloudflare).

  • cloudflared installed on Windows (as I showed earlier).


🔹 2. Authenticate cloudflared

Run:

cloudflared tunnel login
  • A browser window will open → login with your Cloudflare account.

  • Select the domain you want to use.

  • A certificate will be downloaded to C:\Users\<You>\.cloudflared\.


🔹 3. Create a Tunnel

cloudflared tunnel create my-tunnel
  • This creates a new tunnel ID.

  • Config file and credentials will be stored under
    C:\Users\<You>\.cloudflared\.


🔹 4. Configure Tunnel (config.yml)

Make a file at:

C:\Users\<You>\.cloudflared\config.yml

Example:

tunnel: my-tunnel credentials-file: C:\Users\<You>\.cloudflared\my-tunnel.json ingress: - hostname: app.yourdomain.com service: http://localhost:8080 - service: http_status:404

👉 Replace:

  • app.yourdomain.com with your real subdomain.

  • http://localhost:8080 with whatever service/port you’re running.


🔹 5. Connect Domain to Tunnel

Run:

cloudflared tunnel route dns my-tunnel app.yourdomain.com
  • This automatically creates a CNAME record in Cloudflare DNS that points to the tunnel.


🔹 6. Run the Tunnel

Now start the tunnel:

cloudflared tunnel run my-tunnel

or install it as a service:

cloudflared service install