Skip to content
Back to Series Homelab from Scratch
Homelab

Cloudflare Tunnel: Access Your Homelab from Anywhere Without Port Forwarding

A complete guide to setting up Cloudflare Tunnel to access your homelab from anywhere, securely and for free, with no port forwarding required.

A
Alif Afriza Author
· · 5 min read
Cloudflare Tunnel: Access Your Homelab from Anywhere Without Port Forwarding

Cloudflare Tunnel is the safest way to access your homelab from anywhere. No port forwarding, no VPN, and most importantly, your server’s real IP is not exposed to the internet.

Why Cloudflare Tunnel?

The Problem with Traditional Port Forwarding

Port forwarding is the old way to access a server from the internet:

[Internet] → [Port Forward on Router] → [Server]

Problems:

  • Server IP is exposed to the internet
  • Vulnerable to brute force attacks
  • Requires dynamic DNS if the IP changes
  • Open ports = security holes

The Cloudflare Tunnel Solution

[Internet] → [Cloudflare Edge] → [Tunnel] → [Server]

Advantages:

  • Server IP is not exposed
  • No port forwarding required
  • Automatic HTTPS
  • DDoS protection from Cloudflare
  • Access from anywhere
  • Free for normal usage

Preparation

1. Cloudflare Account

Make sure you already have a Cloudflare account and a registered domain.

2. Install Cloudflare on the Server

# Debian/Ubuntu
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 $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update && sudo apt install cloudflared -y

# Or download directly
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared-linux-amd64.deb

3. Log In to Cloudflare

# Login (will open a browser)
cloudflared tunnel login

The browser will open; select the domain you want to use. The certificate file will be saved at ~/.cloudflared/cert.pem.

Creating a Tunnel

Step 1: Create a Tunnel

# Create a new tunnel
cloudflared tunnel create myhomelab

# Note the Tunnel ID that appears
# Example: a1b2c3d4-e5f6-7890-abcd-ef1234567890

Step 2: Configure the Tunnel

Create a configuration file:

# Create directory
mkdir -p ~/.cloudflared

# Create config file
nano ~/.cloudflared/config.yml
# ~/.cloudflared/config.yml
tunnel: your-tunnel-id
credentials-file: /root/.cloudflared/your-tunnel-id.json

ingress:
  # Main website
  - hostname: afrizalab.my.id
    service: http://localhost:80
  
  # ByteNesia Blog
  - hostname: bytenesia.afrizalab.my.id
    service: http://localhost:80
  
  # Nextcloud
  - hostname: nextcloud.afrizalab.my.id
    service: http://localhost:8080
  
  # Pi-hole Admin
  - hostname: pihole.afrizalab.my.id
    service: http://localhost:8053
  
  # SSH Access
  - hostname: ssh.afrizalab.my.id
    service: ssh://localhost:22
  
  # Catch-all rule (required at the end)
  - service: http_status:404

Step 3: Register DNS Routes

# Register DNS route for the tunnel
cloudflared tunnel route dns myhomelab afrizalab.my.id
cloudflared tunnel route dns myhomelab bytenesia.afrizalab.my.id
cloudflared tunnel route dns myhomelab nextcloud.afrizalab.my.id
cloudflared tunnel route dns myhomelab pihole.afrizalab.my.id
cloudflared tunnel route dns myhomelab ssh.afrizalab.my.id

Step 4: Run the Tunnel

# Run tunnel (foreground)
cloudflared tunnel run myhomelab

# Or run as a service
sudo cloudflared service install
sudo systemctl enable cloudflared
sudo systemctl start cloudflared

Setting Up as a Systemd Service

Step 1: Install the Service

# Install as a system service
sudo cloudflared service install

# Enable service to run at boot
sudo systemctl enable cloudflared

# Start service
sudo systemctl start cloudflared

# Check status
sudo systemctl status cloudflared

Step 2: Configure the Service

# Edit service configuration
sudo nano /etc/systemd/system/cloudflared.service
[Unit]
Description=Cloudflare Tunnel
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/cloudflared tunnel run myhomelab
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
# Reload systemd
sudo systemctl daemon-reload

# Restart service
sudo systemctl restart cloudflared

Cloudflare Access (Zero Trust)

Additional Security

Cloudflare Access provides an additional layer of security:

  1. Log in to the Cloudflare Zero Trust Dashboard
  2. Select Access → Applications
  3. Click “Add an application”
  4. Select “Self-hosted”
  5. Fill in the configuration:
    • Application name: Homelab
    • Session duration: 24 hours
    • Application domain: your-domain.com

Set Up a Policy

  1. Select Access → Policies
  2. Click “Add policy”
  3. Configuration:
    • Policy name: Allow Me
    • Action: Allow
    • Include: Emails ending in @gmail.com (your email)

Result

Now, when you access your homelab from the internet, you will be asked to log in first. Only registered emails can access it.

Monitoring the Tunnel

Check Tunnel Status

# Check status
cloudflared tunnel info myhomelab

# Check logs
sudo journalctl -u cloudflared -f

# Or check logs directly
sudo tail -f /var/log/cloudflared/cloudflared.log

Metrics

The Cloudflare dashboard displays:

  • Total requests
  • Bandwidth usage
  • Error rates
  • Latency

Troubleshooting

1. Tunnel Cannot Connect

# Check credentials
ls -la ~/.cloudflared/*.json

# Test connection
cloudflared tunnel run --url http://localhost:80 myhomelab

# Check DNS
dig +short your-domain.com

2. Service Does Not Start

# Check logs
sudo journalctl -u cloudflared -n 50

# Check whether the port is already in use
sudo netstat -tlnp | grep :80

# Restart service
sudo systemctl restart cloudflared

3. 502 Bad Gateway

# Make sure the local service is running
curl http://localhost:80

# Check whether the port is correct
sudo netstat -tlnp

# Test tunnel directly
cloudflared tunnel --url http://localhost:80 run myhomelab

Case Study: A Homelab Accessible from Anywhere

Requirements:

  • Access the blog website from the internet
  • Access Nextcloud from a phone
  • Access the Pi-hole admin panel from outside the home
  • SSH access for maintenance

Configuration:

  • Tunnel: myhomelab
  • Domain: afrizalab.my.id
  • Routes:
    • afrizalab.my.id → localhost:80 (Nginx)
    • nextcloud.afrizalab.my.id → localhost:8080
    • pihole.afrizalab.my.id → localhost:8053
    • ssh.afrizalab.my.id → localhost:22

Results:

  • The website can be accessed from anywhere
  • Nextcloud can be accessed from a phone without a VPN
  • The Pi-hole admin panel can be accessed from outside
  • SSH can be accessed from anywhere (with Cloudflare Access)

Best Practices

1. Use Cloudflare Access

Always enable Cloudflare Access for sensitive services. This prevents unauthorized access.

2. Back Up Tunnel Credentials

# Back up credentials
cp -r ~/.cloudflared/ ~/backup/cloudflared/

# Store in a safe place (cloud storage, etc.)

3. Monitor Usage

Monitor usage in the Cloudflare dashboard to detect suspicious activity.

4. Use WARP for Personal Access

Cloudflare WARP can be used for personal access without exposure to the internet:

# Install WARP
curl -fsSL https://pkg.cloudflareclient.com/install.sh | sudo bash

# Connect
warp-cli connect

Conclusion

Cloudflare Tunnel is the best solution for accessing your homelab from anywhere. No port forwarding, no VPN, and most importantly, your server’s IP stays safe. With Cloudflare Access, you can add an extra layer of security to protect your homelab from unauthorized access.

Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments