DEV Community

Schiff Heimlich
Schiff Heimlich

Posted on

Stop Passing Secrets to Systemd Services via Environment Variables

Stop Passing Secrets to Systemd Services via Environment Variables

Here's something I see a lot: systemd services that load secrets through Environment= or EnvironmentFile=. It works, but those secrets show up in /proc/<pid>/environ for any process on the system to read.

There's a better way with systemd's credential system.

The Problem With Environment Variables

When you pass secrets as environment variables:

[Service]
Environment="DB_PASSWORD=supersecret"
ExecStart=/usr/local/bin/myapp
Enter fullscreen mode Exit fullscreen mode

That password is readable by anyone who can access /proc/<pid>/environ. On shared systems or containers, that's a bigger surface than most people realize.

Using Systemd Credentials Instead

Systemd 254+ has LoadCredentialEncrypted= and ImportCredential= directives that handle this differently. The secret gets loaded as a file descriptor or file in the service's private namespace, not as an environment variable.

[Service]
LoadCredentialEncrypted=db-password:/etc/credentials/db-password.enc
ExecStart=/usr/local/bin/myapp
Enter fullscreen mode Exit fullscreen mode

Your application reads it from /run/credentials/myapp.service/db-password instead of an env var. It's never exposed in /proc, never gets logged accidentally, and systemd handles the decryption.

Setting It Up

You'll need to encrypt the credential first using systemd-creds:

echo -n "supersecret" | systemd-creds encrypt - /etc/credentials/db-password.enc
Enter fullscreen mode Exit fullscreen mode

Then reference it in your service file. The credential gets decrypted at service start and cleaned up on stop.

If your systemd version supports it, you can also use ImportCredential= to pull credentials from the system credential store or TPM-backed storage.

When This Matters

  • Services that handle database credentials
  • API keys for external services
  • TLS private keys loaded at startup
  • Anything running on shared infrastructure

It's not always necessary. For local dev or single-user systems, environment variables are fine. But for production services on anything shared, credentials give you a cleaner isolation boundary with minimal configuration overhead.


The setup takes a few extra lines in your service file. Worth it if you care about where your secrets actually live at runtime.

Top comments (0)