I've set my PATH
in /etc/environment
, and it works when I log in as any user, but when I run my services as any user, it doesn't respect my /etc/environment
.
What gives?
1 Answer
When a user logs in, the PAM libraries are called, and usually the PAM configuration calls pam_env.so
to set up the environment for the user. This setup includes reading /etc/environment
.
In a traditional SysVinit service startup script, you might use su
to start a service as a non-root user; su
would also call the PAM libraries and so would read /etc/environment
, much like a regular user login.
But systemd
is designed to also work in embedded systems where PAM libraries might not be present at all. So if a systemd service is configured to run as a non-root user, systemd
will handle the environment initialization without relying on any external libraries.
See the ENVIRONMENT
paragraph of the systemd.exec(5)
man page for settings you can use in the service file to adjust the service's environment. The defaults provided by systemd
are very minimal, so you should explicitly initialize what your service needs (which is usually a good practice).
For example, you might simply add
EnvironmentFile=/etc/environment
into your service definition.
But if your service needs to start only a few other things, it might be more robust security-wise to explicitly specify them as absolute pathnames in some configuration file, instead of relying on PATH
which might get modified for reasons unrelated to your service, potentially causing nasty surprises.