December 4, 2026

Environment Variables in systemd Services: Environment=, EnvironmentFile, and the Lines That Silently Disappear

A small key cabinet on a server room wall with rows of blank-tagged hooks

A script that works from a terminal and fails as a systemd service is very often missing an environment variable. Services do not read .bashrc or /etc/profile; they get only what their unit file gives them. The two settings for that, Environment= and EnvironmentFile=, look simple, but the file format is not a shell script, and one of the most natural lines to write in it, export NAME=value, is thrown away with only a log message to show for it. Every behavior below was tested with systemd 255 (the version in Ubuntu 24.04), running as PID 1 inside an isolated container, using a test service that printed every variable and argument it received.

Video: tutoriaLinux on YouTube

The two settings

Environment=

Variables can be set directly in the unit file’s [Service] section:

[Service]
Environment=GREETING=hi LEVEL=from-unit

The systemd.exec manual says each line becomes a list of assignments, and that to assign a value containing spaces, quotes go around the whole assignment, as in Environment="GREETING=hello world". It also notes that variable expansion is not performed inside the strings and that $ has no special meaning there.

EnvironmentFile=

The same manual describes EnvironmentFile= as similar, but reading variables from a text file of newline-separated assignments. Empty lines, lines without an =, and lines starting with ; or # are ignored. Keeping values in a separate file means per-machine settings can live outside the unit file, and the unit itself can be shared or copied between machines unchanged.

Person holding a red pencil writing on white paper

The test file

What went in

The environment file was written the way people usually write them, mixing styles on purpose:

# comment line
PLAIN=hello world
QUOTED="hello   world"
SINGLE='it''s'
SPACED = value
export EXPORTED=yes
REF=${PLAIN}/x
LEVEL=from-file
MULTI="line one \
line two"

The service had Environment=GREETING=hi LEVEL=from-unit and EnvironmentFile=/etc/envtest/app.env.

What the service received

GREETING=[hi]
PLAIN=[hello world]
QUOTED=[hello   world]
SINGLE=[its]
SPACED=[value]
EXPORTED=[]
REF=[${PLAIN}/x]
LEVEL=[from-file]
MULTI=[line one line two]

Nine lines in, and four of them did something a shell user might not expect.

Unquoted values keep their spaces

PLAIN=hello world came through whole, as hello world. In a shell, that line would set PLAIN=hello and try to run a command called world. The manual explains that an unquoted value keeps interior whitespace, unlike in a shell, and that leading and trailing whitespace is discarded. That is also why SPACED = value, with spaces around the equals sign, still worked: the spaces were trimmed.

export is not allowed

export EXPORTED=yes produced an empty variable. The journal recorded why:

envtest.service: Ignoring invalid environment assignment 'export EXPORTED=yes': /etc/envtest/app.env

The service still started. Anyone reusing a shell .env file written for source will lose every export line this way, and the only sign is a line in the journal.

No variable expansion

REF=${PLAIN}/x arrived as the literal text ${PLAIN}/x. The file is not run through a shell, so one variable cannot be built from another inside it.

Quotes work like a shell’s, mostly

The double-quoted value kept its three spaces, and the backslash at the end of the MULTI line joined the two lines, discarding the newline as the manual describes. The single-quoted 'it''s' became its: two quoted strings placed side by side, exactly as a shell would read it, not an escaped apostrophe.

Which value wins

The file beats the unit

LEVEL was set to from-unit by Environment= and to from-file in the file. The service saw from-file. The manual states it directly: settings from these files override settings made with Environment=.

Two files, read in order

A unit can list more than one EnvironmentFile=. In a second test, a.env set LEVEL=from-a and PLAIN=a-only, and b.env, listed after it, set only LEVEL=from-b. The service got LEVEL=from-b and PLAIN=a-only. The manual says the files are read in the order they are specified and the later setting overrides the earlier one. A common use is a shipped defaults file followed by an optional local override, EnvironmentFile=-/etc/myapp/local.env, which can be absent without breaking anything.

Quoting inside Environment=

The same test set two variables on one line with each assignment quoted as a whole: Environment="API_KEY=secret-in-unit" "GREETING=hello world". GREETING arrived as hello world, space intact. Quoting just the value, as in GREETING="hello world", is the shell habit; the manual’s rule is to quote the whole assignment.

Drop-ins and the manager environment

A drop-in file (/etc/systemd/system/envtest.service.d/override.conf, the kind systemctl edit creates) set GREETING=from-dropin, and at the same time systemctl set-environment GREETING=from-manager set it for the whole service manager. The service saw from-dropin: a variable set in the unit’s own configuration won over the manager-wide one.

