# SSH security for your servers: keys, a closed port 22, tunnels and alerts

> How to keep SSH safe on a Laravel or PHP server: what Cipi hardens at install, keys done right, app users, brute-force bans, alerts on every login, closing port 22 with Tailscale, NetBird or Cloudflare, SSH tunnels to the database, and a way back in if you lock yourself out.

URL: https://cipi.sh/guides/ssh-security-best-practices · Last updated: 2026-11-30 · By Andrea Pollastri · 8 min read · Category: Security

SSH is how you, your team and sometimes your CI reach a server. Bots try it all day long: within minutes of a VPS going online, its log fills with guesses for `root`, `admin` and `ubuntu`. On a Cipi server those guesses can't work — but there is still a lot you can do to make SSH *quiet*, *auditable* and, in the end, **invisible to the internet**.

## Key ideas, in plain words

| Term | Meaning |
| --- | --- |
| **SSH key** | A pair of files: the *private* key stays on your laptop, the *public* key goes on the server. Whoever has the private key gets in |
| **Passphrase** | A password that encrypts the private key on your disk, so a stolen laptop isn't a stolen server |
| **`cipi` user** | The admin entry point. You log in as `cipi` and run `sudo cipi …`; root can't log in over SSH |
| **App user** | Each app is a Linux user (`shop`, `blog`). It can get SSH or SFTP of its own, per app |
| **Fail2ban** | Bans an address that keeps failing logins, a little longer each time |
| **Port 22** | Where SSH listens. Open to the world by default, so you can log in after install |
| **Lock** | Closing port 22 to the world while it stays open on a private network (Tailscale, NetBird) or through Cloudflare |
| **SSH tunnel** | Using an SSH connection to reach a service that is closed to the internet, such as the database |

1. **Keys only** — done at install
   No passwords, no root login, three tries per connection: guessing is useless from the first minute.
2. **Your keys, cared for** — one per person
   Ed25519 with a passphrase, a name on every key, removed when someone leaves.
3. **App users** — SSH per app
   SFTP only for the apps that need it, admin keys for everything else.
4. **Bans and alerts** — know who knocks, and who gets in
   Fail2ban bans guessers; a notification for every login, sudo and key change.
5. **Close port 22** — reachable from your devices only
   Tailscale, NetBird or Cloudflare carry SSH; port 22 stops answering the internet.
6. **Tunnels** — the database without open ports
   TablePlus, DBeaver and your scripts reach MariaDB or PostgreSQL through SSH.

## What Cipi already did at install

You don't have to touch `sshd_config`. The installer writes:

| Setting | Value | What it means |
| --- | --- | --- |
| `PermitRootLogin` | `no` | Nobody logs in as root over SSH |
| `PasswordAuthentication` | `no` | Keys only for the admin user: there is no password to guess |
| `PubkeyAuthentication` | `yes` | Keys are how you get in |
| `PermitEmptyPasswords` | `no` | — |
| `MaxAuthTries` | `3` | Three attempts per connection, then it's cut |
| `LoginGraceTime` | `20` | Twenty seconds to finish logging in |
| `X11Forwarding` | `no` | No graphical forwarding |
| `AllowGroups` | `cipi-ssh cipi-apps` | Only the admin user and app users may log in at all |

Plus **UFW** (only SSH, 80 and 443 open), **Fail2ban** on SSH, and a key-only `cipi` user that runs exactly one thing as root without a password: the `cipi` command. Check it any time:

```bash
$ sudo cipi firewall status        # what is open, who holds each port
$ su root                          # a root shell (the root password is in the install summary)
# sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|maxauthtries|allowgroups)'
```

> [!NOTE]
> Edit `/etc/ssh/sshd_config` only above Cipi's managed block (`# >>> cipi ssh apps`): Cipi rewrites that block on updates, and validates every change it makes with `sshd -t` before reloading, so a typo never replaces a working configuration.

## 1. Use good keys, and look after them

On your laptop:

```bash
$ ssh-keygen -t ed25519 -C "andrea@laptop"     # set a passphrase when asked
$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519   # macOS: unlock once per session (Linux: ssh-add)
$ cat ~/.ssh/id_ed25519.pub                    # the public key, the one you give the server
```

On the server, one key **per person and per device**, each with a name you'll recognise in a year:

```bash
$ sudo cipi ssh add "ssh-ed25519 AAAAC3Nz… mario@workstation"
$ sudo cipi ssh list                  # keys, fingerprints, the one you're using now
$ sudo cipi ssh rename 2 "mario-workstation"
$ sudo cipi ssh remove 2              # Mario left: his key goes, for every app at once
```

