Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
DATABASE_URL=postgres://shop@10.0.2.21:5432/shop
LOG_LEVEL=info[Service]
EnvironmentFile=/etc/shop/api.env
Environment=TZ=Asia/RiyadhStep by step
- Three ways, by use:
| Way | Use for |
|---|---|
Environment=KEY=value in the unit | Simple, non-secret settings. Visible in systemctl show. |
EnvironmentFile=/etc/app/x.env | Many settings in one file (rights 600, owned by root). |
LoadCredential= (systemd credentials) | Secrets: given to the service as files, not as env variables. |
- The env file format — one
KEY=valueper line, noexport, no spaces around=, quotes only when the value has spaces:
LOG_LEVEL=info
APP_NAME="Shop API"A - in front makes the file optional: EnvironmentFile=-/etc/shop/extra.env.
- 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'- 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:
[Service]
LoadCredential=db_password:/etc/shop/secrets/db_passwordThe 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.
- Changes need a restart — env variables are read only at start:
- Never put secrets in the main unit file under
/usr/lib/systemd, inEnvironment=lines in Git, or inExecStartarguments (visible inps).
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| The app does not see a variable | It was set in your shell or .bashrc. | Put it in Environment= or the env file. |
Failed to load environment files: No such file or directory | Wrong path. | Fix the path, or - prefix for optional files. |
| Value has quotes in the app | Quotes copied from a shell export line. | Remove the quotes (or keep only for values with spaces). |
$HOME / $PATH not expanded | The env file is not a shell script. | Write full values; systemd does not run shell expansion here. |
| Old value after a change | Service not restarted. | systemctl restart. |
Secret visible with systemctl show | It is in Environment=. | Move it to LoadCredential= or a 600 env file. |
Go further
- Command pages: systemctl, env, printenv.
- Next how-tos: Create a systemd service for your app, Limit CPU and memory of a service, Generate a strong password or secret.
- Learn it: RHCSA — Edit units, TLCL — The environment, envsubst in DevOps.
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.