Checking what a unit will get

systemctl show prints the configured values without starting anything:

$ systemctl show envtest.service -p Environment -p EnvironmentFiles
Environment=GREETING=from-dropin LEVEL=from-unit
EnvironmentFiles=/etc/envtest/app.env (ignore_errors=no)

It shows the Environment= lines and which files will be read, but not the file contents. For those, the only full check is to run the service and look.

White printing paper on a white surface

Missing files and changed files

A missing file stops the service

Pointing EnvironmentFile= at a file that did not exist made the start fail:

Job for envtest.service failed because of unavailable resources or another system error.
envtest.service: Failed to load environment files: No such file or directory

The manual says that if the file does not exist, cannot be read or has invalid content, the service fails to start, and that prefixing the path with - makes it optional. With EnvironmentFile=-/etc/envtest/missing.env, the same service started normally.

Edits take effect on the next start

After changing LEVEL in the file, without running systemctl daemon-reload, the next start printed LEVEL=[changed-in-file]. The file is read each time the service starts, so editing it needs only a restart of the service. Editing the unit file itself, including its Environment= lines, needs daemon-reload first, as the earlier post on Restart=on-failure also showed for other settings.

What a service starts with

The manager’s own environment

Before any of those settings apply, a service inherits the service manager’s environment, which systemctl show-environment prints. In the test container it held LANG=C.UTF-8, a PATH, and anything added with systemctl set-environment. A service with no environment settings at all printed:

REGION=[us-east] PATH=[/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin] HOME=[]

REGION, set earlier with set-environment, reached it without being asked for. HOME was empty: a system service running as root has no login, so nothing sets it. Scripts that write to ~/something will not find the directory they expect.

A short PATH

That default PATH contains four directories. Anything installed elsewhere, such as /opt/app/bin or a user’s ~/.local/bin, is not found by name, which is behind many “command not found” failures that only happen under systemd. Setting Environment=PATH=/opt/app/bin:/usr/bin:/bin in a second test service replaced it completely, and UnsetEnvironment=LANG removed LANG, which came through empty. Using full paths in ExecStart= avoids the question entirely.

Three white mail envelopes

Variables on the command line

$VAR versus ${VAR}

The test service was started with ExecStart=/usr/local/bin/showenv $PLAIN ${PLAIN} $QUOTED and printed its arguments:

argc=5 args: [hello] [world] [hello world] [hello] [world]

$PLAIN was split into two arguments, ${PLAIN} stayed as one, and $QUOTED was split too, its three spaces collapsing. The systemd.service manual describes exactly this: ${FOO} is replaced by the exact value including all whitespace, always resulting in exactly a single argument, while $FOO as a separate word is split at whitespace into zero or more arguments.

Which to use

For a path, a password or anything that might contain a space, ${VAR} is the safe form. $VAR is for a variable that deliberately holds several options, such as OPTS=--verbose --port 8080, meant to become several arguments.

A pattern that works

The layout

Put defaults in the unit with Environment=, put per-machine values in a file under /etc and point the unit at it with EnvironmentFile=, and use ${VAR} in ExecStart=.

Not for passwords

The systemd.exec manual is blunt about secrets. It says environment variables are not suitable for passing secrets, such as passwords and key material, to services: variables set for a unit are exposed to unprivileged clients over D-Bus, and they are passed down the process tree, including across security boundaries, where they can leak. The API_KEY in the test above is exactly what not to do. The manual points instead to LoadCredential= and its encrypted variants, which hand the service a file in a private directory named by $CREDENTIALS_DIRECTORY, readable only by the service’s user and root. Locking down the environment file’s permissions protects the file on disk, but not the values once systemd has loaded them.

Rules for the file

One NAME=value per line, no export, no references to other variables, double quotes around anything with leading spaces or special characters, and comments on their own lines. After editing, restart the service and check its journal for an “Ignoring invalid environment assignment” line, the one message that says a variable never arrived. The earlier post on .bashrc and .bash_profile covers why the variables in a login shell never reach a service in the first place.

Sources

  • systemd.exec(5), Linux manual page (man7.org): https://man7.org/linux/man-pages/man5/systemd.exec.5.html
  • systemd.service(5), Linux manual page (man7.org): https://man7.org/linux/man-pages/man5/systemd.service.5.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

One Response to “Environment Variables in systemd Services: Environment=, EnvironmentFile, and the Lines That Silently Disappear”

Leave a Reply

Your email address will not be published. Required fields are marked *