System wide variables across all users and all shells. How?

Hello all!

What is the best way to define system wide variable that will be available for all users under any of shells?

Means seeking solutions that not require me to define something for each of user separately in ~/.profile or ~/.login.conf or any other files. Means seeking solution to define it at one place for every user under any of shells.
 
I've once tried defining environment variables for all user on/etc/rc.conf[.local], without luck.
Not tried, but
  • creating something like /etc/login.local (for /bin/[t]csh as shell)
  • for variables for base components and site-local ones,
  • /usr/local/etc/login.local for anything others (needed only for ports components and host-local ones)
  • /usr/local/etc/login.conf is included from /etc/login.local if exists (conditionally).
  • /etc/login.local alone is included from ~/.login of all users.
could work.

Note that how to define environment variables depends on the shell used, so these configuration files shoule be prepared for all type of shells (/bin/sh, /bin/csh, /usr/local/bin/zsh, and so on) used in the host separately.
 
[…] What is the best way to define system wide variable that will be available for all users under any of shells?
Do you mean you declare one variable and if one user changes the variable’s value, all other logged‑in users see that changed variable’s value, too? This is not possible. Well, at least not without effort.​

If you mean, you set the initial environ(7)ment variable’s value but otherwise each user maintains their own independent copy: There’s /etc/login.conf(5), there’s /etc/profile (sh(1) and many shells(5) consult this file), there’s pam_putenv(3) (should you program a PAM module) … familiarize with each of them to assess what’s the best for your situation.​
 
Yep, setenv from login.conf(5) is probably the first option to look at in the described scenario if we are talking about any shell.
It would be. But I'm not 100% sure if it always promised to work for shell scripts invoked by nologin users (usually for daemons) and every shells in the wild (including ones which does not in ports collection).
 
It would be. But I'm not 100% sure if it always promised to work for shell scripts invoked by nologin users (usually for daemons) and every shells in the wild (including ones which does not in ports collection).

I think that if you come in via a ssh command (not a login) there are shells that don't load any dotfiles. I also think FreeBSD's /bin/sh is one.
 
It would be. But I'm not 100% sure if it always promised to work for shell scripts invoked by nologin users (usually for daemons) and every shells in the wild (including ones which does not in ports collection).
You're right about the scripts, I just thought OP was asking about interactive shells.

And yes, sh(1) doesn't source any files by default unless invoked as a login shell, but it can (again, if run interactively) if the ENV variable is set.
 
Back
Top