4

How can I set an env variable for all the users even the nologin ones?

In particular I want to set an env variable containing a path that will be used from users script and the webserver (www-data that is a nologin shell user and then can't benefits from PAM's /etc/environment nor /etc/profile and similars)

2
  • Just to clarify: you want the envvar set for (interactive) login sessions started through SSH or locally, and also for scripts (CGI, PHP, whatever) started by the HTTP server? What HTTP server are you running?
    – ilkkachu
    May 16, 2018 at 10:37
  • @ilkkachu, yes it is correct, I want both. I'm using apache and I already have set /etc/envinroment and /etc/apache2/envvars to achieve my goal, but what if I'll need to use also nodejs or other service? I have to set for them all. I was looking for one only config (like /etc/envinroment but that works for all users)
    – Luca Ricci
    May 16, 2018 at 10:45

2 Answers 2

1

Assuming you use the default init system:

https://www.freedesktop.org/software/systemd/man/systemd-system.conf.html

DefaultEnvironment=

Sets manager environment variables passed to all executed processes. Takes a space-separated list of variable assignments. See environ(7) for details about environment variables.

Example:

DefaultEnvironment="VAR1=word1 word2" VAR2=word3 "VAR3=word 5 6"

Sets three variables "VAR1", "VAR2", "VAR3".

"all executed processes" here meaning all processes executed by systemd. Generally, all processes are either started by systemd, or children of a process that was. But please be careful with this, otherwise you might have to recover your system using init=/bin/sh etc. :-).

It is possible that some processes will clear their environment variables, or make assignments that supercede these. I think user login (PAM?) would be an important example here.

As you say, you can cover all PAM sessions using pam_env / /etc/environment.

1
  • thanks! setting the variable in both /etc/systemd/system.conf and /etc/environment did the trick it's more generic then my solution (/etc/apache2/envvars) but still has 2 conf that I have to update, I'll wait a little bit for other solutions, if none than I'll accept this as answer.
    – Luca Ricci
    May 16, 2018 at 13:36
0

If you mean as all users - same user, starting service, both login and nologin - you should set variable via systemd. On my system i tried to edit $HOME/.profile, but this affected only login user. I will show examples for my zabbix-agent and PATH variable.

There is practice to use parameter EnvironmentFile in default service units. Search for this entry in your unit file and edit/create specified file. You can find the name of unit file in output of systemctl status zabbix-agent . Specify variables in VARIABLE=value format.

If there is no EnvironmentFile in your unit, then you can override default systemd unit.

systemctl edit zabbix-agent.service

Then write

[Service]
Environment="PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/pgsql-9.6/bin"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.