scripts/service.cloudflared.md
2025-05-05 11:58:47 -06:00

3.2 KiB

tags, author, date
tags author date
script
The Bunker Admin 2025-04-29

Overview

This guide provides instructions for configuring a Cloudflare Tunnel as a systemd service on Ubuntu Linux. This ensures the tunnel automatically starts whenever the server boots up, providing continuous access to your services.

Prerequisites

  • Ubuntu Linux server (tested on Ubuntu 24.04)
  • Cloudflared installed
  • Existing Cloudflare Tunnel configuration file
  • Administrator (sudo) privileges

Ensure you have a Cloudflare Tunnel already created by following instructions at config.cloudflare.homelab.

Step-by-Step Instructions

1. Locate your cloudflared binary

First, determine the exact path to your cloudflared executable:

which cloudflared

[!note] Standard Output

/usr/local/bin/cloudflared

Note the output (typically cloudflared).

2. Create a systemd service file

Create a new systemd service file to manage the Cloudflare Tunnel:

sudo nano /etc/systemd/system/cloudflared-tunnel.service

Add the following configuration to the file, replacing the placeholders with your specific values.

Replace the following:

  • YOUR_USERNAME: The system user that should run the cloudflared service
  • /path/to/cloudflared: The full path to your cloudflared binary (from step 1)
  • /path/to/your/config.yml: The full path to your tunnel configuration file
[Unit]
Description=Cloudflare Tunnel Service
After=network.target

[Service]
User=YOUR_USERNAME
ExecStart=[/path/to/cloudflared] tunnel --config [/path/to/your/config.yml] run
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target

Save and close the file (Ctrl+O, Enter, Ctrl+X).

3. Reload systemd configuration

Reload the systemd manager configuration to recognize the new service:

sudo systemctl daemon-reload

4. Enable the service to start at boot

Configure the service to start automatically at system boot:

sudo systemctl enable cloudflared-tunnel.service

5. Start the service

Start the service immediately:

sudo systemctl start cloudflared-tunnel.service

6. Verify service status

Check that the service is running correctly:

sudo systemctl status cloudflared-tunnel.service

Verification

After a system reboot, the Cloudflare Tunnel should start automatically. To confirm:

  1. Reboot the server:
sudo reboot
  1. Once the server is back online, check the service status:
sudo systemctl status cloudflared-tunnel.service
  1. Review the service logs if needed:
    sudo journalctl -u cloudflared-tunnel.service
    

Troubleshooting

  • If the service fails to start, check the configuration file path is correct
  • Verify the user specified in the service file has permissions to run cloudflared
  • Ensure the cloudflared binary is correctly installed at the specified path
  • Check the log files for specific error messages:
    sudo journalctl -u cloudflared-tunnel.service -f
    

Customizing the Service Name

If you're running multiple tunnels, you may want to use more descriptive service names:

sudo nano /etc/systemd/system/cloudflared-TUNNEL_NAME.service

Replace TUNNEL_NAME with an identifier for your specific tunnel.