Credentials via systemd don't work #1271

Closed
opened 2026-02-22 10:36:59 +00:00 by muemo · 5 comments

What I'm trying to do and why

I have been trying to follow the documentation in setting up encrypted credentials via systemd.

Steps to reproduce

I used systemd-ask-password -n | systemd-creds encrypt - /etc/credstore.encrypted/borgmatic.pw to first encrypt the secret, added encryption_passphrase: "{credential systemd borgmatic.pw}" to my borgmatic config file, and debian set LoadCredentialEncrypted=borgmatic.pw in the borgmatic service file by default.
Then, running sudo borgmatic or systemctl start borgmatic.service results in the outputs below in Actual behavior.

Actual behavior

No commands to run for pre-everything hook
/etc/borgmatic/config.yaml: Skipping prune/compact actions due to configured skip_actions
/etc/borgmatic/config.yaml: Calling systemd hook function load_credential
/etc/borgmatic/config.yaml: /etc/borgmatic/config.yaml: Error getting local Borg version
/etc/borgmatic/config.yaml: Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set
/etc/borgmatic/config.yaml: An error occurred
No commands to run for post-everything hook

summary:
/etc/borgmatic/config.yaml: Loading configuration file
An error occurred
/etc/borgmatic/config.yaml: Error getting local Borg version
Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set

Need some help? https://torsion.org/borgmatic/#issues

Feb 22 11:29:03 redacted systemd[1]: Starting borgmatic.service - borgmatic backup...
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: /etc/borgmatic/config.yaml: Error getting local Borg version
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: Cannot load invalid credential name "borgmatic.pw"
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: An error occurred
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL summary:
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL An error occurred
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: Error getting local Borg version
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Cannot load invalid credential name "borgmatic.pw"
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL
Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Need some help? https://torsion.org/borgmatic/#issues
Feb 22 11:30:05 redacted systemd-inhibit[3613]: /usr/bin/borgmatic failed with exit status 1.
Feb 22 11:30:05 redacted systemd[1]: borgmatic.service: Main process exited, code=exited, status=1/FAILURE
Feb 22 11:30:05 redacted systemd[1]: borgmatic.service: Failed with result 'exit-code'.
Feb 22 11:30:05 redacted systemd[1]: Failed to start borgmatic.service - borgmatic backup.

Expected behavior

Borgmatic decrypts the credentials and uses them.

Other notes / implementation ideas

I have tried running

systemd-run --pipe --wait --property=LoadCredentialEncrypted=borgmatic.pw systemd-creds cat borgmatic.pw

and it correctly shows the decrypted secret.
Also, replacing the encryption_passphrase with just the passphrase in plain text makes everything work.

borgmatic version

1.9.14

borgmatic installation method

Debian package

Borg version

borg 1.4.0

Python version

Python 3.13.5

Database version (if applicable)

No response

Operating system and version

PRETTY_NAME="Debian GNU/Linux 13 (trixie)" NAME="Debian GNU/Linux" VERSION_ID="13" VERSION="13 (trixie)" VERSION_CODENAME=trixie DEBIAN_VERSION_FULL=13.3 ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"

### What I'm trying to do and why I have been trying to follow [the documentation](https://torsion.org/borgmatic/reference/configuration/credentials/systemd/) in setting up encrypted credentials via systemd. ### Steps to reproduce I used `systemd-ask-password -n | systemd-creds encrypt - /etc/credstore.encrypted/borgmatic.pw` to first encrypt the secret, added `encryption_passphrase: "{credential systemd borgmatic.pw}"` to my borgmatic config file, and debian set `LoadCredentialEncrypted=borgmatic.pw` in the borgmatic service file by default. Then, running `sudo borgmatic` or `systemctl start borgmatic.service` results in the outputs below in Actual behavior. ### Actual behavior ``` No commands to run for pre-everything hook /etc/borgmatic/config.yaml: Skipping prune/compact actions due to configured skip_actions /etc/borgmatic/config.yaml: Calling systemd hook function load_credential /etc/borgmatic/config.yaml: /etc/borgmatic/config.yaml: Error getting local Borg version /etc/borgmatic/config.yaml: Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set /etc/borgmatic/config.yaml: An error occurred No commands to run for post-everything hook summary: /etc/borgmatic/config.yaml: Loading configuration file An error occurred /etc/borgmatic/config.yaml: Error getting local Borg version Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set Need some help? https://torsion.org/borgmatic/#issues ``` --- ``` Feb 22 11:29:03 redacted systemd[1]: Starting borgmatic.service - borgmatic backup... Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: /etc/borgmatic/config.yaml: Error getting local Borg version Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: Cannot load invalid credential name "borgmatic.pw" Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: An error occurred Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL summary: Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL An error occurred Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL /etc/borgmatic/config.yaml: Error getting local Borg version Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Cannot load invalid credential name "borgmatic.pw" Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Feb 22 11:30:05 redacted borgmatic[3617]: CRITICAL Need some help? https://torsion.org/borgmatic/#issues Feb 22 11:30:05 redacted systemd-inhibit[3613]: /usr/bin/borgmatic failed with exit status 1. Feb 22 11:30:05 redacted systemd[1]: borgmatic.service: Main process exited, code=exited, status=1/FAILURE Feb 22 11:30:05 redacted systemd[1]: borgmatic.service: Failed with result 'exit-code'. Feb 22 11:30:05 redacted systemd[1]: Failed to start borgmatic.service - borgmatic backup. ``` ### Expected behavior Borgmatic decrypts the credentials and uses them. ### Other notes / implementation ideas I have tried running ``` systemd-run --pipe --wait --property=LoadCredentialEncrypted=borgmatic.pw systemd-creds cat borgmatic.pw ``` and it correctly shows the decrypted secret. Also, replacing the `encryption_passphrase` with just the passphrase in plain text makes everything work. ### borgmatic version 1.9.14 ### borgmatic installation method Debian package ### Borg version borg 1.4.0 ### Python version Python 3.13.5 ### Database version (if applicable) _No response_ ### Operating system and version PRETTY_NAME="Debian GNU/Linux 13 (trixie)" NAME="Debian GNU/Linux" VERSION_ID="13" VERSION="13 (trixie)" VERSION_CODENAME=trixie DEBIAN_VERSION_FULL=13.3 ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"
Owner

A few thoughts here:

  • Error getting local Borg version: This suggest that borgmatic could not find Borg. Is borg's parent directory in the system PATH, both at the command-line and when running under systemd? If not, you can add it to the PATH or just set a local_path option in borgmatic.
  • Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set: Can I get a look at your borgmatic systemd service? Are you getting this error even when running borgmatic from systemd, or only when run manually from the command-line? If you want to run borgmatic manually from the command-line even when systemd credentials are configured, you'll need to upgrade to a newer version of borgmatic. (See the docs about this. You'll need 2.0.9+.)
A few thoughts here: * `Error getting local Borg version`: This suggest that borgmatic could not find Borg. Is `borg`'s parent directory in the system `PATH`, both at the command-line and when running under systemd? If not, you can add it to the `PATH` or just set a `local_path` option in borgmatic. * `Cannot load credential "borgmatic.pw" because the systemd CREDENTIALS_DIRECTORY environment variable is not set`: Can I get a look at your borgmatic systemd service? Are you getting this error even when running borgmatic from `systemd`, or only when run manually from the command-line? If you want to run borgmatic manually from the command-line even when systemd credentials are configured, you'll need to upgrade to a newer version of borgmatic. (See [the docs](https://torsion.org/borgmatic/reference/configuration/credentials/systemd/) about this. You'll need 2.0.9+.)
Author

Thanks for your response!

  • Considering it works both via systemd and on the command line with the plain text passphrase as encryption_passphrase, I am convinced, that borg is in fact in PATH. In fact, I even get the Error getting local Borg version problem, when the local_path is specified to be /usr/bin/borg.
  • My systemd service is the default debian one, listed below. I get the error both on command line as well as via systemd, see above for the two different logs. I would prefer to stay on the version in debian, and only need it to work in the systemd service.
borgmatic.service
[Unit]
Description=borgmatic backup
Wants=network-online.target
After=network-online.target
# Prevent borgmatic from running unless the machine is plugged into power. Remove this line if you
# want to allow borgmatic to run anytime.
ConditionACPower=true
Documentation=https://torsion.org/borgmatic/
ConditionFileNotEmpty=|/etc/borgmatic/config.yaml
ConditionPathExistsGlob=|/etc/borgmatic.d/*.yaml

[Service]
Type=oneshot
RuntimeDirectory=borgmatic
StateDirectory=borgmatic

# Load single encrypted credential.
LoadCredentialEncrypted=borgmatic.pw

# Load multiple encrypted credentials.
# LoadCredentialEncrypted=borgmatic:/etc/credstore.encrypted/borgmatic/

# Security settings for systemd running as root, optional but recommended to improve security. You
# can disable individual settings if they cause problems for your use case. For more details, see
# the systemd manual: https://www.freedesktop.org/software/systemd/man/systemd.exec.html
LockPersonality=true
# Certain borgmatic features like Healthchecks integration need MemoryDenyWriteExecute to be off.
# But you can try setting it to "yes" for improved security if you don't use those features.
MemoryDenyWriteExecute=no
NoNewPrivileges=yes
PrivateDevices=yes
PrivateTmp=yes
ProtectClock=yes
ProtectControlGroups=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
SystemCallErrorNumber=EPERM
# To restrict write access further, change "ProtectSystem" to "strict" and
# uncomment "ReadWritePaths", "TemporaryFileSystem", "BindPaths" and
# "BindReadOnlyPaths". Then add any local repository paths to the list of
# "ReadWritePaths". This leaves most of the filesystem read-only to borgmatic.
ProtectSystem=full
# ReadWritePaths=-/mnt/my_backup_drive
# This will mount a tmpfs on top of /root and pass through needed paths
# TemporaryFileSystem=/root:ro
# BindPaths=-/root/.cache/borg -/root/.config/borg -/root/.borgmatic
# BindReadOnlyPaths=-/root/.ssh

# May interfere with running external programs within borgmatic hooks.
CapabilityBoundingSet=CAP_DAC_READ_SEARCH CAP_NET_RAW

# Lower CPU and I/O priority.
Nice=19
CPUSchedulingPolicy=batch
IOSchedulingClass=best-effort
IOSchedulingPriority=7
IOWeight=100

Restart=no
# Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that
# doesn't support this (pre-240 or so), you may have to remove this option.
LogRateLimitIntervalSec=0

# Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
# dbus-user-session to be installed.
ExecStartPre=sleep 1m
ExecStart=systemd-inhibit --who="borgmatic" --what="sleep:shutdown" --why="Prevent interrupting scheduled backup" /usr/bin/borgmatic --verbosity -2 --syslog-verbosity 1
Thanks for your response! * Considering it works both via systemd and on the command line with the plain text passphrase as `encryption_passphrase`, I am convinced, that `borg` _is_ in fact in `PATH`. In fact, I even get the `Error getting local Borg version` problem, when the `local_path` is specified to be `/usr/bin/borg`. * My systemd service is the default debian one, listed below. I get the error both on command line as well as via systemd, see above for the two different logs. I would prefer to stay on the version in debian, and only need it to work in the systemd service. <details> <summary>borgmatic.service</summary> ``` [Unit] Description=borgmatic backup Wants=network-online.target After=network-online.target # Prevent borgmatic from running unless the machine is plugged into power. Remove this line if you # want to allow borgmatic to run anytime. ConditionACPower=true Documentation=https://torsion.org/borgmatic/ ConditionFileNotEmpty=|/etc/borgmatic/config.yaml ConditionPathExistsGlob=|/etc/borgmatic.d/*.yaml [Service] Type=oneshot RuntimeDirectory=borgmatic StateDirectory=borgmatic # Load single encrypted credential. LoadCredentialEncrypted=borgmatic.pw # Load multiple encrypted credentials. # LoadCredentialEncrypted=borgmatic:/etc/credstore.encrypted/borgmatic/ # Security settings for systemd running as root, optional but recommended to improve security. You # can disable individual settings if they cause problems for your use case. For more details, see # the systemd manual: https://www.freedesktop.org/software/systemd/man/systemd.exec.html LockPersonality=true # Certain borgmatic features like Healthchecks integration need MemoryDenyWriteExecute to be off. # But you can try setting it to "yes" for improved security if you don't use those features. MemoryDenyWriteExecute=no NoNewPrivileges=yes PrivateDevices=yes PrivateTmp=yes ProtectClock=yes ProtectControlGroups=yes ProtectHostname=yes ProtectKernelLogs=yes ProtectKernelModules=yes ProtectKernelTunables=yes RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK RestrictNamespaces=yes RestrictRealtime=yes RestrictSUIDSGID=yes SystemCallArchitectures=native SystemCallFilter=@system-service SystemCallErrorNumber=EPERM # To restrict write access further, change "ProtectSystem" to "strict" and # uncomment "ReadWritePaths", "TemporaryFileSystem", "BindPaths" and # "BindReadOnlyPaths". Then add any local repository paths to the list of # "ReadWritePaths". This leaves most of the filesystem read-only to borgmatic. ProtectSystem=full # ReadWritePaths=-/mnt/my_backup_drive # This will mount a tmpfs on top of /root and pass through needed paths # TemporaryFileSystem=/root:ro # BindPaths=-/root/.cache/borg -/root/.config/borg -/root/.borgmatic # BindReadOnlyPaths=-/root/.ssh # May interfere with running external programs within borgmatic hooks. CapabilityBoundingSet=CAP_DAC_READ_SEARCH CAP_NET_RAW # Lower CPU and I/O priority. Nice=19 CPUSchedulingPolicy=batch IOSchedulingClass=best-effort IOSchedulingPriority=7 IOWeight=100 Restart=no # Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that # doesn't support this (pre-240 or so), you may have to remove this option. LogRateLimitIntervalSec=0 # Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and # dbus-user-session to be installed. ExecStartPre=sleep 1m ExecStart=systemd-inhibit --who="borgmatic" --what="sleep:shutdown" --why="Prevent interrupting scheduled backup" /usr/bin/borgmatic --verbosity -2 --syslog-verbosity 1 ``` </details>
Owner

Thanks for providing those details. It looks like the version of borgmatic included in Debian has a bug preventing use of credential names that contain the "." character (#1044). If you don't want to upgrade to get the fix, then you can rename the credential (e.g. to borgmaticpw) in both the systemd service and borgmatic's configuration. Make sure to reload the service as well.

Let me know how that works out for you!

Thanks for providing those details. It looks like the version of borgmatic included in Debian has a bug preventing use of credential names that contain the "." character (#1044). If you don't want to upgrade to get the fix, then you can rename the credential (e.g. to `borgmaticpw`) in both the systemd service and borgmatic's configuration. Make sure to reload the service as well. Let me know how that works out for you!
Author

Thank you a lot, this does indeed work!

Thank you a lot, this does indeed work!
muemo closed this issue 2026-02-22 22:10:25 +00:00
Owner

Great, glad to hear it!

Great, glad to hear it!
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
borgmatic-collective/borgmatic#1271
No description provided.