Effortless Cloudflared Setup on Ubuntu: Set and Forget

Andy

Administrator
Creative Team
User ID
1
Joined
7 Jan 2019
Messages
1,145
Reaction score
58
Points
48
Embrace the simplicity and efficiency of automating your Cloudflared installation on Ubuntu Linux with this definitive guide. Ensure your system consistently runs Cloudflared without the need for manual intervention.

Step-by-Step Cloudflared Service Configuration
Create a Service File:
Kickstart the automation by creating a dedicated system service file for Cloudflared. This configuration allows Ubuntu's systemd to seamlessly manage the service. Begin by crafting the service file with your preferred editor:
Bash:
sudo nano /etc/systemd/system/cloudflared.service

Insert the following settings into your new service file to define how your system should handle Cloudflared:
Bash:
[Unit]
Description=cloudflared
After=network.target

[Service]
TimeoutStartSec=0
Type=notify
ExecStart=/usr/bin/cloudflared --no-autoupdate tunnel run --token [GET_YOUR_OWN_TOKEN_FROM_DASH.CLOUDFLARE.COM]
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

Enable and Start Your Service: Once your file is configured, activate Cloudflared to run at startup, then launch the service immediately:
Bash:
sudo systemctl enable cloudflared.service
sudo systemctl start cloudflared.service

Verify Service Operation: Check that Cloudflared is running smoothly and without issues:
Bash:
sudo systemctl status cloudflared.service

Installation and Updates:
Add Cloudflare Repository Key:
Secure your package installation by adding Cloudflare's GPG key:
Bash:
curl -fsSL [URL]https://pkg.cloudflare.com/cloudflare-main.gpg[/URL] | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

Configure the Cloudflare Repository: Navigate to the keyrings directory and set up the Cloudflare package repository:
Bash:
cd /usr/share/keyrings/ls -alecho 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] [URL]https://pkg.cloudflare.com/cloudflared[/URL] jammy main' | sudo tee /etc/apt/sources.list.d/cloudflared.listls -al /etc/apt/sources.list.d/

Update and Install Cloudflared: Update your system’s package list and install Cloudflared:
Bash:
sudo apt-get update
sudo apt-get install cloudflared
cat /etc/apt/sources.list.d/cloudflared.list

Keep Cloudflared Up-to-Date: Regularly check and apply updates to maintain Cloudflared’s functionality and security:
Bash:
cloudflared update

Managing the Cloudflared Service: To start or stop the Cloudflared service as needed, use these straightforward commands:
Bash:
sudo service cloudflared start
sudo service cloudflared stop

Optimize Your Ubuntu Experience
With these steps, your Ubuntu system will efficiently run Cloudflared, ensuring secure and reliable service operation. By automating Cloudflared, you free yourself to focus on more critical tasks, confident in the continuous performance and security enhancements automatically applied in the background. Enjoy a streamlined, hassle-free Cloudflared experience on Ubuntu!
 
To completely remove, it's super-easy too!
  1. Stop the cloudflared service: If the service is running, you should stop it first.
    Bash:
    sudo systemctl stop cloudflared
    (No output is expected)
  2. Disable the service: This prevents the service from starting at boot time.
    Bash:
    sudo systemctl disable cloudflared
    Output:
    Code:
    Removed /etc/systemd/system/multi-user.target.wants/cloudflared.service.
  3. Remove the cloudflared package: Uninstall the cloudflared software package.
    Bash:
    sudo apt-get remove --purge cloudflared
    Output:
    Code:
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following packages will be REMOVED:
      cloudflared*
    0 upgraded, 0 newly installed, 1 to remove and 17 not upgraded.
    After this operation, 36.5 MB disk space will be freed.
    Do you want to continue? [Y/n] Y
    (Reading database ... 113245 files and directories currently installed.)
    Removing cloudflared (2024.3.0) ...
    Processing triggers for man-db (2.10.2-1) ...
    (Reading database ... 113241 files and directories currently installed.)
    Purging configuration files for cloudflared (2024.3.0) ...
  4. Remove the repository: Delete the Cloudflare repository from your system sources.
    Bash:
    sudo rm /etc/apt/sources.list.d/cloudflared.list -v
    Output:
    Code:
    removed '/etc/apt/sources.list.d/cloudflared.list'
  5. Remove the GPG key: Since you won't be needing the Cloudflare GPG key, you can remove it too.
    Bash:
    sudo rm /usr/share/keyrings/cloudflare-main.gpg -v
    Output:
    Code:
    removed '/usr/share/keyrings/cloudflare-main.gpg'
  6. Clean up your system: Update your package lists and remove any unused packages.
    Bash:
    sudo apt-get update
    sudo apt-get autoremove
    Output for sudo apt-get update:
    Code:
    Get:1 https://downloads.plex.tv/repo/deb public InRelease [6,685 B]
    Get:2 https://downloads.plex.tv/repo/deb public/main amd64 Packages [428 B]
    Hit:3 http://nz.archive.ubuntu.com/ubuntu jammy InRelease
    Get:4 http://nz.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
    Hit:5 http://nz.archive.ubuntu.com/ubuntu jammy-backports InRelease
    Get:6 http://nz.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
    Get:7 http://nz.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1,562 kB]
    Get:8 http://nz.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,074 kB]
    Get:9 http://nz.archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1,346 kB]
    Get:10 http://nz.archive.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [852 kB]
    Fetched 5,071 kB in 3s (1,863 kB/s)
    Reading package lists... Done
    Output for sudo apt-get autoremove:
    Code:
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    0 upgraded, 0 newly installed, 0 to remove and 21 not upgraded.
  7. Remove systemd service file: If there's any leftover systemd service file, you should remove it.
    Bash:
    sudo rm /etc/systemd/system/cloudflared.service -v
    Output:
    Code:
    removed '/etc/systemd/system/cloudflared.service'
  8. Reload systemd: Reload systemd to apply changes and remove any traces of the cloudflared service.
    Bash:
    sudo systemctl daemon-reload
    (No output is expected)
  9. Last but not least, remove the cloudflared config folder:
    Bash:
    rm -rfv /usr/local/etc/cloudflared
    Output:
    Code:
    removed directory '/usr/local/etc/cloudflared'
Following these steps should completely remove cloudflared from your system and undo all related configurations.
 
 Short URL:
Back
Top