You set up Borg on twelve hosts pointing to one big shared repo. It works. Then a developer laptop with a copy of the borg key gets exfiltrated by malware that knows the borg command line, runs borg delete --force, and your last six months of history evaporate in twenty minutes. The dashboard never alerted because the operation completed successfully. The problem isn’t Borg — Borg did exactly what an authenticated client asked it to do. The problem is the trust model: any client with the key can destroy the past.
What’s happening
Borg has three security postures, and most teams discover them in the wrong order.
Standard mode — what you get out of the box — is ssh user@server borg serve. The client has both the SSH key and the repo passphrase. The server delegates everything to the client. A malicious client can append, modify the manifest, prune, delete, and recompact. This is convenient and the default, and it is the mode in which a stolen client key takes out your history.
Append-only mode — ssh ... borg serve --append-only — is enforced server-side via SSH-forced commands. The server-side borg serve rejects any operation that would shrink the repo. Clients can create, info, list, extract. They cannot delete, prune, or compact. Operators who need to reclaim space do it through a separately-authenticated path (a console session on the server, or a dedicated “maintenance” SSH key that only the server admin holds). A stolen client key can fill the disk but cannot destroy history.
Per-host repos — every client backs up to its own repository, not a shared one. Combined with append-only, this means a compromise of client A cannot read OR damage client B’s history. The cost is no global dedup across hosts (only intra-host dedup) and more server-side directories to manage. For most fleets the trade is correct: the dedup savings are real but rarely worth the blast radius they require.
The server-side configuration that makes this all work is one SSH-forced-command line per client, in a single authorized_keys file, plus a directory structure that grants each client write access only to its own subdirectory. The forced command is the hard boundary; everything else is bookkeeping.
How to verify
On the server, confirm each client’s authorized_keys line includes restrict and an explicit forced command with --append-only:
cat /var/lib/borgserver/.ssh/authorized_keys
# command="borg serve --append-only --restrict-to-repository /var/lib/borgserver/repos/web01",restrict ssh-ed25519 AAAA...web01-client
# command="borg serve --append-only --restrict-to-repository /var/lib/borgserver/repos/web02",restrict ssh-ed25519 AAAA...web02-client
Three things must be present per line: command="..." with --append-only, the restrict option (forbids port forwarding, agent forwarding, X11, etc.), and --restrict-to-repository pointing at the per-client path. Missing any of these reopens the blast radius.
From a client, attempt a forbidden operation and confirm it fails:
borg delete --force ssh://borg@backups.internal/var/lib/borgserver/repos/web01
# Should return: "Remote: Borg server: Repository was opened in append-only mode."
Confirm append-only mode is actually enforced by attempting to delete an archive:
borg delete --force ssh://borg@backups.internal/var/lib/borgserver/repos/web01::web01-2026-05-13
# Same error
Check disk pressure on the server — append-only means clients fill space, server prunes:
df -h /var/lib/borgserver/repos/
du -sh /var/lib/borgserver/repos/* | sort -h
The fix
Two installer scripts: one for the Borg server, one for each client. Both target AlmaLinux 9.
Server installer. Save as /root/borg-server-setup.sh and run on the dedicated backup VM:
#!/usr/bin/env bash
set -euo pipefail
### BorgBackup server on AlmaLinux 9
### One append-only repo per client, ssh-forced-command isolation
### Server-side prune via a dedicated maintenance key
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT ===
BORG_USER="borgserver"
BORG_HOME="/var/lib/borgserver"
REPO_BASE="${BORG_HOME}/repos"
SSH_PORT="2222" # Move off 22 to reduce log noise
ADMIN_KEY="" # Paste your maintenance public key here (no command= prefix)
# ============
echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y install borgbackup chrony fail2ban policycoreutils-python-utils \
vim curl wget tar gzip jq lsof tcpdump net-tools
systemctl enable --now chronyd
echo "==> SELinux enforcing"
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1 || true
echo "==> Creating borg user and tree"
useradd -r -m -d "${BORG_HOME}" -s /bin/bash "${BORG_USER}"
mkdir -p "${REPO_BASE}" "${BORG_HOME}/.ssh"
chown -R "${BORG_USER}:${BORG_USER}" "${BORG_HOME}"
chmod 700 "${BORG_HOME}/.ssh"
chmod 750 "${BORG_HOME}"
touch "${BORG_HOME}/.ssh/authorized_keys"
chmod 600 "${BORG_HOME}/.ssh/authorized_keys"
chown "${BORG_USER}:${BORG_USER}" "${BORG_HOME}/.ssh/authorized_keys"
echo "==> Hardening SSH (server)"
cat > /etc/ssh/sshd_config.d/10-borg.conf <<EOF
Port ${SSH_PORT}
Port 22
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes
AllowUsers root ${BORG_USER}
Match User ${BORG_USER}
ChrootDirectory ${BORG_HOME}
AllowTcpForwarding no
X11Forwarding no
PermitTTY no
EOF
# We don't actually chroot below because borg serve needs system libs;
# keep ChrootDirectory commented unless you've built a chroot tree.
sed -i 's|^[[:space:]]*ChrootDirectory|# ChrootDirectory|' /etc/ssh/sshd_config.d/10-borg.conf
sshd -t && systemctl restart sshd
echo "==> fail2ban"
cat > /etc/fail2ban/jail.d/sshd.local <<EOF
[sshd]
enabled = true
port = ${SSH_PORT},ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
EOF
systemctl enable --now fail2ban
echo "==> Maintenance key (used for server-side prune/check; no append-only)"
if [[ -n "${ADMIN_KEY}" ]]; then
echo "${ADMIN_KEY}" >> "${BORG_HOME}/.ssh/authorized_keys"
echo "Maintenance key installed."
else
echo "WARNING: ADMIN_KEY blank — paste your maintenance pubkey before relying on this."
fi
echo "==> Add-client helper script"
cat > /usr/local/sbin/borg-add-client <<'ADDCLIENT'
#!/usr/bin/env bash
set -euo pipefail
# borg-add-client <hostname> < clientpubkey.pub
# Reads pubkey on stdin, creates per-client repo dir, adds restricted authorized_keys line
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <hostname> (paste pubkey on stdin)" >&2
exit 2
fi
HOST="$1"
BORG_USER="borgserver"
BORG_HOME="/var/lib/borgserver"
REPO_BASE="${BORG_HOME}/repos"
REPO_PATH="${REPO_BASE}/${HOST}"
PUBKEY="$(cat)"
if [[ -z "${PUBKEY}" ]]; then
echo "ERROR: empty pubkey on stdin" >&2
exit 2
fi
# Strip comment, single-line normalize
PUBKEY_CLEAN="$(echo "${PUBKEY}" | tr -d '\n' | sed 's/[[:space:]]\+/ /g')"
install -d -m 750 -o ${BORG_USER} -g ${BORG_USER} "${REPO_PATH}"
LINE="command=\"borg serve --append-only --restrict-to-repository ${REPO_PATH}\",restrict ${PUBKEY_CLEAN}"
echo "${LINE}" >> "${BORG_HOME}/.ssh/authorized_keys"
echo "Added client: ${HOST}"
echo "Repo path: ${REPO_PATH}"
echo "Tell the client to init with:"
echo " borg init --encryption=repokey-blake2 ssh://${BORG_USER}@<server>:${SSH_PORT}${REPO_PATH}"
ADDCLIENT
chmod 750 /usr/local/sbin/borg-add-client
echo "==> Maintenance scripts (run on server, NOT via client SSH)"
mkdir -p /etc/borg
cat > /usr/local/sbin/borg-prune-all <<PRUNE
#!/usr/bin/env bash
set -euo pipefail
### Server-side prune for all per-host repos.
### Reads passphrase per host from /etc/borg/passphrases/<host>
KEEP_DAILY=14
KEEP_WEEKLY=8
KEEP_MONTHLY=12
for repo in ${REPO_BASE}/*; do
[[ -d "\${repo}" ]] || continue
host="\$(basename "\${repo}")"
pwfile="/etc/borg/passphrases/\${host}"
if [[ ! -f "\${pwfile}" ]]; then
echo "skip \${host}: no passphrase file at \${pwfile}"
continue
fi
echo "===> prune \${host}"
BORG_PASSPHRASE="\$(cat "\${pwfile}")" \\
runuser -u ${BORG_USER} -- \\
borg prune --list \\
--keep-daily \${KEEP_DAILY} \\
--keep-weekly \${KEEP_WEEKLY} \\
--keep-monthly \${KEEP_MONTHLY} \\
"\${repo}"
echo "===> compact \${host}"
runuser -u ${BORG_USER} -- borg compact "\${repo}"
done
PRUNE
chmod 750 /usr/local/sbin/borg-prune-all
mkdir -p /etc/borg/passphrases
chmod 700 /etc/borg /etc/borg/passphrases
echo "Place per-host passphrases at /etc/borg/passphrases/<host> (mode 600, root-owned)"
cat > /usr/local/sbin/borg-check-all <<CHECK
#!/usr/bin/env bash
set -euo pipefail
for repo in ${REPO_BASE}/*; do
[[ -d "\${repo}" ]] || continue
host="\$(basename "\${repo}")"
pwfile="/etc/borg/passphrases/\${host}"
[[ -f "\${pwfile}" ]] || continue
echo "===> check \${host}"
BORG_PASSPHRASE="\$(cat "\${pwfile}")" \\
runuser -u ${BORG_USER} -- \\
borg check --verify-data --last 3 "\${repo}"
done
CHECK
chmod 750 /usr/local/sbin/borg-check-all
echo "==> systemd timers for prune + check"
cat > /etc/systemd/system/borg-prune.service <<UNIT
[Unit]
Description=Borg prune all repos
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/borg-prune-all
TimeoutStartSec=12h
RuntimeMaxSec=14h
UNIT
cat > /etc/systemd/system/borg-prune.timer <<'TIMER'
[Unit]
Description=Weekly borg prune
[Timer]
OnCalendar=Sun *-*-* 05:00:00
Persistent=true
[Install]
WantedBy=timers.target
TIMER
cat > /etc/systemd/system/borg-check.service <<UNIT
[Unit]
Description=Borg integrity check all repos
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/borg-check-all
TimeoutStartSec=12h
RuntimeMaxSec=14h
UNIT
cat > /etc/systemd/system/borg-check.timer <<'TIMER'
[Unit]
Description=Weekly borg check
[Timer]
OnCalendar=Wed *-*-* 04:00:00
Persistent=true
[Install]
WantedBy=timers.target
TIMER
systemctl daemon-reload
systemctl enable --now borg-prune.timer borg-check.timer
firewall-cmd --permanent --add-port=${SSH_PORT}/tcp >/dev/null 2>&1 || true
firewall-cmd --reload >/dev/null 2>&1 || true
echo
echo "========================================"
echo "Borg server ready at port ${SSH_PORT}"
echo "========================================"
echo
echo "Add a client (paste the client's public key on stdin):"
echo " ssh-keyscan myclient | /usr/local/sbin/borg-add-client myclient"
echo " cat client.pub | /usr/local/sbin/borg-add-client myclient"
echo
echo "Maintenance authorized_keys lines should NOT have --append-only:"
echo " ${BORG_HOME}/.ssh/authorized_keys"
echo
echo "Per-host passphrases live at /etc/borg/passphrases/<host>"
Client installer. Save as /root/borg-client-setup.sh on every host that backs up:
#!/usr/bin/env bash
set -euo pipefail
### Borg client on AlmaLinux 9
### Generates a per-host SSH keypair, registers with the server via borg-add-client,
### writes systemd timer for nightly backup.
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT ===
BORG_SERVER="backups.internal"
BORG_SERVER_PORT="2222"
BORG_SERVER_USER="borgserver"
HOST_NAME="$(hostname -s)"
REPO_PASSPHRASE="CHANGE_ME_BORG_PASSPHRASE"
BACKUP_PATHS=(/etc /var/log /var/lib /home /srv)
EXCLUDES=(/var/lib/borg-cache /var/cache /tmp /proc /sys /dev /run /var/lib/docker/overlay2)
# ============
dnf -y install epel-release
dnf -y install borgbackup openssh-clients curl jq
echo "==> Generating per-host SSH key"
install -d -m 700 /root/.ssh
if [[ ! -f /root/.ssh/borg_id_ed25519 ]]; then
ssh-keygen -t ed25519 -N '' -C "borg-client-${HOST_NAME}" -f /root/.ssh/borg_id_ed25519
fi
echo
echo "Public key (give this to the borg server admin):"
echo "----"
cat /root/.ssh/borg_id_ed25519.pub
echo "----"
echo "On the server, run:"
echo " echo '<the pubkey above>' | /usr/local/sbin/borg-add-client ${HOST_NAME}"
echo
mkdir -p /etc/borg
cat > /etc/borg/env <<EOF
BORG_REPO=ssh://${BORG_SERVER_USER}@${BORG_SERVER}:${BORG_SERVER_PORT}/var/lib/borgserver/repos/${HOST_NAME}
BORG_PASSPHRASE=${REPO_PASSPHRASE}
BORG_RSH=ssh -i /root/.ssh/borg_id_ed25519 -o StrictHostKeyChecking=accept-new
BORG_RELOCATED_REPO_ACCESS_IS_OK=no
BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=no
EOF
chmod 600 /etc/borg/env
cat > /etc/borg/excludes <<EOF
$(printf '%s\n' "${EXCLUDES[@]}")
EOF
cat > /usr/local/sbin/borg-backup <<BACKUP
#!/usr/bin/env bash
set -euo pipefail
source /etc/borg/env
ARCHIVE="${HOST_NAME}-\$(date +%Y-%m-%dT%H:%M:%S)"
TAG="logger -t borg-backup[${HOST_NAME}]"
\${TAG} starting
borg create --stats --compression zstd,3 \\
--exclude-from /etc/borg/excludes \\
--one-file-system \\
"\${BORG_REPO}::\${ARCHIVE}" \\
${BACKUP_PATHS[@]} \\
2>&1 | logger -t "borg-backup[${HOST_NAME}]"
\${TAG} completed
# NOTE: clients do NOT prune. The server prunes weekly.
BACKUP
chmod 750 /usr/local/sbin/borg-backup
cat > /etc/systemd/system/borg-backup.service <<'UNIT'
[Unit]
Description=Borg backup
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
EnvironmentFile=/etc/borg/env
ExecStart=/usr/local/sbin/borg-backup
TimeoutStartSec=4h
RuntimeMaxSec=5h
Nice=10
IOSchedulingClass=best-effort
IOSchedulingPriority=7
UNIT
cat > /etc/systemd/system/borg-backup.timer <<'TIMER'
[Unit]
Description=Daily borg backup
[Timer]
OnCalendar=*-*-* 02:00:00
RandomizedDelaySec=45m
Persistent=true
Unit=borg-backup.service
[Install]
WantedBy=timers.target
TIMER
systemctl daemon-reload
echo
echo "Next steps:"
echo " 1. Give the pubkey above to the server admin and have them add it."
echo " 2. Initialize the repo (ONCE, from this host):"
echo " source /etc/borg/env && borg init --encryption=repokey-blake2"
echo " 3. Run a smoke backup:"
echo " systemctl start borg-backup.service"
echo " journalctl -u borg-backup.service -f"
echo " 4. Enable the timer for nightly runs:"
echo " systemctl enable --now borg-backup.timer"
Run the server installer once, then for each client:
- Run the client installer on the host, copy its emitted public key.
- On the server, register the client:
echo '<pubkey>' | /usr/local/sbin/borg-add-client <hostname>. - Store the per-host passphrase on the server at
/etc/borg/passphrases/<hostname>(mode 600) so server-side prune can run. - From the client, init the repo and run a smoke backup.
A few notes that catch operators out:
-
The client’s
BORG_PASSPHRASEand the server’s/etc/borg/passphrases/<host>must match. Borg’srepokey-blake2encryption mode keeps the key inside the repo, encrypted by the passphrase. The server needs the passphrase to run prune/check. This is fine — the server is the trust anchor. -
--append-onlycan be temporarily lifted by editing the client’s authorized_keys line and reloading sshd, but doing so undoes the security property. Use the maintenance key path for ad-hoc prune work, not the client’s key. -
ZFS or XFS underneath. Borg repos churn small files; ZFS with
recordsize=1Mandatime=offis the cleanest substrate. XFS withnoatime,nodiratimeworks too. ext4 is fine for small fleets but starts to feel the metadata pressure past ~5 TB of unique data. -
Compact runs after prune.
borg prunemarks segments for reuse;borg compactactually reclaims disk. The server-side script does both. Without compact, deletes never free disk.
For fleets that need this set up and operated — including the maintenance host, off-site mirror, and integration with the customer’s IAM — Stack Harbor runs it as part of environment management. Talk to our monitoring + support team or book a backup-architecture review.