# Command-line reference

> Every postern-hub command, subcommand and flag, with usage, prompts, output, exit codes and an example, and how to run them for each install method.

Source: https://docs.posternmail.app/reference/cli/

This page lists every command of the `postern-hub` program: what it does, its flags, what it
prints and how it exits. The Hub's settings are described in [Configuration](https://docs.posternmail.app/configuration/).

## How to run a command

Every command reads the same configuration as the running Hub (data directory, key file,
passphrase), so run it with the same environment.

**Docker Compose:**
Run commands inside the Hub's container. The container must be running:

```sh
cd ~/postern && docker compose exec hub postern-hub doctor
```

Commands that need the Hub stopped (`restore`, `rotate-key`, `keygen --force`,
`import-mailmanager`) run in a one-off container instead. `docker compose run` starts the same
image with the same volumes and environment, but does not publish the port:

```sh
cd ~/postern && docker compose stop hub
docker compose run --rm hub restore --from /data/backups/backup-20261205-030000
docker compose up -d
```

The image's entry point is `postern-hub`, so after `run --rm hub` you write only the command.
Paths are paths inside the container: the data directory is `/data`, the key file
`/key/hub.key`. The old name `mailclient-hub` also works in the image.
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Run commands as the `postern` user with the variables from `hub.env` loaded:

```sh
sudo -u postern sh -c 'set -a; . /etc/postern/hub.env; exec postern-hub doctor'
```

`set -a` exports every variable the file sets. For a command that needs the Hub stopped:

```sh
sudo systemctl stop postern-hub
sudo -u postern sh -c 'set -a; . /etc/postern/hub.env; exec postern-hub restore --from /var/lib/postern/backups/backup-20261205-030000'
sudo systemctl start postern-hub
```

