32

I have a Debian Lenny server, and I would like the www-data user to have /usr/local/zend/bin in its PATH, so it can execute a script in cron as www-data.

How do I add /usr/local/zend/bin to PATH, so www-data can execuate files in /usr/local/zend/bin ?

Sandra
  • 10,303
  • 38
  • 112
  • 165

3 Answers3

46

The first place where PATH is set is /etc/login.defs. There's a setting for root and a setting for everyone else.

Another place where you can define environment variables is /etc/environment. These settings will apply to everyone (you can't write arbitrary shell code there).

A third place where you can define environment variables is /etc/profile. There you can write arbitrary shell code. If you want a user-specific setting, there is the corresponding per-user file ~www-data/.profile. But this will only apply to console interactive logins; in particular it won't apply to cron jobs unless they explicitly source /etc/profile.

If you only need that PATH setting in a user crontab, you can write it at the beginning of the crontab. Note that you need the full list (PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/zend/bin), you can't use a variable substitution (PATH=$PATH:/usr/local/zend/bin won't work there).

  • ----how do you reload e.g. /etc/login.defs? – MrCalvin Apr 24 '17 at 18:20
  • @MrCalvin By logging in. – Gilles 'SO- stop being evil' Apr 24 '17 at 19:40
  • 1
    Is there any reasons `/etc/login.defs` would be ignored? I've updated `ENV_PATH` and am echoing $PATH when .bashrc is loaded. Somewhere in the middle it's either being changed, or it's not updated as it's displaying the old value. I've restarted the computer - so it's not that. – AnnanFay May 26 '17 at 16:04
  • @Annan Chances are that it's being changed somewhere. Do note that my answer was written in 2010, before systemd; if your system uses systemd, it may have its own way to define `PATH` in addition or in replacement of other ways. – Gilles 'SO- stop being evil' May 26 '17 at 17:43
  • @AnnanFay: I think that depends on the distro you use. I noticed that on Debian 11, /etc/login.defs would be completely overridden by /etc/profile – at least for Bash and any other shell that utilize /etc/profile. – Carl Winbäck Mar 17 '22 at 09:19
9

To set a path for all users except root, edit /etc/profile or /etc/enviroment. For root or an individual user edit their .bashrc or .bash_profile in their home directories, respectively. Add the PATH=$PATH:/new/location/.

simhumileco
  • 155
  • 1
  • 8
David Rickman
  • 3,320
  • 18
  • 16
2

To simply set var for all users while not burden your profile, you can add your own small script in /etc/profile.d directory like that:

echo 'PATH="/usr/local/zend/bin:$PATH"' > /etc/profile.d/zend_path.sh

Then relogin.

user3132194
  • 179
  • 5