Secure and Scalable Remote Access with Cloudflare TCP Tunnels

In an era where remote connectivity is essential, securing and accessing critical services like databases, remote desktops, and SSH servers has become a priority for businesses and system administrators. Cloudflare Tunnel provides a robust solution for managing TCP tunnels without exposing services directly to the internet, reducing security risks and simplifying management.

This article explores how to configure and optimize a TCP tunnel using Cloudflare, highlighting its security and performance benefits.


Why Use TCP Tunnels with Cloudflare?

Using Cloudflare TCP tunnels allows secure traffic routing without opening firewall ports, reducing attack surfaces. Key advantages include:

  1. Enhanced Security: Protects internal services without exposing them to the internet.
  2. Advanced Authentication: Integrates with Cloudflare Access for multi-factor authentication (MFA) and identity verification.
  3. Versatility: Works with various services, including SSH, RDP, and databases.
  4. Reduced Latency: Cloudflare routes traffic through its global network, improving speed compared to traditional VPNs.
  5. Scalability: Ideal for businesses needing to connect multiple users to internal resources without compromising security.

Use Cases for Cloudflare TCP Tunnels

Cloudflare Tunnel is useful in several scenarios, including:

  • Managing SSH Servers Without Open Ports: Allows administrators to securely connect to Linux servers without exposing port 22.
  • Remote Desktop (RDP) Access: Enables remote employees to securely access internal workstations.
  • Secure Database Connections: Protects databases like PostgreSQL, MySQL, and SQL Server without exposing sensitive ports.
  • Internal Application Access: Connects services securely without requiring a VPN or public IP addresses.

Setting Up a TCP Tunnel with Cloudflare

The following steps guide you through setting up a secure Cloudflare Tunnel for PostgreSQL database access.

1. Install cloudflared

First, install cloudflared, Cloudflare’s tunnel client, on the target server:

sudo apt-get update && sudo apt-get -y install cloudflared

For other operating systems, Cloudflare provides specific packages in its official documentation.

2. Authenticate and Create the Tunnel

After installation, authenticate the server with Cloudflare and register a new tunnel:

cloudflared tunnel login

This opens a browser link where you authorize the connection to your Cloudflare account. Once authenticated, create a new tunnel:

cloudflared tunnel create postgres-tunnel

Cloudflare generates a unique UUID for the tunnel.

3. Configure DNS for Traffic Routing

Set up a subdomain in Cloudflare to correctly route traffic:

cloudflared tunnel route dns <TUNNEL_UUID> database.yourcompany.com

This creates a CNAME record in Cloudflare, directing traffic to the tunnel.

4. Configure the Server-Side Service

Create a configuration file at ~/.cloudflared/config.yaml to define tunnel behavior:

tunnel: <TUNNEL_UUID>
credentials-file: /root/.cloudflared/<TUNNEL_UUID>.json

ingress:
- hostname: database.yourcompany.com
service: tcp://127.0.0.1:5432
- service: http_status:404

This setup ensures connections to database.yourcompany.com are forwarded to port 5432 on the local server.

5. Start the Tunnel as a Linux Service

To ensure the tunnel starts automatically on system boot, install it as a systemd service:

sudo cloudflared --config ~/.cloudflared/config.yaml service install

Verify that the tunnel is running:

systemctl status cloudflared

6. Configure the Client for Remote Connection

On the client machine, install and authenticate cloudflared, then run the following command to establish a secure connection:

cloudflared access tcp --hostname database.yourcompany.com --url 127.0.0.1:5432

Now, you can securely connect to the PostgreSQL database using:

psql -h 127.0.0.1 -p 5432 -U user -d database

Enhancing and Optimizing Your Cloudflare TCP Tunnel

1. Strengthening Security and Authentication

  • Enable Cloudflare Access to restrict access to authorized users.
  • Implement multi-factor authentication (MFA) for additional security.
  • Monitor access logs and traffic for auditing and compliance.

2. Improving Performance

  • Monitor latency using Cloudflare’s analytics tools.
  • Optimize routing rules for enhanced speed and reliability.

3. Automating and Scaling

  • Use scripts to start and stop the tunnel based on demand.
  • Deploy Docker containers to manage multiple tunnels across different servers.

Comparison with Other Remote Access Solutions

FeatureCloudflare TunnelTraditional VPNDirect Exposure
SecurityHigh (encrypted, no open ports)Medium (depends on configuration)Low (ports exposed to the internet)
Ease of UseHigh (simple setup)Low (requires client configuration)High (but insecure)
CostFree for basic useVariable (depends on provider)No extra cost but high risk
LatencyLow (optimized Cloudflare network)Medium (depends on VPN server)Variable (depends on ISP)

Conclusion

Cloudflare TCP Tunnels provide a modern, secure, and efficient way to connect to internal services without exposing them to the internet. Unlike traditional VPNs, Cloudflare Tunnel simplifies remote access management by integrating advanced authentication and security features while maintaining high performance.

For system administrators and enterprises looking for a scalable and reliable remote access solution, Cloudflare Tunnel stands out as a key tool in today’s cybersecurity and connectivity landscape.

Scroll to Top