| Guard | Why it matters |
| --- | --- |
| You can't remove the key of your own session | No accidental lock-out |
| You can't remove the last key | The server always has a way in |
| Format and duplicate checks | No broken or repeated keys |
| A notification for every add, remove and rename | A key that appears out of nowhere is noticed |

Good habits that cost nothing:

- **Never share a private key.** Two people, two keys: when one leaves, only theirs goes.
- **Don't forward your agent** (`ForwardAgent yes`) to servers. To hop through one machine, use `ProxyJump` instead.
- **Check the server's fingerprint** the first time you connect. Read it once from the provider's console: `ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub`.
- Give your laptop a short `~/.ssh/config`, so the right key is always used:

```text
Host prod
  HostName 203.0.113.10
  User cipi
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes
```

Then `ssh prod`, and `sudo cipi status`.

## 2. App users: SSH and SFTP only where needed

Every app is its own Linux user, and the server's admin keys always open a shell as any of them (`ssh shop@server`). Whether an app *also* accepts its own password and keys from outside — for a client's SFTP upload, for example — is decided per app:

```bash
$ sudo cipi ssh apps                  # every app: enabled or disabled
$ sudo cipi ssh apps enable blog      # the client uploads to blog over SFTP
$ sudo cipi ssh apps disable blog     # done: admin keys only again
$ sudo cipi ssh apps disable --all    # every app, and new apps from now on
```