Running a command as `root` instead creates files owned by `root` in the data directory, which
the service then cannot read. Always use `sudo -u postern`.
**Desktop app (built-in Hub):**
The desktop app has no command line for its built-in Hub. Setup, users and settings work in
the app itself. See [Desktop app](https://docs.posternmail.app/apps/desktop/).

If you need a command such as `doctor` or `backup` anyway, quit the app first, then run the
binary bundled with it (`hub` in the `hub` folder of the app's resources, `hub.exe` on Windows)
with the app's folders:

```sh
cd ~/Library/Application\ Support/Postern
/Applications/Postern.app/Contents/Resources/hub/hub doctor --data-dir hub-data --key-file hub-key/hub.key
```

On Linux the folder is `~/.config/Postern`, on Windows `%APPDATA%\Postern`.

Throughout this page, examples show the bare command (`postern-hub doctor`). Put the prefix for
your install method in front.

## Command line rules

```text
postern-hub [command] [arguments] [global flags]
```

- The command comes first. Without one (or when the first argument starts with `-`) the
  command is `serve`.
- Global flags may come anywhere **after** the command. `postern-hub --data-dir /srv/d doctor`
  fails with `error: unexpected arguments: [doctor]` because `--data-dir` made the command
  `serve`. Write `postern-hub doctor --data-dir /srv/d`.
- Flags work as `--flag value` and `--flag=value`. A single dash (`-flag`) works too.
- `-h` or `--help` anywhere prints the general usage and exits with 0. There is no separate help
  per command; this page is it.
- In a source checkout the binary may be called `hub` (`go run ./cmd/hub doctor`); some
  messages call the program `hub` for that reason.

### Exit codes

| Code | Meaning |
|---|---|
| `0` | Success. `doctor` with warnings only also exits 0. |
| `1` | Any error. The message is printed on standard error as `error: <message>`. |
| `3` | `engine` only: the Hub signed this device out. |
| `4` | `engine` only: another engine already runs on the data directory. |

### Output symbols

Commands print their results on standard output with these markers: `✓` done or fine, `!`
something to note or a warning, `✗` a problem (`doctor`), and indented lines for details.

## Global flags

These work with every command except `version` and `help`. Each one has an environment variable
([Configuration](https://docs.posternmail.app/configuration/#core)); the flag wins.

| Flag | Variable | Default | Meaning |
|---|---|---|---|
| `--data-dir DIR` | `POSTERN_DATA_DIR` | `./data` | Data directory. |
| `--listen ADDR` | `POSTERN_LISTEN` | `:8080` | Listen address (`serve`, `engine`). |
| `--public-url URL` | `POSTERN_PUBLIC_URL` | empty | Public URL of the Hub, like `https://mail.example.com`. |
| `--key-file PATH` | `POSTERN_KEY_FILE` | `<data dir>/hub.key` | Master key file. |
| `--log-level LEVEL` | `POSTERN_LOG_LEVEL` | `info` | `debug`, `info`, `warn`, `error`. |
| `--dev` | `POSTERN_DEV` | off | Development mode. `--dev=false` (or anything but `true`/`1`) turns it off. |

A global flag at the end without a value fails with `error: flag --listen needs a value`.

## Commands at a glance

| Command | Hub must be stopped | What it does |
|---|---|---|
| [`serve`](https://docs.posternmail.app/reference/cli/#serve) | | Run the Hub (the default). |
| [`setup-code`](https://docs.posternmail.app/reference/cli/#setup-code) | no | Print the one-time code for the first-run setup. |
| [`doctor`](https://docs.posternmail.app/reference/cli/#doctor) | no | Check configuration, data, key, network, providers and backups. |
| [`admin create-user`](https://docs.posternmail.app/reference/cli/#admin-create-user) | no | Create a user. |
| [`admin list-users`](https://docs.posternmail.app/reference/cli/#admin-list-users) | no | List users. |
| [`admin reset-password`](https://docs.posternmail.app/reference/cli/#admin-reset-password) | no | Set a new password and sign the user out everywhere. |
| [`admin disable-2fa`](https://docs.posternmail.app/reference/cli/#admin-disable-2fa) | no | Turn off TOTP two-factor sign-in for a user. |
| [`admin delete-user`](https://docs.posternmail.app/reference/cli/#admin-delete-user) | no | Delete a user and all their data. |
| [`admin invite`](https://docs.posternmail.app/reference/cli/#admin-invite) | no | Create an invite link. |
| [`backup`](https://docs.posternmail.app/reference/cli/#backup) | no | Write a consistent backup. |
| [`restore`](https://docs.posternmail.app/reference/cli/#restore) | **yes** | Restore a backup. |
| [`keygen`](https://docs.posternmail.app/reference/cli/#keygen) | yes, when replacing a key | Create a master key file. |
| [`rotate-key`](https://docs.posternmail.app/reference/cli/#rotate-key) | **yes** | Re-encrypt everything for a new master key. |
| [`import-mailmanager`](https://docs.posternmail.app/reference/cli/#import-mailmanager) | **yes** | Import MailManager's rules. |
| [`engine`](https://docs.posternmail.app/reference/cli/#engine) | | Run a device mail engine (used by the desktop app). |
| [`version`](https://docs.posternmail.app/reference/cli/#version) | no | Print the version. |
| [`help`](https://docs.posternmail.app/reference/cli/#help) | no | Print the usage. |

Commands marked "no" can run next to a running Hub: they open the database directly, and the
Hub picks up their changes.

## serve

```text
postern-hub [serve] [global flags]
```

Runs the Hub until it receives `SIGINT` or `SIGTERM` (or, with `POSTERN_EXIT_ON_STDIN_CLOSE=1`,
until its standard input closes). It takes no arguments of its own: anything else fails with
`error: unexpected arguments: [...]`.

At start it creates the data directory and the key file if missing, applies database migrations
(after writing a `pre-migrate-<time>.db` backup when the database already existed), locks
`<data dir>/hub.lock` and logs to standard error:

```text
time=2026-12-05T16:35:16.706+01:00 level=INFO msg=starting version="Postern Hub 0.1.0 linux/amd64 go1.27.1" data=/var/lib/postern listen=127.0.0.1:8080
time=2026-12-05T16:35:16.729+01:00 level=WARN msg="This Hub is not set up yet. Open it in a browser and enter the setup code 4C3S-J3Q7-KVXD (also shown by 'postern-hub setup-code')." subsystem=platform setup_code=4C3S-J3Q7-KVXD
time=2026-12-05T16:35:16.730+01:00 level=INFO msg=listening addr=127.0.0.1:8080
```

The setup-code line appears only while the Hub has no users. On shutdown it logs
`shutting down` and stops within the stop timeout (30 s in the unit and in `compose.yaml`).

If another Hub already uses the data directory, it exits with
`error: the data directory is in use by a running Hub (/var/lib/postern/hub.lock)`.

Example (a Hub for local testing, data in `./data`):

```sh
postern-hub serve --listen 127.0.0.1:8080 --data-dir ./data
```

## setup-code

```text
postern-hub setup-code
```

Prints the one-time code that the setup page asks for when you create the first administrator
from another computer. A browser on the Hub's own machine, connecting directly to the Hub (no
proxy in between), needs no code. The code is stored in `<data dir>/setup-code` and deleted
once the Hub has a user. If no code exists yet, this command creates one.

Output (the code on standard output, the hint on standard error):

```text
4C3S-J3Q7-KVXD
Enter this code on your Hub's setup page to create the administrator account.
```

When the Hub already has users:

```text
This Hub is already set up: no setup code is needed. Sign in, or create users with 'postern-hub admin create-user'.
```

On the setup page, letters are compared without regard to case, spaces or dashes. See
[Setup](https://docs.posternmail.app/setup/).

## doctor

```text
postern-hub doctor
```

Checks the installation and prints one line per check. It never changes anything: the database
is opened read-only. Sections:

| Section | Checks |
|---|---|
| (first line) | The version. Then a warning for each legacy `MC_*` variable shadowed by a different `POSTERN_*` value. |
| Data directory | Exists, is a directory, mode (warns if other users can read it), writable, whether a Hub is running on it, free disk space (problem under 512 MiB, warning under 2 GiB). |
| Database | Readable by other users (warning), `PRAGMA integrity_check`, applied and pending schema migrations (for databases up to 512 MB). |
| Master key | Exists (a problem if it is missing but a database exists), mode (a problem if others can read it), plain or sealed, unlocks with `POSTERN_KEY_PASSPHRASE`, and decrypts this database's secrets. |
| Setup | Only while there are no users: shows the setup code. |
| Network | `POSTERN_PUBLIC_URL` set and HTTPS; fetches `<public URL>/api/v1/info` (through Cloudflare Access with the configured service token); clock difference to that server; TLS certificate expiry (warning under 14 days); `POSTERN_TLS_CERT` file; push relay health. |
| Sign-in & providers | Google and Microsoft app credentials from the environment; OIDC issuer reachable. |
| Backups | Schedule, newest backup (warning when older than twice the interval plus an hour). |

The last line sums up: `Everything looks good.`, `No problems, 3 warning(s).` or
`2 problem(s), 1 warning(s).` With problems, the exit code is 1 and the last line on standard
error is `error: doctor found 2 problem(s)`.

Example output on a new machine:

```text
Postern Hub 0.1.0 linux/amd64 go1.27.1

Data directory
! /var/lib/postern does not exist yet (the Hub creates it on first start)

Database
! no database at /var/lib/postern/hub.db yet (created on first start)

Master key
! no key file at /var/lib/postern-key/hub.key yet (created on first start)

Setup
! this Hub is not set up yet: run 'postern-hub setup-code' (or start the Hub) for the setup code

Network
! POSTERN_PUBLIC_URL is not set: invite and pairing links use whatever address a browser used, and passkeys only work on localhost
  Push relay disabled (POSTERN_RELAY_URL not set): mobile apps rely on background refresh or UnifiedPush.

Sign-in & providers
  Google OAuth not configured via environment (can also be set in the admin UI)
  Microsoft client not configured via environment (can also be set in the admin UI)
  Single sign-on (OIDC) not configured

Backups
✓ scheduled backups every 24h0m0s, keeping 7, in /var/lib/postern/backups
  No backups yet (run: hub backup)

No problems, 5 warning(s).
```

Every message and what to do about it: [Doctor](https://docs.posternmail.app/admin/doctor/).

## admin create-user

```text
postern-hub admin create-user --email EMAIL --name NAME [--password PASSWORD] [--admin]
```

| Flag | Meaning |
|---|---|
| `--email` | Sign-in address (required). |
| `--name` | Display name (required, at most 100 characters). |
| `--password` | Password, at least 10 characters. Without it a strong one is generated and printed. |
| `--admin` | Make the user an administrator. The first user of a Hub is always an administrator. |

```sh
postern-hub admin create-user --email you@example.com --name "Alex Example" --admin
```

```text
✓ Created admin you@example.com (usr_01m93dh1ewdkz3ekf2wkfwgacp)
  Password: XBLVZ-ADf4E-gZwM2-PW2Nh
  Ask the user to change it after signing in.
```

A password given with `--password` is visible in your shell history and process list; prefer
the generated one. Errors: `--email: enter a valid email address`, `--name is required` (also
for a name longer than 100 characters), `--password must be at least 10 characters`, and a
message when the address is already in use.

## admin list-users

```text
postern-hub admin list-users
```

```text
EMAIL            NAME          ROLE   2FA   STATUS  CREATED     ID
you@example.com  Alex Example  admin  totp  active  2026-12-05  usr_01m93dh1ewdkz3ekf2wkfwgacp
sam@example.com  Sam Example   member -     active  2026-12-06  usr_01m93dh6pfazf9rtc9v71trxas
```

`2FA` is `totp` or `-`. `STATUS` is `active`, `disabled` or `deleting` (deletion scheduled).
With no users it prints
`No users yet. Open the web app to run the setup, or: hub admin create-user --admin ...`.

## admin reset-password

```text
postern-hub admin reset-password --email EMAIL [--password PASSWORD]
```

Sets a new password, signs the user out on every device and records the event in their
security log. Without `--password` a temporary password is generated:

```sh
postern-hub admin reset-password --email sam@example.com
```

```text
✓ Password reset for sam@example.com; all of their devices were signed out.
  Temporary password: Qjwz7-Uvq5B-jxJ33-2tXKS
```

Errors: `--email is required`, `no user with email sam@example.com`,
`--password must be at least 10 characters`.

## admin disable-2fa

```text
postern-hub admin disable-2fa --email EMAIL
```

Turns off TOTP two-factor sign-in and deletes the user's recovery codes, for someone who lost
their authenticator. Passkeys are not touched. The user can set up TOTP again in **Settings →
Security**.

```text
✓ Two-factor authentication disabled for sam@example.com
```

## admin delete-user

```text
postern-hub admin delete-user --email EMAIL [--yes]
```

Deletes a user with all their mailboxes, mail and settings. Nothing is deleted at the mail
providers. In a terminal it asks first:

```text
Delete sam@example.com and ALL of their mailboxes, mail and settings? This cannot be undone. [y/N]
```

Only `y` or `yes` continues. Without a terminal (scripts, `docker compose exec -T`) it stops with
`error: aborted (pass --yes to confirm non-interactively)` unless you pass `--yes`.

The command signs the user out everywhere and marks the account for deletion; the Hub process
deletes the data:

```text
✓ sam@example.com is signed out everywhere and scheduled for deletion.
  The running Hub deletes the account and its data within a minute.
```

When no Hub is running the second line reads
`The Hub deletes the account and its data when it next starts.` You cannot delete the only
administrator while other users exist:
`This is the only administrator. Make another user an administrator first.`

## admin invite

```text
postern-hub admin invite [--email EMAIL] [--role member|admin]
```

Creates an invite link, valid for 7 days. `--email` limits it to one address; `--role` defaults
to `member`.

```sh
postern-hub admin invite --email sam@example.com
```

```text
✓ Invite created (member, valid until 2026-12-12T15:35Z)
  https://mail.example.com/invite/PIK_BKtC5hSa91wxJCwx
```

Without `POSTERN_PUBLIC_URL` it prints the path instead:

```text
  Open <your Hub address>/invite/_Tbg_VuHNwGA0GMb-1Kh
  (set POSTERN_PUBLIC_URL to get a full link)
```

Errors: `--role must be member or admin`, `--email: enter a valid email address`. See
[Users](https://docs.posternmail.app/admin/users/).

## backup

```text
postern-hub backup [--out DIR]
```

Writes a consistent backup while the Hub keeps running: a directory
`backup-YYYYMMDD-HHMMSS` (UTC) with `hub.db`, `blobs/` and `manifest.json`.

| Flag | Meaning |
|---|---|
| `--out DIR` | Write into this directory instead of `POSTERN_BACKUP_DIR`. Old backups are pruned (`POSTERN_BACKUP_KEEP`) only when `--out` is not given. |

```sh
postern-hub backup
```

```text
✓ Backup written to /var/lib/postern/backups/backup-20261205-153524 (957.0 KiB)
! The master key file /var/lib/postern-key/hub.key is NOT included. Back it up separately: without it the backup cannot be decrypted.
```

Fails with `no Hub database at /var/lib/postern/hub.db` before the first start. See
[Backups](https://docs.posternmail.app/admin/backups/).

## restore

```text
postern-hub restore --from PATH [--yes]
```

Replaces the database and blobs with a backup. **Stop the Hub first**; otherwise:
`error: the data directory is in use by a running Hub: stop the Hub before restoring`.

| Flag | Meaning |
|---|---|
| `--from PATH` | A backup directory (`backup-...`), or a bare database file ending in `.db` such as `pre-migrate-20261205-030000.db` (then only the database is restored). Required. |
| `--yes` | Do not ask. Without a terminal, `--yes` is required. |

It asks `Replace the Hub data in /var/lib/postern with <path>? [y/N]`, checks the backup's
integrity, moves the current `hub.db` (with `-wal` and `-shm`) and `blobs/` into
`<data dir>/pre-restore-<time>/`, then copies the backup in:

```text
✓ Restored /var/lib/postern/backups/backup-20261205-153524
  The previous database and blobs were kept in /var/lib/postern/pre-restore-20261205-153543
  Make sure /var/lib/postern-key/hub.key is the key file that belongs to this backup.
```

Errors: `--from is required`, `<path> does not look like a backup (no hub.db)`,
`backup database is damaged: ...`. Delete the `pre-restore-*` directory once the restored Hub
works.

## keygen

```text
postern-hub keygen [--sealed] [--force]
```

Writes a new master key file at `POSTERN_KEY_FILE`. You rarely need it: the Hub creates a plain
key file on first start. Use it to create a **sealed** key before the first start.

| Flag | Meaning |
|---|---|
| `--sealed` | Protect the key file with a passphrase: `POSTERN_KEY_PASSPHRASE` if set (at least 12 characters), otherwise it prompts twice in the terminal. The Hub then needs `POSTERN_KEY_PASSPHRASE` at every start. |
| `--force` | Overwrite an existing key file. The old one is saved as `hub.key.bak-<time>` first. **Data encrypted with the old key becomes unreadable.** Needs the Hub stopped. To change keys of a Hub in use, use `rotate-key`. |

```sh
postern-hub keygen --sealed
```

```text
New passphrase (min. 12 characters):
Repeat the passphrase:
✓ New sealed (set POSTERN_KEY_PASSPHRASE when starting the Hub) key file written to /var/lib/postern-key/hub.key
! Back up this file separately from the data directory and its backups.
```

Errors: `a key file already exists at <path>; refusing to overwrite it (use 'hub rotate-key' to change keys)`,
`a passphrase is needed: set POSTERN_KEY_PASSPHRASE or run in a terminal`,
`the passphrase must be at least 12 characters`, `the passphrases do not match`.

## rotate-key

```text
postern-hub rotate-key [--sealed]
```

Creates a new master key and re-encrypts every user's data key and every Hub secret with it.
Mail itself is not rewritten. **Stop the Hub first**. If the current key is sealed, it reads
`POSTERN_KEY_PASSPHRASE` or prompts `Passphrase for <path>:`.

| Flag | Meaning |
|---|---|
| `--sealed` | Seal the new key file with a passphrase (`POSTERN_KEY_PASSPHRASE` or a prompt, at least 12 characters). |

```text
✓ Rotated the master key: 2 secret(s) re-sealed, user keys re-wrapped.
  New key: /var/lib/postern-key/hub.key (old key kept as /var/lib/postern-key/hub.key.bak)
! Back up the new key file now. Older backups still need the OLD key: keep it with them.
```

The new key is written to `hub.key.new` before the database changes. If something fails halfway
the error says which file the database now needs; keep both files. See
[Encryption](https://docs.posternmail.app/security/encryption/).

## import-mailmanager

```text
postern-hub import-mailmanager <mailmanager.db> [--user EMAIL] [--dry-run]
```

Imports the rules of a MailManager database as Postern rules in **Test** mode. Each MailManager
mailbox is matched to the Postern mailbox with the same address; rules of unmatched mailboxes
are skipped. Running it again adds only what is new. **Stop the Hub first**; otherwise:
`the data directory is in use by a running Hub (...): stop the Hub, run the import, then start the Hub again`.

| Argument | Meaning |
|---|---|
| `<mailmanager.db>` | Path to MailManager's SQLite database, opened read-only. Required. |
| `--user EMAIL` | The Postern user to import for. Needed when the Hub has several users. |
| `--dry-run` | Only report what would be imported. |

```sh
postern-hub import-mailmanager /srv/mailmanager/mailmanager.db --user you@example.com --dry-run
```

The report lists the matched mailboxes, then the rules under **Would be imported (in Test
mode)** or **Imported (in Test mode)**, **Already imported earlier (left as they are)** and
**Skipped** (with the reason), what is never imported (captured mail, alert channels, the
activity log, MailManager users), and a count line such as `4 imported, 0 already there, 1 skipped.`

Errors: `<path> does not look like a MailManager database (no rules and accounts tables)`,
`this Hub has several users (...): choose one with --user`, `no Postern user <email>`,
`this Hub has no users yet`. Full guide: [Import from MailManager](https://docs.posternmail.app/rules/mailmanager/).

## engine

```text
postern-hub engine --hub URL --data-dir DIR [--listen ADDR] [--token-file PATH] [--device-name NAME]
```

Runs a device mail engine in direct mode: it syncs one user's mail straight from the providers,
mirrors settings from the Hub and serves the web app and API locally. The desktop app starts
it for you; you only run it by hand for development. Design:
[DIRECT-MODE.md](https://github.com/TheOriUHD/postern/blob/main/docs/dev/DIRECT-MODE.md).

| Flag | Meaning |
|---|---|
| `--hub URL` | The Hub to attach to. Required. |
| `--data-dir DIR` | The engine's own data directory (or `POSTERN_DATA_DIR`). Required: the default `./data` is refused, and so is a Hub data directory (one with `hub.db`), because the engine deletes its directory when signed out. |
| `--listen ADDR` | Local address. Default `127.0.0.1:0` (a free port), not the Hub's `:8080`. |
| `--token-file PATH` | Read the device's Hub token from this file. Otherwise it comes from `POSTERN_HUB_TOKEN`. |
| `--device-name NAME` | Device name. |

`--token` and `--hub-token` are refused: `the Hub token is never accepted on the command line;
set POSTERN_HUB_TOKEN or use --token-file`. Gateway headers come from
`POSTERN_HUB_EDGE_HEADERS`.

Standard output carries only JSON event lines; logs go to standard error:

```text
{"event":"ready","url":"http://127.0.0.1:52144","token":"<local token>"}
{"event":"signedOut"}
```

`signedOut` means the Hub revoked the device: the engine deletes its data directory and exits
with code 3. A second engine on the same directory exits at once with code 4
(`another mail engine is running on <dir>`). It stops on `SIGINT`, `SIGTERM` or when its
standard input (a pipe from the parent) closes.

```sh
POSTERN_HUB_TOKEN="$(cat ~/token)" postern-hub engine --hub https://mail.example.com --data-dir ~/postern-engine
```

## version

```text
postern-hub version
```

```text
Postern Hub 0.1.0 (c4d3244f0a1b) built 2026-12-05T10:00:00Z linux/amd64 go1.27.1
```

The version comes from the build (`git describe` for `scripts/build-hub.sh`; `dev` for a plain
`go build`). Commit and date are left out when unknown.

## help

```text
postern-hub help
```

Prints the general usage (the same as `-h`). An unknown command prints
`error: unknown command "frob" (run 'hub help')` with a short command list and exits with 1.
