Enable Cloudflared Tunnel on Linux Boot (Create Services on Linux)

Esteban Suárez

Getting an application to run at system startup is very important, whether we use Windows, Linux, MacOs, services are an essential part to avoid repetitive code and to automate sensitive actions.

To create the service that will run Cloudflared Tunnel formerly known as Argo Tunnel the following steps should be followed.

  1. Create a user with the name cloudflared or any other name depending on the service you want to run. This will be in charge of starting the processes for our software at all times.
sudo useradd -s /usr/sbin/nologin -r -M cloudflared
  1. Create a configuration file with the values ​​that serve as parameters for the execution of cloudflared.
sudo nano /etc/default/cloudflared

Here the file that we use for the configuration of our service is created inside the directory /etc/default/ and the name of it cloudflared the name is the one you choose and in this file you can put more settings of other services that you have running on the system.

Inside the file copy the following data.

# Commandline args for cloudflared, using Cloudflare DNS
CLOUDFLARERED_OPTS=--hostname mydomain.com --url localhost:80

  1. Give the user permission for the configuration file we have created.
    sudo chown cloudflared:cloudflared /etc/default/cloudflared
    sudo chown cloudflared:cloudflared /usr/local/bin/cloudflared
  2. Create the service in the folder /etc/systemd/system with the name you want to give it. In this case cloudflared.service
sudo nano /etc/systemd/system/cloudflared.service
  1. Copy the following code

    [Units]
    Description=cloudflared my local website on the internet
    After=syslog.target network-online.target
    
    [Service]
    type=simple
    User=cloudflared
    EnvironmentFile=/etc/default/cloudflared
    ExecStart=/usr/local/bin/cloudflared $CLOUDFLARERED_OPTS
    restart=on-failure
    RestartSec=10
    KillMode=process
    
    [Install]
    WantedBy=multi-user.target

  2. With our service created, the only thing left for us is to know the commands that will be useful to manage services in Linux.

Enable the service on the system:

sudo systemctl enable cloudflared
Start the service:
sudo systemctl start cloudflared
Show service status:
sudo systemctl status cloudflared
Disable the service:
sudo systemctl disable cloudflared

Now you can enjoy the service to keep your website, blog, app or ftp online.