New servers create apps **disabled**. Deploys are never affected: they connect to `localhost`. Password reset for an app user, when you do enable it: `sudo cipi app reset-password blog`. Details: [App users' SSH and SFTP](https://cipi.sh/docs/ssh-keys#cli-ssh-apps).

## 3. Let Fail2ban (and CrowdSec) deal with guessers

Fail2ban bans an address after **3 failed logins within an hour**, and a little longer each time it comes back. Tune it, and look at who's banned:

```bash
$ sudo cipi firewall attempts         # the current threshold
$ sudo cipi firewall attempts 5       # more forgiving, for a team on shared IPs
$ sudo cipi ban list                  # who is banned now (Fail2ban and CrowdSec together)
$ sudo cipi ban unban 198.51.100.23   # a colleague who fat-fingered a passphrase
```

For the web *and* SSH, [CrowdSec](https://cipi.sh/guides/crowdsec-laravel-server) adds behaviour detection and a community blocklist of known attackers, with your own SSH session allowlisted before it starts. The panel's [Firewall tab](https://cipi.sh/docs/firewall#panel) shows UFW, Fail2ban and CrowdSec together, each ban with *Unban*.

## 4. Be told about every login

A ban says someone knocked. A **login alert** says someone got in — and that's the one you want on your phone. Cipi sends a security notification for:

| Trigger | When |
| --- | --- |
| `ssh_login` | An SSH login of `cipi`, root or a sudoer — with source IP and key fingerprint |
| `sudo`, `su` | Someone became root |
| `ssh_key_add`, `ssh_key_remove`, `ssh_key_rename` | The keys that open the server changed |
| `credentials_show` | Someone printed or exported the server's credentials |

Send them where you'll see them:

```bash
$ sudo cipi notification add telegram oncall --token=<bot-token> --chat-id=<chat-id>
$ sudo cipi notification events oncall urgent    # failures and security events only
$ sudo cipi notification test oncall
```

An alert for a login you didn't make is your cue: remove the key (`sudo cipi ssh remove`), check `sudo cipi ssh list`, and read [Notifications](https://cipi.sh/docs/notifications) for routing everything else. Step by step: [Server notifications done right](https://cipi.sh/guides/notifications-alerts).

## 5. Keep CI off SSH

A CI runner with an SSH key for `cipi` can run *any* `cipi` command. Most pipelines only need to deploy, so give them exactly that:

```bash
$ sudo cipi api token create --name=github-actions --abilities=deploy-manage --expires=2027-06-30
```

The pipeline calls the [REST API](https://cipi.sh/docs/api) over HTTPS with that token: no SSH key on the runner, nothing to steal that could do more than a deploy. Or skip CI access altogether and [deploy on push](https://cipi.sh/docs/auto-deploy) with the Cipi Agent's signed webhook. Recipes for both: [CI/CD pipelines](https://cipi.sh/docs/ci-pipelines).

## 6. Close port 22 to the internet

With keys only and Fail2ban, port 22 is safe — but it is still *there*, for every scanner and for the next OpenSSH vulnerability. The best SSH port is one the internet can't reach. Cipi gives you two ways, and both keep port 22 itself: only *who* can reach it changes.

| | Tailscale or NetBird (mesh) | Cloudflare Tunnel + Access |
| --- | --- | --- |
| How you connect | `ssh cipi@100.101.102.103` from any device on your private network | `ssh cipi@ssh.example.com`, through Cloudflare, after signing in |
| What you install on your laptop | The Tailscale or NetBird client | `cloudflared` |
| Second factor | Your identity provider's login for the network; optional Tailscale SSH | Cloudflare Access login, optional short-lived certificates instead of keys |
| Best for | You and your team reaching servers | Teams already on Cloudflare; SSH next to sites published through the tunnel |

Which one fits, in detail: [Cloudflare Zero Trust vs Tailscale vs NetBird](https://cipi.sh/guides/cloudflare-zero-trust-vs-tailscale-netbird).

### With Tailscale or NetBird

```bash
$ sudo cipi zt tailscale enable --auth-key=tskey-auth-…    # or: sudo cipi zt netbird enable --setup-key=…
$ sudo cipi zt tailscale status                            # the server's address on the network
```

1. **Join** — the server gets a private address
   It closes nothing: port 22 still answers the world.
2. **Prove it** — log in over the network
   From a laptop on the same network: `ssh cipi@100.101.102.103`. That login is the proof the way in works.
3. **Lock SSH** — port 22 from the network only
   From that session: `sudo cipi zt tailscale lock ssh`. Cipi refuses until it has seen a login over the network.

```bash
$ ssh cipi@100.101.102.103                # from your laptop, on the tailnet
$ sudo cipi zt tailscale lock ssh         # port 22 now answers on the mesh only
$ sudo cipi zt tailscale unlock ssh       # back to the world, if you ever need it
```

Full walkthrough: [Reach your servers over Tailscale or NetBird](https://cipi.sh/guides/tailscale-netbird-private-server-access).

### With Cloudflare

```bash
$ sudo cipi zt ssh enable --hostname=ssh.example.com   # SSH through the tunnel, behind Access
```

On your laptop, `~/.ssh/config` (the command prints it for you):

```text
Host ssh.example.com
  ProxyCommand cloudflared access ssh --hostname %h
  User cipi
  IdentityFile ~/.ssh/id_ed25519
```

Then `ssh ssh.example.com` — a browser tab asks you to sign in once — and only when that works:

```bash
$ sudo cipi zt lock ssh --yes      # port 22 closed to the world
```

Want to stop managing keys altogether? **Short-lived certificates**: Access signs a certificate for each login, valid for minutes, tied to the person's email.

```bash
$ sudo cipi zt ssh cert enable --principals=andrea,mario
$ cloudflared access ssh-config --hostname ssh.example.com --short-lived-cert >> ~/.ssh/config   # on your laptop
```

Full walkthrough: [Put a Laravel server behind Cloudflare Zero Trust](https://cipi.sh/guides/cloudflare-zero-trust-laravel).

> [!WARNING]
> Lock SSH only from a session that already came **through** the new way in, and keep your provider's web console at hand. `sudo cipi firewall` refuses to reopen a port a lock holds, and names the command that does.

## 7. Reach the database through SSH, not through an open port

Never open 3306 or 5432 to the internet. Your SSH connection can carry the database instead:

```bash
# a local port 3307 that leads to MariaDB on the server
$ ssh -N -L 3307:127.0.0.1:3306 prod
# in another terminal, or in your database client
$ mysql -h 127.0.0.1 -P 3307 -u shop -p shop
```

TablePlus, DBeaver, Sequel Ace and DataGrip do the same in their *Over SSH* option: SSH host `prod` (or the mesh address), database host `127.0.0.1`, the app's database user and password. With port 22 locked to a mesh, the tunnel simply runs over the mesh. Behind Cloudflare, `sudo cipi zt tcp add mysql --hostname=db.example.com` puts the database behind Access instead, and `cloudflared access tcp` gives you the local port ([TCP services](https://cipi.sh/docs/zero-trust)).

For a copy of production on your laptop without anyone's personal data, use the [anonymizer](https://cipi.sh/guides/anonymized-database-dumps) rather than a raw dump.

## 8. Plan your way back in

| You lost… | Way back |
| --- | --- |
| Your IP (banned) | Wait for the ban to expire, or connect from another network and `sudo cipi ban unban <ip>` |
| Your IP, with CrowdSec on | The rescue command you stored off the server: `sudo cipi crowdsec rescue token` prints it ([rescue](https://cipi.sh/docs/crowdsec#rescue)) |
| Your laptop's key | A teammate's key, or the provider's web console; then `sudo cipi ssh add` a new key and `sudo cipi ssh remove` the old one |
| The mesh or Cloudflare | The provider's web console, then `sudo cipi zt tailscale unlock ssh` or `sudo cipi zt ssh unlock` |

Write down where the provider's console is (Hetzner, DigitalOcean, AWS all have one) before you need it.

## Checklist

| | Check |
| --- | --- |
| ☐ | Ed25519 keys with a passphrase, one per person and device, named (`sudo cipi ssh list`) |
| ☐ | Former team members' keys removed |
| ☐ | App users' SSH disabled where nobody needs SFTP (`sudo cipi ssh apps`) |
| ☐ | Login and key alerts on a channel you read (`sudo cipi notification events`) |
| ☐ | CI deploys with an API token that can only deploy, not with an SSH key |
| ☐ | Port 22 locked to Tailscale, NetBird or Cloudflare |
| ☐ | The database reached through SSH, never on an open port |
| ☐ | The provider's web console tested once |

## Troubleshooting

| Problem | Fix |
| --- | --- |
| `Permission denied (publickey)` | The key on your laptop isn't on the server: `ssh -v` shows which key is offered. Add the right one with `sudo cipi ssh add` from a working session |
| Asked for a password as `cipi` | You're offering no key: check `IdentityFile` and `ssh-add -l` |
| Connection times out after a few tries | Fail2ban banned your IP: from elsewhere, `sudo cipi ban list` and `sudo cipi ban unban <ip>` |
| `lock ssh` refused | Cipi hasn't seen a login over the mesh or the tunnel yet: log in that way first, from that session lock |
| An app user can't log in with its password | SSH for that app is disabled: `sudo cipi ssh apps enable <app>` |
| SFTP client fails but SSH works | Use the app user (`blog@server`), not `cipi`, and the path `/home/blog/htdocs` |

## Reference docs

- [SSH keys](https://cipi.sh/docs/ssh-keys)
- [Firewall & bans](https://cipi.sh/docs/firewall)
- [Tailscale & NetBird](https://cipi.sh/docs/mesh-network)
- [Cloudflare Zero Trust](https://cipi.sh/docs/zero-trust)
- [Notifications](https://cipi.sh/docs/notifications)
- [Security model](https://cipi.sh/docs/security-model)

## Questions

### Is SSH on port 22 unsafe?

Not with keys only, which is how Cipi sets it up: bots knock all day and get nowhere. The risk left is the noise, a stolen key and the next OpenSSH bug. Closing port 22 to the world with a private network or Cloudflare removes all three from the internet.

### Should I move SSH to another port?

It cuts log noise and nothing else: a port scan finds it in seconds. Cipi doesn't change the port for you. If you want SSH off the internet, close it with a lock (step 6): the port stays 22, and only your devices can reach it.

### Can I log in as root?

Not over SSH: the installer sets `PermitRootLogin no`. Log in as `cipi` and type `sudo cipi …`; for a root shell, `su root` with the root password from the install summary.

### Can I use a password instead of a key?

Not for the admin user: passwords are off (`PasswordAuthentication no`). An app user can get a password for SFTP if you allow it per app (`sudo cipi ssh apps enable shop`).

### Which key type?

Ed25519, protected by a passphrase. `cipi ssh add` takes `ssh-ed25519`, `ecdsa-sha2-*` and `ssh-rsa`; for a second factor use Cloudflare's short-lived certificates or Tailscale SSH (step 6).

### How do I know someone logged in?

Every SSH login of `cipi`, root or a sudoer, every `sudo` and `su`, and every key added or removed is a security notification, with source IP and key fingerprint. Send them to Telegram or Slack with `sudo cipi notification events <channel> urgent`.

### I banned myself. What now?

Wait for the Fail2ban ban to expire, connect from another network, use the CrowdSec rescue if you enabled CrowdSec, or open your provider's web console and run `sudo cipi ban unban <your-ip>`.

### Do deploys need port 22?

No. Webhook deploys run on the server, and deploys from CI can go through the REST API with a token that may only deploy. Deploys use SSH to `localhost`, which no lock closes.

---

Index of every page: https://cipi.sh/llms.txt · Everything in one file: https://cipi.sh/llms-full.txt · Source: https://github.com/cipi-sh/cipi (MIT)
