How to use the command 'cloudflared' (with examples)
Command-line tool to create a persistent connection to the Cloudflare network.
Use case 1: Authenticate and associate the connection to a domain in the Cloudflare account
Motivation: This use case allows users to authenticate their connection to a domain in their Cloudflare account. By associating the connection to a specific domain, users can ensure that all traffic to and from that domain goes through Cloudflare’s network.
Code:
cloudflared tunnel login
Explanation:
tunnel login
: This argument tellscloudflared
to authenticate the connection.
Example output:
INFO[0000] The server generated a login token for you. Your login link is: https://cloudflare.com
Use case 2: Establish a tunnel to a host in Cloudflare from the local server
Motivation: This use case allows users to establish a secure tunnel between their local server and a host in Cloudflare. By routing their traffic through Cloudflare, users can leverage the benefits of Cloudflare’s network, such as improved security and performance.
Code:
cloudflared tunnel --hostname hostname localhost:port_number
Explanation:
--hostname hostname
: This argument specifies the hostname of the destination host in Cloudflare.localhost:port_number
: This argument specifies the local server and port number that the traffic should be directed to.
Example output:
INFO[0000] Your tunnel ID is xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
INFO[0000] Fingerprint: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Use case 3: Establish a tunnel to a host in Cloudflare from the local server, without verifying the local server’s certificate
Motivation: This use case allows users to establish a tunnel to a host in Cloudflare without verifying the local server’s certificate. This can be useful when dealing with self-signed or invalid certificates on the local server.
Code:
cloudflared tunnel --hostname hostname localhost:port_number --no-tls-verify
Explanation:
--no-tls-verify
: This argument instructscloudflared
to not verify the local server’s certificate.
Example output:
INFO[0000] Your tunnel ID is xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
INFO[0000] Fingerprint: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Use case 4: Save logs to a file
Motivation: This use case allows users to save the logs generated by cloudflared
to a file. This can be useful for debugging or logging purposes.
Code:
cloudflared tunnel --hostname hostname http://localhost:port_number --loglevel panic|fatal|error|warn|info|debug --logfile path/to/file
Explanation:
http://localhost:port_number
: This argument specifies the local server and port number that the traffic should be directed to.--loglevel
: This argument specifies the desired log level. Users can choose from ‘panic’, ‘fatal’, ’error’, ‘warn’, ‘info’, or ‘debug’.--logfile path/to/file
: This argument specifies the path to the file where the logs should be saved.
Example output:
INFO[0000] Logs saved to path/to/file
Use case 5: Install cloudflared as a system service
Motivation: This use case allows users to install cloudflared
as a system service. By doing so, users can ensure that cloudflared
runs automatically on system startup and stays running in the background.
Code:
cloudflared service install
Explanation:
service install
: This argument tellscloudflared
to install itself as a system service.
Example output:
INFO[0000] Successfully installed cloudflared as a system service
Conclusion
The cloudflared
command-line tool provides users with various options to create a persistent connection to the Cloudflare network. Through authentication, establishing tunnels, adjusting log settings, and installing as a system service, users can leverage the benefits of Cloudflare’s network for their applications.