Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 105System · systemd

How to give a service its environment variables

Your app reads DATABASE_URL, LOG_LEVEL and an API key from environment variables. In a systemd service, your shell's export and .bashrc do not count — the service starts clean. This how-to sets variables in three ways (Environment=, an env file, and systemd credentials for secrets), shows what the service really sees, and keeps secrets out of systemctl show.

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

105.1

The quick answer

/etc/shop/api.env
DATABASE_URL=postgres://shop@10.0.2.21:5432/shop
LOG_LEVEL=info
chmod 600 /etc/shop/api.env
systemctl edit shop-api
/etc/systemd/system/shop-api.service.d/override.conf
[Service]
EnvironmentFile=/etc/shop/api.env
Environment=TZ=Asia/Riyadh
systemctl daemon-reload && systemctl restart shop-api

105.2

Step by step

  1. Three ways, by use:
WayUse for
Environment=KEY=value in the unitSimple, non-secret settings. Visible in systemctl show.
EnvironmentFile=/etc/app/x.envMany settings in one file (rights 600, owned by root).
LoadCredential= (systemd credentials)Secrets: given to the service as files, not as env variables.
  1. The env file format — one KEY=value per line, no export, no spaces around =, quotes only when the value has spaces:
/etc/shop/api.env
LOG_LEVEL=info
APP_NAME="Shop API"

A - in front makes the file optional: EnvironmentFile=-/etc/shop/extra.env.

  1. What does the service see?
systemctl show shop-api -p Environment -p EnvironmentFiles
cat /proc/$(systemctl show -p MainPID --value shop-api)/environ | tr '\0' '\n'
  1. Secrets as credentials (systemd 250+ on RHEL 9/10 and current Ubuntu) — the file is copied into a private folder only the service can read:
/etc/systemd/system/shop-api.service.d/secrets.conf
[Service]
LoadCredential=db_password:/etc/shop/secrets/db_password

The app reads $CREDENTIALS_DIRECTORY/db_password. The secret does not appear in systemctl show, in /proc/…/environ of other processes, or in child processes' environment.

  1. Changes need a restart — env variables are read only at start:
systemctl daemon-reload
systemctl restart shop-api
  1. Never put secrets in the main unit file under /usr/lib/systemd, in Environment= lines in Git, or in ExecStart arguments (visible in ps).

105.3

Check that it worked

The app starts with the right settings (its log shows the log level / DB host), systemctl show -p EnvironmentFiles lists your file, and secret values are not visible in systemctl show shop-api.

105.4

Common problems

ProblemCauseFix
The app does not see a variableIt was set in your shell or .bashrc.Put it in Environment= or the env file.
Failed to load environment files: No such file or directoryWrong path.Fix the path, or - prefix for optional files.
Value has quotes in the appQuotes copied from a shell export line.Remove the quotes (or keep only for values with spaces).
$HOME / $PATH not expandedThe env file is not a shell script.Write full values; systemd does not run shell expansion here.
Old value after a changeService not restarted.systemctl restart.
Secret visible with systemctl showIt is in Environment=.Move it to LoadCredential= or a 600 env file.

105.5

Go further

1Why does a service not see variables you exported in your shell?

systemd starts services with their own clean environment; only Environment=/EnvironmentFile= count.

2How should a service get a secret without putting it in an environment variable?

LoadCredential= — the app reads it from $CREDENTIALS_DIRECTORY.

Alle commando's in dit hoofdstuk