All posts

Secure SSH with Cloudflare Zero Trust – short-lived certificates and no open port 22

Shield between a laptop and servers – port 22 closed

For years every server I ran had port 22 open to the internet. Hardened, key-only, fail2ban on top – but still open. Every log was full of bots knocking. A few months ago I closed it everywhere, and I don’t miss it.

If you’ve ever opened auth.log on a fresh server, you know the feeling. Within minutes there are login attempts for root, admin, ubuntu and – my personal favourite – oracle, on a box that has never seen Oracle in its life. It’s not dangerous if you have done the basics. But it is a constant reminder that your front door is facing a very busy street.

What changed

SSH now goes through Cloudflare Zero Trust. The server runs cloudflared and makes an outbound tunnel to Cloudflare. Nothing listens on a public port. When I connect, Cloudflare Access checks who I am and which device I’m on before a single packet reaches the server.

  • Identity – logging in with my Google Workspace account and MFA.
  • Device posture – the laptop must run WARP and be enrolled in my Zero Trust org.
  • Short-lived certificates – instead of static keys, Access issues an SSH certificate that expires after minutes.

The outbound part is the clever bit. The server calls out to Cloudflare, not the other way around. So the firewall can drop every incoming connection, and the server is basically invisible from the outside. You can’t attack a door you can’t find.

Why short-lived certificates matter

A private key on a laptop lives forever unless you remember to rotate it. A certificate that expires after 15 minutes is worthless if it leaks tomorrow. The server only trusts certificates signed by the Access CA, so there is no authorized_keys file to keep in sync across machines.

Be honest: when did you last rotate your SSH key? If the answer involves the words “I think” or “at my old job”, you are not alone. I had keys older than some of the frameworks I use. Certificates take that whole problem off the table. There is nothing to forget, because nothing lives long enough to be forgotten.

$ nmap -p 22 srv-01.simonduun.com
22/tcp  filtered
$ ssh deploy@srv-01.simonduun.com
✓ Device posture: WARP enrolled
✓ Short-lived cert issued (15m)

How I set it up

Roughly, in the order that caused the fewest tears:

  • Install the tunnel first and test SSH through it while port 22 is still open. Never remove the old door before the new one works.
  • Add an Access application for the SSH hostname with a policy: my identity, MFA and an enrolled device.
  • Enable short-lived certificates and tell sshd to trust the Access CA with TrustedUserCAKeys.
  • Close port 22 in the firewall, and then check from outside that it is really gone.
  • Remove the old keys from authorized_keys. This part is weirdly satisfying.

The first time I did it, I closed the port before testing properly and locked myself out. Do as I say, not as I did.

The trade-offs

You depend on Cloudflare being up, so I keep a break-glass path through the hosting provider’s console. Setting up device posture rules takes an afternoon of trial and error. And CI pipelines need a service token instead of a user login.

There’s also a small learning curve for anyone else who needs access. “Install WARP, log in, then SSH as usual” is easy to say, but the first time always comes with a few “it says access denied?” messages. Write a short guide. Future you will be grateful.

But the result is worth it: no open port, no scanning noise, and access that is tied to a person and a device – not to a key file someone copied three years ago.

And my logs are finally quiet. I almost miss the bots. Almost.