An app needs environment variables containing secrets. How do you give them to a systemd service safely?
AdvancedScenarioProcesses, services and systemd
Answer
Situation read → Avoid secrets in unit files, which are world-readable.
What I'd do →
- Better: use systemd credentials:
[Service]
LoadCredential=db_password:/etc/app/secrets/db_password
ExecStart=/opt/app/bin/app
The app reads $CREDENTIALS_DIRECTORY/db_password. With systemd-creds encrypt, use LoadCredentialEncrypted= (TPM-bound).
2. Acceptable: EnvironmentFile=/etc/app/app.env with chmod 600 root:root.
3. Best for fleets: fetch from Vault or a cloud secret manager at start.
Why → Environment= lines show up in systemctl show, and environment variables leak to child processes and /proc/<pid>/environ.
Interview tip → Mention that systemctl show reveals Environment= to any user.
More on Processes, services and systemd
Your team has a Python API that someone starts with `nohup python app.py &`. Make it a proper service running as a non-root user.BeginnerThe service you created must bind to port 443, but it runs as a non-root user. How?BeginnerYou need to change one setting (memory limit) in a vendor-supplied unit file without editing it. How?BeginnerA service depends on a database on the same host and fails at boot because the DB isn't ready. How do you fix the ordering?IntermediateA service keeps crashing and systemd stops restarting it after a few attempts. How do you tune this?IntermediateA long-running app must reload its config without dropping connections. How do you wire this into systemd?Intermediate