Replication using SSH over Cloudflared Tunnels

I am new here but I wanted to share my setup in case its useful to others. I have been using the Cloudflared App for some time now to connect to Frigate and other services running on both of my TrueNAS SCALE servers. I have been pretty happy with it, but the one thing I was really hoping for was to utilise the SSH tunnel functionality of Cloudflared so that my two servers could backup to each other. Previous attempts of doing this have failed, but I have now been able to make it work, meaning that my two servers can securely replicate without needing to do be on the same network or exposed to the internet in a nasty way behind a NAT or port forwarded router.

To do this I used the existing cloudflared app (I have mine with the host network option turned on), and I also created a new app from YAML based on it. You need both. One for connecting out to cloudflare, and one for coming back.

services:
  cloudflared:
    cap_drop:
      - ALL
    command:
      - access
      - tcp
      - '--hostname'
      - othernas-ssh.example.com
      - '--url'
      - 0.0.0.0:2222
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4096M
    environment:
      GID: '568'
      GROUP_ID: '568'
      HOME: /tmp
      NO_AUTOUPDATE: 'true'
      NVIDIA_VISIBLE_DEVICES: void
      PGID: '568'
      PUID: '568'
      TUNNEL_SERVICE_TOKEN_ID: <SERVICE-TOKEN-CLIENT-ID>.access
      TUNNEL_SERVICE_TOKEN_SECRET: <SERVICE-TOKEN-SECRET>
      TZ: Etc/UTC
      UID: '568'
      UMASK: '002'
      UMASK_SET: '002'
      USER_ID: '568'
    group_add:
      - 568
    healthcheck:
      disable: True
    image: cloudflare/cloudflared:latest
    platform: linux/amd64
    ports:
      - '2222:2222'
    privileged: False
    restart: unless-stopped
    security_opt:
      - no-new-privileges=true
    stdin_open: False
    tty: False
    user: '568:568'

The above is the yaml for my custom app. it creates a service running on your TrueNAS server on port 2222 that we will use to connect our SSH to and route ourselves through to the other server via Cloudflare. you will need to change 3 things:

  • hostname: the domain name you have set up in cloudflare to point to your other servers ssh server. If the other server is running the cloudflared app, then navigate to your tunnel in the Cloudflare dashboard and create the public route and point it at ssh://127.0.0.1:22
  • service-token-client-id + secret: navigate to your Cloudflare dashboard and go to Zero Trust > Access Controls > Serivce credentials. Create a new service token. Copy the client-id (including the .access suffix) and secret and paste them into the correct environment variables

After you have the service running, the only thing left to do in cloudflare is set up an application and policy so that your service token can connect to your ssh endpoint.

  • Navifate to Zero Trust > Access controls > Applications.
  • Create a new application
  • set the sub domain and domain to match your hostname above
  • create a new access policy
  • Set the action (on the right side) to be Service Auth
  • Set the Include to be a Service Token and select the token you created earlier
  • Click Save and Save again

The only final thing to do is setup the SSH in your Truenas instance. This is as simple as setting the host to 127.0.0.1 and the port to 22. All of the other configuration around usernames and private keys are the same as any other SSH set up so I won’t go into them here.

If you want both servers replicating to each other, you need to go through the same steps for the other server. I recommend two different service accounts in your Cloudflare dashboard (one for each server) for the best security.

Hopefully that helps others wanting to use Cloudflare tunnels for SSH replication tasks. For my part this means I can move my servers between sites and not have to reconfigure my replication settings. Best of luck to others.

I’m using the regular Cloudflared app for routing to frigate. I have frigate.example.com as a route in my Cloudflare dashboard pointing to https://127.0.0.1:30193 which then resolves to the frigate app in Truenas.

Frigate does require you to disable TLS verify for its Cloudflare route in the dashboard, but most of the other apps I run don’t need that.

The custom access service I wrote about in my post is only needed to allow the SSH client on the box to connect out through cloudflare. Everything else is handled by the normal Cloudflared app just like normal. My service also uses the official cloudflare image as I wanted to avoid building or trusting something third party given the security implications of SSH routing all of my critical data.

Here’s a quick mockup of the setup in cloudflare:

For those wanting more of a walkthrough for the policies needed in Cloudflare, here’s a quick set of screenshots that might be useful:

Creating tokens:

Creating an application for your SSH endpoint:

Creating the access policy for the application:

One other helpful illustration is the set up in Truenas itself: To connect to our remote server’s SSH, we need to use our local port 2222 where the access app is running:

Everything else in the SSH set up is the same. In my original post, I don’t think I quite made it clear how the local settings needed to be changed.

Nice setup. One thing to think about for ransomware is what the source box can do to the backup copy. If the replication login on the target can delete or destroy snapshots, anyone who owns the source can wipe the target through the same tunnel. A dedicated replication user on the target with only receive rights, or a pull task run from the backup box, keeps that from happening. Also keep a longer snapshot retention on the target than on the source, so an old snapshot is still there if the problem sat unnoticed for a few weeks.